forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headinfo.go
89 lines (71 loc) · 2.76 KB
/
headinfo.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
package bstream
import (
"context"
"fmt"
"sync"
"time"
"github.com/streamingfast/dgrpc"
pbheadinfo "github.com/streamingfast/pbgo/sf/headinfo/v1"
)
func NetworkLIBBlockRefGetter(headinfoServiceAddr string) BlockRefGetter {
return blockRefGetter(headinfoServiceAddr, pbheadinfo.HeadInfoRequest_NETWORK, func(resp *pbheadinfo.HeadInfoResponse) BlockRef {
return &BasicBlockRef{id: resp.LibID, num: resp.LibNum}
})
}
func NetworkHeadBlockRefGetter(headinfoServiceAddr string) BlockRefGetter {
return blockRefGetter(headinfoServiceAddr, pbheadinfo.HeadInfoRequest_NETWORK, func(resp *pbheadinfo.HeadInfoResponse) BlockRef {
return &BasicBlockRef{id: resp.HeadID, num: resp.HeadNum}
})
}
func StreamLIBBlockRefGetter(headinfoServiceAddr string) BlockRefGetter {
return blockRefGetter(headinfoServiceAddr, pbheadinfo.HeadInfoRequest_STREAM, func(resp *pbheadinfo.HeadInfoResponse) BlockRef {
return &BasicBlockRef{id: resp.LibID, num: resp.LibNum}
})
}
func StreamHeadBlockRefGetter(headinfoServiceAddr string) BlockRefGetter {
return blockRefGetter(headinfoServiceAddr, pbheadinfo.HeadInfoRequest_STREAM, func(resp *pbheadinfo.HeadInfoResponse) BlockRef {
return &BasicBlockRef{id: resp.HeadID, num: resp.HeadNum}
})
}
func GetStreamHeadInfo(ctx context.Context, addr string) (head BlockRef, lib BlockRef, err error) {
cli, err := headInfoClient(addr)
if err != nil {
return nil, nil, err
}
resp, err := cli.GetHeadInfo(ctx, &pbheadinfo.HeadInfoRequest{Source: pbheadinfo.HeadInfoRequest_STREAM})
if err != nil {
return nil, nil, err
}
return NewBlockRef(resp.HeadID, resp.HeadNum), NewBlockRef(resp.LibID, resp.LibNum), nil
}
func headInfoClient(addr string) (pbheadinfo.HeadInfoClient, error) {
conn, err := dgrpc.NewInternalClient(addr)
if err != nil {
return nil, fmt.Errorf("reaching out to headinfo service %q: %w", addr, err)
}
return pbheadinfo.NewHeadInfoClient(conn), nil
}
func blockRefGetter(headinfoServiceAddr string, source pbheadinfo.HeadInfoRequest_Source, extract func(resp *pbheadinfo.HeadInfoResponse) BlockRef) BlockRefGetter {
var lock sync.Mutex
var headinfoCli pbheadinfo.HeadInfoClient
return func(ctx context.Context) (BlockRef, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
lock.Lock()
defer lock.Unlock()
if headinfoCli == nil {
cli, err := headInfoClient(headinfoServiceAddr)
if err != nil {
return nil, err
}
headinfoCli = cli
}
resp, err := headinfoCli.GetHeadInfo(ctx, &pbheadinfo.HeadInfoRequest{Source: source})
if err == nil && resp.HeadNum != 0 {
return extract(resp), nil
}
// TODO: distinguish the remote `NotFound` and another error, return `NotFound` is it was
// indeed not found, and its not another type of error.
return nil, ErrTrackerBlockNotFound
}
}