-
Notifications
You must be signed in to change notification settings - Fork 12
/
dial_factory.go
44 lines (37 loc) · 1.22 KB
/
dial_factory.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
package grpc_client
import (
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/config"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/grpc_client/interceptor"
"google.golang.org/grpc"
"log"
)
type DialFactory interface {
GetEventsProcessingPlatformConn() (*grpc.ClientConn, error)
Close(conn *grpc.ClientConn)
}
type DialFactoryImpl struct {
conf *config.GrpcClientConfig
eventsProcessingPlatformConn *grpc.ClientConn
}
func (dfi DialFactoryImpl) Close(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.Printf("Error closing connection: %v", err)
}
}
func (dfi DialFactoryImpl) GetEventsProcessingPlatformConn() (*grpc.ClientConn, error) {
if dfi.eventsProcessingPlatformConn != nil {
return dfi.eventsProcessingPlatformConn, nil
}
conn, err := grpc.Dial(dfi.conf.EventsProcessingPlatformUrl, grpc.WithInsecure(),
grpc.WithUnaryInterceptor(
interceptor.ApiKeyEnricher(dfi.conf.EventsProcessingPlatformApiKey),
))
dfi.eventsProcessingPlatformConn = conn
return conn, err
}
func NewDialFactory(conf *config.GrpcClientConfig) DialFactory {
dfi := new(DialFactoryImpl)
dfi.conf = conf
return *dfi
}