-
Notifications
You must be signed in to change notification settings - Fork 148
/
grpc_connector.go
70 lines (60 loc) · 1.74 KB
/
grpc_connector.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package clientpool
import (
"context"
"fmt"
"io"
"log"
"plumbing"
"google.golang.org/grpc"
)
type GRPCConnector struct {
doppler string
zonePrefix string
dial DialFunc
ingestorClient IngestorClientFunc
opts []grpc.DialOption
}
type DialFunc func(string, ...grpc.DialOption) (*grpc.ClientConn, error)
type IngestorClientFunc func(*grpc.ClientConn) plumbing.DopplerIngestorClient
func MakeGRPCConnector(
doppler string,
zonePrefix string,
df DialFunc,
cf IngestorClientFunc,
opts ...grpc.DialOption,
) GRPCConnector {
return GRPCConnector{
doppler: doppler,
zonePrefix: zonePrefix,
dial: df,
ingestorClient: cf,
opts: opts,
}
}
func (c GRPCConnector) Connect() (io.Closer, plumbing.DopplerIngestor_PusherClient, error) {
closer, pusher, err := c.connect(c.zonePrefix + "." + c.doppler)
if err != nil {
return c.connect(c.doppler)
}
return closer, pusher, err
}
func (c GRPCConnector) connect(doppler string) (io.Closer, plumbing.DopplerIngestor_PusherClient, error) {
conn, err := c.dial(doppler, c.opts...)
if err != nil {
return nil, nil, fmt.Errorf("error dialing ingestor stream to %s: %s", c, err)
}
client := c.ingestorClient(conn)
log.Printf("successfully connected to doppler %s", c)
pusher, err := client.Pusher(context.Background())
if err != nil {
// TODO: this close is not tested as we don't know how to assert
// against a grpc.ClientConn being closed.
conn.Close()
return nil, nil, fmt.Errorf("error establishing ingestor stream to %s: %s", c, err)
}
log.Printf("successfully established a stream to doppler %s", c)
return conn, pusher, err
}
func (c GRPCConnector) String() string {
return fmt.Sprintf("[%s]%s", c.zonePrefix, c.doppler)
}