-
Notifications
You must be signed in to change notification settings - Fork 0
/
supervisor.go
46 lines (35 loc) · 1.02 KB
/
supervisor.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
package adapter
import (
"github.com/sakuraapp/api/internal/config"
supervisorpb "github.com/sakuraapp/protobuf/supervisor"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/resolver"
)
type SupervisorAdapter struct {
supervisorpb.SupervisorServiceClient
conn *grpc.ClientConn
}
func (a *SupervisorAdapter) Conn() *grpc.ClientConn {
return a.conn
}
func NewSupervisorAdapter(conf *config.Config) (*SupervisorAdapter, error) {
creds, err := credentials.NewClientTLSFromFile(conf.SupervisorKeyPath, "")
if err != nil {
return nil, err
}
resolver.SetDefaultScheme("dns")
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [ { "round_robin": {} } ] }`), // use round-robin LB
}
conn, err := grpc.Dial(conf.SupervisorAddr, opts...)
if err != nil {
return nil, err
}
client := supervisorpb.NewSupervisorServiceClient(conn)
return &SupervisorAdapter{
SupervisorServiceClient: client,
conn: conn,
}, nil
}