forked from bittorrent/go-btfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts.go
65 lines (58 loc) · 1.81 KB
/
hosts.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
package spin
import (
"context"
"fmt"
"time"
"github.com/TRON-US/go-btfs/core"
"github.com/TRON-US/go-btfs/core/commands/storage"
"github.com/TRON-US/go-btfs/core/commands/store/hosts"
"github.com/TRON-US/go-btfs/core/commands/store/stats"
cmds "github.com/TRON-US/go-btfs-cmds"
)
const (
hostSyncPeriod = 60 * time.Minute
hostStatsSyncPeriod = 30 * time.Minute
hostSettingsSyncPeriod = 60 * time.Minute
hostSyncTimeout = 30 * time.Second
)
func Hosts(node *core.IpfsNode, env cmds.Environment) {
cfg, err := node.Repo.Config()
if err != nil {
log.Errorf("Failed to get configuration %s", err)
return
}
if cfg.Experimental.HostsSyncEnabled {
m := cfg.Experimental.HostsSyncMode
fmt.Printf("Storage host info will be synced at [%s] mode\n", m)
go periodicHostSync(hostSyncPeriod, hostSyncTimeout, "hosts",
func(ctx context.Context) error {
return hosts.SyncHosts(ctx, node, m)
})
}
if cfg.Experimental.StorageHostEnabled {
fmt.Println("Current host stats will be synced")
go periodicHostSync(hostStatsSyncPeriod, hostSyncTimeout, "host stats",
func(ctx context.Context) error {
return stats.SyncStats(ctx, cfg, node, env)
})
fmt.Println("Current host settings will be synced")
go periodicHostSync(hostSettingsSyncPeriod, hostSyncTimeout, "host settings",
func(ctx context.Context) error {
_, err = storage.GetHostStorageConfig(ctx, node)
return err
})
}
}
func periodicHostSync(period, timeout time.Duration, msg string, syncFunc func(context.Context) error) {
tick := time.NewTicker(period)
defer tick.Stop()
// Force tick on immediate start
for ; true; <-tick.C {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err := syncFunc(ctx)
if err != nil {
log.Errorf("Failed to sync %s: %s", msg, err)
}
}
}