/
client.go
66 lines (60 loc) · 1.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
package rpcbroadcaster
import (
"context"
"time"
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"go.uber.org/zap"
)
// RPCClient represent rpc client for a single node.
type RPCClient struct {
client *client.Client
addr string
close chan struct{}
responses chan request.RawParams
log *zap.Logger
sendTimeout time.Duration
method SendMethod
}
// SendMethod represents rpc method for sending data to other nodes.
type SendMethod func(*client.Client, request.RawParams) error
// NewRPCClient returns new rpc client for provided address and method.
func (r *RPCBroadcaster) NewRPCClient(addr string, method SendMethod, timeout time.Duration, ch chan request.RawParams) *RPCClient {
return &RPCClient{
addr: addr,
close: r.close,
responses: ch,
log: r.Log.With(zap.String("address", addr)),
sendTimeout: timeout,
method: method,
}
}
func (c *RPCClient) run() {
// We ignore error as not every node can be available on startup.
c.client, _ = client.New(context.Background(), c.addr, client.Options{
DialTimeout: c.sendTimeout,
RequestTimeout: c.sendTimeout,
})
for {
select {
case <-c.close:
return
case ps := <-c.responses:
if c.client == nil {
var err error
c.client, err = client.New(context.Background(), c.addr, client.Options{
DialTimeout: c.sendTimeout,
RequestTimeout: c.sendTimeout,
})
if err != nil {
c.log.Error("failed to create client to submit oracle response", zap.Error(err))
continue
}
}
err := c.method(c.client, ps)
if err != nil {
c.log.Error("error while submitting oracle response", zap.Error(err))
}
}
}
}