/
sinks.go
138 lines (113 loc) · 2.92 KB
/
sinks.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
package mining
import (
"fmt"
"log"
"time"
"net/http"
"encoding/json"
"github.com/gorilla/websocket"
)
type HashRateSink interface {
SetCurrentHashRates(map[int]float64) error
}
type hashRateStdOutSink struct {}
type hashRateSocketSink struct {
sockets map[*websocket.Conn]bool
upgrader *websocket.Upgrader
sendFrequency int // Number of seconds between sends
lastSendTimestamp int64
lastTotalHashRate float64
}
func NewHashRateStdOutSink() *hashRateStdOutSink {
return &hashRateStdOutSink{}
}
type CurrentHashRate struct {
Timestamp int64 `json:"timestamp"`
HashRate float64 `json:"hashrate"`
}
func NewHashRateSocketSink(endpoint string, sendFrequency int) *hashRateSocketSink {
sockets := make(map[*websocket.Conn]bool)
upgrader := &websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
sink := &hashRateSocketSink{
sockets: sockets,
sendFrequency: sendFrequency,
lastSendTimestamp: 0,
lastTotalHashRate: 0,
upgrader: upgrader,
}
http.HandleFunc("/hashrate/stream", func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sockets[conn] = true
})
http.HandleFunc("/hashrate", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
fmt.Println("Invalid HTTP Method for GET /hashrate")
return
}
currentHashRate := CurrentHashRate{
Timestamp: sink.lastSendTimestamp,
HashRate: sink.lastTotalHashRate,
}
js, err := json.Marshal(currentHashRate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
})
go func() {
http.ListenAndServe(endpoint, nil)
}()
return sink
}
func (s *hashRateStdOutSink) SetCurrentHashRates(hashRates map[int]float64) error {
fmt.Print("\r")
var total float64
for minerID, hashRate := range hashRates {
fmt.Printf("%d-%.1f ", minerID, hashRate)
total += hashRate
}
fmt.Printf("Total: %.2f MH/s", total)
return nil
}
func (s *hashRateSocketSink) SetCurrentHashRates(hashRates map[int]float64) error {
var total float64
for _, hashRate := range hashRates {
total += hashRate
}
timestamp := time.Now().Unix()
if timestamp - s.lastSendTimestamp < int64(s.sendFrequency) {
return nil
}
s.lastSendTimestamp = timestamp
s.lastTotalHashRate = total
for socket := range s.sockets {
currentHashRate := CurrentHashRate{
Timestamp: s.lastSendTimestamp,
HashRate: s.lastTotalHashRate,
}
js, err := json.Marshal(currentHashRate)
if err != nil {
log.Printf("Websocket error: %s", err)
continue
}
err = socket.WriteMessage(websocket.TextMessage, []byte(js))
if err != nil {
log.Printf("Websocket error: %s", err)
socket.Close()
delete(s.sockets, socket)
}
}
return nil
}