forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
88 lines (78 loc) · 2.2 KB
/
client.go
File metadata and controls
88 lines (78 loc) · 2.2 KB
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
package lotus
import (
"context"
"fmt"
"net/http"
"time"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/lotus/api/apistruct"
logging "github.com/ipfs/go-log/v2"
ma "github.com/multiformats/go-multiaddr"
"github.com/textileio/powergate/util"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
)
var (
heightProbingInterval = time.Second * 10
log = logging.Logger("lotus-client")
)
// ClientBuilder creates a new Lotus client.
type ClientBuilder func() (*apistruct.FullNodeStruct, func(), error)
// NewBuilder creates a new ClientBuilder.
func NewBuilder(maddr ma.Multiaddr, authToken string, connRetries int) (ClientBuilder, error) {
addr, err := util.TCPAddrFromMultiAddr(maddr)
if err != nil {
return nil, err
}
headers := http.Header{
"Authorization": []string{"Bearer " + authToken},
}
return func() (*apistruct.FullNodeStruct, func(), error) {
var api apistruct.FullNodeStruct
var closer jsonrpc.ClientCloser
var err error
for i := 0; i < connRetries; i++ {
closer, err = jsonrpc.NewMergeClient(context.Background(), "ws://"+addr+"/rpc/v0", "Filecoin",
[]interface{}{
&api.Internal,
&api.CommonStruct.Internal,
}, headers)
if err == nil {
break
}
log.Warnf("failed to connect to Lotus client %s, retrying...", err)
time.Sleep(time.Second * 5)
}
if err != nil {
return nil, nil, fmt.Errorf("couldn't connect to Lotus API: %s", err)
}
return &api, closer, nil
}, nil
}
// MonitorLotusSync fires a goroutine that will generate
// metrics with Lotus node height.
func MonitorLotusSync(clientBuilder ClientBuilder) {
if err := view.Register(vHeight); err != nil {
log.Fatalf("register metrics views: %v", err)
}
go func() {
for {
refreshHeightMetric(clientBuilder)
time.Sleep(heightProbingInterval)
}
}()
}
func refreshHeightMetric(clientBuilder ClientBuilder) {
c, cls, err := clientBuilder()
if err != nil {
log.Error("creating lotus client for monitoring: %s", err)
return
}
defer cls()
heaviest, err := c.ChainHead(context.Background())
if err != nil {
log.Errorf("get lotus sync status: %s", err)
return
}
stats.Record(context.Background(), mLotusHeight.M(int64(heaviest.Height())))
}