This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsync.go
157 lines (124 loc) · 3.12 KB
/
sync.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package sync
import (
"fmt"
"log"
"sync"
"time"
"github.com/siacentral/sia-host-dashboard/dashboard/persist"
siaapi "gitlab.com/NebulousLabs/Sia/node/api/client"
)
var (
apiClient *siaapi.UnsafeClient
counters *bandwidthCounters
bandwidthMu sync.Mutex
)
type bandwidthCounters struct {
lastUpload uint64
lastDownload uint64
totalUpload uint64
totalDownload uint64
}
func waitInterval(d time.Duration) {
current := time.Now()
sleepTime := current.Add(d).Truncate(d).Sub(current)
time.Sleep(sleepTime)
}
func refreshContracts() {
for {
if err := syncContracts(); err != nil {
log.Println("refreshing contracts:", err)
time.Sleep(time.Second * 30)
continue
}
waitInterval(time.Minute * 10)
}
}
func refreshConnectivity() {
for {
if err := syncHostConnectivity(); err != nil {
log.Println("refreshing connectivity:", err)
time.Sleep(time.Second * 30)
continue
}
waitInterval(time.Minute * 10)
}
}
func refreshStatus() {
for {
if err := syncHostStatus(); err != nil {
log.Println("refreshing status:", err)
time.Sleep(time.Second * 30)
continue
}
waitInterval(time.Second * 10)
}
}
func getBandwidthUsage() (upload, download uint64) {
bandwidthMu.Lock()
defer bandwidthMu.Unlock()
bw, err := apiClient.HostBandwidthGet()
if err != nil {
log.Printf("warn: unable to retrieve bandwidth: %s", err)
return
}
dUp := bw.Upload
dDown := bw.Download
if dUp >= counters.lastUpload {
dUp -= counters.lastUpload
}
if dDown >= counters.lastDownload {
dDown -= counters.lastDownload
}
upload = counters.totalUpload + dUp
download = counters.totalDownload + dDown
counters.totalUpload = upload
counters.totalDownload = download
counters.lastUpload = bw.Upload
counters.lastDownload = bw.Download
return
}
// initializes the bandwidth counters from the database to count the total bandwidth usage of the
// host. This will cause us to lose any existing bytes on initialization, but should prevent counting
// bandwidth twice
func initBandwidthCounters() {
bandwidthMu.Lock()
defer bandwidthMu.Unlock()
meta, err := persist.GetLastMetadata()
if err != nil {
log.Printf("warn: unable to load bandwidth: %s", err)
return
}
bw, err := apiClient.HostBandwidthGet()
if err != nil {
log.Printf("warn: unable to retrieve bandwidth: %s", err)
return
}
counters = new(bandwidthCounters)
counters.totalUpload = meta.UploadBandwidth
counters.totalDownload = meta.DownloadBandwidth
counters.lastUpload = bw.Upload
counters.lastDownload = bw.Download
}
//Start begins syncing data from Sia
func Start(siaAddr string) error {
apiClient = siaapi.NewUnsafeClient(siaapi.Client{
Options: siaapi.Options{
Address: siaAddr,
UserAgent: "Sia-Agent",
},
})
initBandwidthCounters()
if err := syncContracts(); err != nil {
return fmt.Errorf("refreshing contracts: %w", err)
}
if err := syncHostConnectivity(); err != nil {
log.Println(fmt.Errorf("refreshing connectivity: %w", err))
}
if err := syncHostStatus(); err != nil {
return fmt.Errorf("refreshing status: %w", err)
}
go refreshContracts()
go refreshStatus()
go refreshConnectivity()
return nil
}