slaventius 2 years ago
parent 270244b998
commit cb6a45544b
  1. 2
      cmd/main.go
  2. 8
      internal/transport/grpc/grpc.go
  3. 6
      internal/transport/kafka/kafka_reader.go
  4. 6
      internal/transport/kafka/kafka_writer.go

@ -18,7 +18,7 @@ import (
func main() { func main() {
config := config.NewConfig() config := config.NewConfig()
ctx, ctxCancel := context.WithCancel(context.Background()) ctx, ctxCancel := context.WithCancel(context.Background())
srv := server.NewServer(config) srv := server.NewServer(ctx, config)
s := grpc.NewServer() s := grpc.NewServer()
// //

@ -31,14 +31,16 @@ type AuthDBServer struct {
users map[string]*user users map[string]*user
kafkaWriter *kafka.KafkaWriter kafkaWriter *kafka.KafkaWriter
api.UnimplementedAuthDBServer api.UnimplementedAuthDBServer
id int32 ctx context.Context
id int32
} }
func NewServer(config *config.Config) *AuthDBServer { func NewServer(ctx context.Context, config *config.Config) *AuthDBServer {
return &AuthDBServer{ return &AuthDBServer{
mu: sync.Mutex{}, mu: sync.Mutex{},
users: make(map[string]*user), users: make(map[string]*user),
kafkaWriter: kafka.NewWriter(topicRegistrations, net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))), kafkaWriter: kafka.NewWriter(ctx, topicRegistrations, net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))),
ctx: ctx,
id: 0, id: 0,
} }
} }

@ -1,17 +1,21 @@
package kafka package kafka
import ( import (
"context"
"github.com/segmentio/kafka-go" "github.com/segmentio/kafka-go"
) )
type KafkaReader struct { type KafkaReader struct {
ctx context.Context
reader *kafka.Reader reader *kafka.Reader
first string first string
topic string topic string
} }
func NewReader(topic string, address ...string) *KafkaReader { func NewReader(ctx context.Context, topic string, address ...string) *KafkaReader {
return &KafkaReader{ return &KafkaReader{
ctx: ctx,
reader: kafka.NewReader(kafka.ReaderConfig{ reader: kafka.NewReader(kafka.ReaderConfig{
Topic: topic, Topic: topic,
Brokers: address, Brokers: address,

@ -10,13 +10,15 @@ import (
) )
type KafkaWriter struct { type KafkaWriter struct {
ctx context.Context
writer *kafka.Writer writer *kafka.Writer
first string first string
topic string topic string
} }
func NewWriter(topic string, address ...string) *KafkaWriter { func NewWriter(ctx context.Context, topic string, address ...string) *KafkaWriter {
s := &KafkaWriter{ s := &KafkaWriter{
ctx: ctx,
writer: &kafka.Writer{ writer: &kafka.Writer{
Topic: topic, Topic: topic,
Balancer: &kafka.LeastBytes{}, Balancer: &kafka.LeastBytes{},
@ -115,7 +117,7 @@ func (s *KafkaWriter) checkTopic() error {
} }
func (s *KafkaWriter) WriteMessage(key string, value string) error { func (s *KafkaWriter) WriteMessage(key string, value string) error {
return s.writer.WriteMessages(context.Background(), kafka.Message{ return s.writer.WriteMessages(s.ctx, kafka.Message{
Key: []byte(key), Key: []byte(key),
Value: []byte(value), Value: []byte(value),
}) })

Loading…
Cancel
Save