-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
125 lines (108 loc) · 2.7 KB
/
client.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package stream_drpc_client
import (
"context"
"time"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/bifrost/protocol"
stream_drpc "github.com/aperturerobotics/bifrost/stream/drpc"
"github.com/aperturerobotics/controllerbus/bus"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"storj.io/drpc/drpcconn"
)
// Client is a common drpc client implementation.
type Client struct {
// le is the logger
le *logrus.Entry
// b is the bus
b bus.Bus
// c is the config
c *Config
// timeoutDur is the request timeout duration
// if unset, has none
timeoutDir time.Duration
// srcPeer is the src peer id
srcPeer peer.ID
// destPeers is the dest peer ids
destPeers []peer.ID
}
// NewClient constructs a new client.
func NewClient(le *logrus.Entry, b bus.Bus, c *Config) (*Client, error) {
srcPeer, err := c.ParseSrcPeerId()
if err != nil {
return nil, errors.Wrap(err, "src_peer_id")
}
serverPeerIDs, err := c.ParseServerPeerIds()
if err != nil {
return nil, errors.Wrap(err, "src_peer_id")
}
timeoutDur, err := c.ParseTimeoutDur()
if err != nil {
return nil, errors.Wrap(err, "timeout_dur")
}
return &Client{
le: le,
b: b,
c: c,
timeoutDir: timeoutDur,
srcPeer: srcPeer,
destPeers: serverPeerIDs,
}, nil
}
// BuildTimeoutCtx builds a context with the configured timeout.
func (c *Client) BuildTimeoutCtx(ctx context.Context) (context.Context, context.CancelFunc) {
to := c.timeoutDir
if to <= 0 {
return context.WithCancel(ctx)
}
return context.WithTimeout(ctx, to)
}
// ExecuteConnection attempts to contact one of the configured servers and
// execute the given callback, which should construct & use drpc clients.
//
// Callback should return nextServer, err.
// If next=true is returned, tries another server.
func (c *Client) ExecuteConnection(
ctx context.Context,
protocolID protocol.ID,
cb func(conn *drpcconn.Conn) (next bool, err error),
) error {
var lastErr error
for _, destPeer := range c.destPeers {
estCtx, estCtxCancel := c.BuildTimeoutCtx(ctx)
defer estCtxCancel()
le := c.le.WithField("server-peer-id", destPeer.Pretty())
conn, connRel, err := stream_drpc.EstablishDrpcConn(
estCtx,
c.b,
c.c.GetDrpcOpts(),
protocolID,
c.srcPeer, destPeer,
c.c.GetTransportId(),
)
if err != nil {
le.WithError(err).Warn("unable to establish drpc conn")
lastErr = err
continue
}
var tryNext bool
var tryErr error
func() {
// this also catches panic cases.
defer connRel()
defer conn.Close()
tryNext, tryErr = cb(conn)
}()
if tryErr == nil {
return nil
}
lastErr = tryErr
if !tryNext {
break
}
}
if lastErr == nil {
lastErr = errors.New("connection failed")
}
return lastErr
}