-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_eznode.go
58 lines (49 loc) · 1.16 KB
/
new_eznode.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
package eznode
import (
"log"
"sync"
"time"
)
// Option is a functional parameter for NewEzNode
type Option func(*EzNode)
// NewEzNode creates a new EzNode
func NewEzNode(chains []*Chain, options ...Option) *EzNode {
chainHashMap := make(map[string]*Chain)
for _, userChain := range chains {
chainHashMap[userChain.id] = userChain
}
ezNode := &EzNode{
chains: chainHashMap,
apiCaller: &apiCallerClient{
client: createHttpClient(),
},
syncStorage: syncStorage{
interval: 60 * time.Second,
ticker: &time.Ticker{},
done: make(chan bool),
isRun: false,
mutex: &sync.Mutex{},
},
}
for _, option := range options {
option(ezNode)
}
return ezNode
}
// WithApiClient sets the api client
func WithApiClient(apiCaller ApiCaller) Option {
return func(ezNode *EzNode) {
ezNode.apiCaller = apiCaller
}
}
// WithSyncInterval sets the sync interval for calling sync stats function
func WithSyncInterval(
interval time.Duration,
) Option {
if interval <= 0 {
log.Fatal("save interval cannot be less than 0 [less than 10 seconds is not recommended]")
}
return func(ezNode *EzNode) {
ezNode.syncStorage.interval = interval
}
}