-
Notifications
You must be signed in to change notification settings - Fork 6
/
service.go
48 lines (40 loc) · 1.21 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package kafka
import (
"fmt"
"github.com/twmb/franz-go/pkg/kgo"
"go.uber.org/zap"
)
// Service acts as interface to interact with the Kafka Cluster
type Service struct {
Config Config
Logger *zap.Logger
KafkaClient *kgo.Client
}
// NewService creates a new Kafka service and immediately checks connectivity to all components. If any of these external
// dependencies fail an error wil be returned.
func NewService(cfg Config, logger *zap.Logger) (*Service, error) {
kgoOpts, err := NewKgoConfig(&cfg, logger)
if err != nil {
return nil, fmt.Errorf("failed to create a valid kafka client config: %w", err)
}
kafkaClient, err := kgo.NewClient(kgoOpts...)
if err != nil {
return nil, fmt.Errorf("failed to create kafka client: %w", err)
}
return &Service{
Config: cfg,
Logger: logger,
KafkaClient: kafkaClient,
}, nil
}
func (s *Service) NewKafkaClient() (*kgo.Client, error) {
kgoOpts, err := NewKgoConfig(&s.Config, s.Logger)
if err != nil {
return nil, fmt.Errorf("failed to create a valid kafka client config: %w", err)
}
kafkaClient, err := kgo.NewClient(kgoOpts...)
if err != nil {
return nil, fmt.Errorf("failed to create kafka client: %w", err)
}
return kafkaClient, nil
}