mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 14:03:45 +03:00
[hw-6] add notifier service, kafka
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/sarama"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/rs/zerolog"
|
||||
@@ -22,6 +23,7 @@ import (
|
||||
"route256/loms/internal/domain/service"
|
||||
"route256/loms/internal/infra/config"
|
||||
mw "route256/loms/internal/infra/grpc/middleware"
|
||||
"route256/loms/internal/infra/messaging/kafka"
|
||||
"route256/loms/internal/infra/postgres"
|
||||
|
||||
pb "route256/pkg/api/loms/v1"
|
||||
@@ -61,11 +63,20 @@ func NewApp(configPath string) (*App, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
producer, err := setupSaramaAsyncConn([]string{c.Kafka.Brokers})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stockRepo := stocksRepository.NewStockRepository(masterPool, replicaPool)
|
||||
orderRepo := ordersRepository.NewOrderRepository(masterPool)
|
||||
txManager := postgres.NewTxManager(masterPool, replicaPool)
|
||||
kafkaProducer, err := kafka.NewStatusProducer(c.Kafka.OrderTopic, producer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
service := service.NewLomsService(orderRepo, stockRepo, txManager)
|
||||
service := service.NewLomsService(orderRepo, stockRepo, txManager, kafkaProducer)
|
||||
controller := server.NewServer(service)
|
||||
|
||||
app := &App{
|
||||
@@ -79,7 +90,6 @@ func NewApp(configPath string) (*App, error) {
|
||||
func (app *App) Shutdown(ctx context.Context) (err error) {
|
||||
if app.httpServer != nil {
|
||||
err = app.httpServer.Shutdown(ctx)
|
||||
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("failed http gateway server shutdown")
|
||||
}
|
||||
@@ -197,3 +207,19 @@ func getPostgresPools(c *config.Config) (masterPool, replicaPool *pgxpool.Pool,
|
||||
|
||||
return pools[0], pools[1], nil
|
||||
}
|
||||
|
||||
func setupSaramaAsyncConn(brokers []string) (sarama.AsyncProducer, error) {
|
||||
cfg := sarama.NewConfig()
|
||||
cfg.Producer.RequiredAcks = sarama.WaitForAll
|
||||
cfg.Producer.Idempotent = true
|
||||
cfg.Producer.Return.Successes = true
|
||||
cfg.Producer.Retry.Max = 5
|
||||
cfg.Net.MaxOpenRequests = 1
|
||||
|
||||
producer, err := sarama.NewAsyncProducer(brokers, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create async producer: %w", err)
|
||||
}
|
||||
|
||||
return producer, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user