-
Notifications
You must be signed in to change notification settings - Fork 104
/
UpSessionManager.go
253 lines (203 loc) · 6.32 KB
/
UpSessionManager.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"fmt"
"time"
"github.com/golang/glog"
)
type UpSessionInfo struct {
minerNum int
ready bool
upSession UpSession
}
type FakeUpSessionInfo struct {
minerNum int
upSession FakeUpSession
}
type UpSessionManager struct {
id string // 打印日志用的连接标识符
subAccount string
config *Config
parent *SessionManager
upSessions []UpSessionInfo
fakeUpSession FakeUpSessionInfo
eventChannel chan interface{}
initSuccess bool
initFailureCounter int
printingMinerNum bool
}
func NewUpSessionManager(subAccount string, config *Config, parent *SessionManager) (manager *UpSessionManager) {
manager = new(UpSessionManager)
manager.subAccount = subAccount
manager.config = config
manager.parent = parent
upSessions := make([]UpSessionInfo, manager.config.Advanced.PoolConnectionNumberPerSubAccount)
manager.upSessions = upSessions[:]
manager.fakeUpSession.upSession = manager.config.sessionFactory.NewFakeUpSession(manager)
manager.eventChannel = make(chan interface{}, manager.config.Advanced.MessageQueueSize.PoolSessionManager)
if manager.config.MultiUserMode {
manager.id = fmt.Sprintf("<%s> ", manager.subAccount)
}
return
}
func (manager *UpSessionManager) Run() {
go manager.fakeUpSession.upSession.Run()
for i := range manager.upSessions {
go manager.connect(i)
}
manager.handleEvent()
}
func (manager *UpSessionManager) connect(slot int) {
for i := range manager.config.Pools {
up := manager.config.sessionFactory.NewUpSession(manager, i, slot)
up.Init()
if up.Stat() == StatAuthorized {
go up.Run()
manager.SendEvent(EventUpSessionReady{slot, up})
return
}
}
manager.SendEvent(EventUpSessionInitFailed{slot})
}
func (manager *UpSessionManager) SendEvent(event interface{}) {
manager.eventChannel <- event
}
func (manager *UpSessionManager) addDownSession(e EventAddDownSession) {
defer manager.tryPrintMinerNum()
var selected *UpSessionInfo
// 寻找连接数最少的服务器
for i := range manager.upSessions {
info := &manager.upSessions[i]
if info.ready && (selected == nil || info.minerNum < selected.minerNum) {
selected = info
}
}
if selected != nil {
selected.minerNum++
e.Session.SendEvent(EventSetUpSession{selected.upSession})
return
}
// 服务器均未就绪,若已启用 AlwaysKeepDownconn,就把矿机托管给 FakeUpSession
if manager.config.AlwaysKeepDownconn {
manager.fakeUpSession.minerNum++
e.Session.SendEvent(EventSetUpSession{manager.fakeUpSession.upSession})
return
}
// 未启用 AlwaysKeepDownconn,直接断开连接,防止矿机认为 BTCAgent 连接活跃
e.Session.SendEvent(EventPoolNotReady{})
}
func (manager *UpSessionManager) upSessionReady(e EventUpSessionReady) {
defer manager.tryPrintMinerNum()
manager.initSuccess = true
info := &manager.upSessions[e.Slot]
info.upSession = e.Session
info.ready = true
// 从 FakeUpSession 拿回矿机
manager.fakeUpSession.upSession.SendEvent(EventTransferDownSessions{})
}
func (manager *UpSessionManager) upSessionInitFailed(e EventUpSessionInitFailed) {
if manager.initSuccess {
glog.Error(manager.id, "Failed to connect to all ", len(manager.config.Pools), " pool servers, please check your configuration! Retry in 5 seconds.")
go func() {
time.Sleep(5 * time.Second)
manager.connect(e.Slot)
}()
return
}
manager.initFailureCounter++
if manager.initFailureCounter >= len(manager.upSessions) {
glog.Error(manager.id, "Too many connection failure to pool, please check your sub-account or pool configurations! Sub-account: ", manager.subAccount, ", pools: ", manager.config.Pools)
manager.parent.SendEvent(EventStopUpSessionManager{manager.subAccount})
return
}
}
func (manager *UpSessionManager) upSessionBroken(e EventUpSessionBroken) {
defer manager.tryPrintMinerNum()
info := &manager.upSessions[e.Slot]
info.ready = false
info.minerNum = 0
go manager.connect(e.Slot)
}
func (manager *UpSessionManager) updateMinerNum(e EventUpdateMinerNum) {
defer manager.tryPrintMinerNum()
manager.upSessions[e.Slot].minerNum -= e.DisconnectedMinerCounter
if glog.V(3) {
glog.Info(manager.id, "miner num update, slot: ", e.Slot, ", miners: ", manager.upSessions[e.Slot].minerNum)
}
if manager.config.MultiUserMode {
minerNum := 0
for i := range manager.upSessions {
minerNum += manager.upSessions[i].minerNum
}
if minerNum < 1 {
glog.Info(manager.id, "no miners on sub-account ", manager.subAccount, ", close pool connections")
manager.parent.SendEvent(EventStopUpSessionManager{manager.subAccount})
}
}
}
func (manager *UpSessionManager) updateFakeMinerNum(e EventUpdateFakeMinerNum) {
defer manager.tryPrintMinerNum()
manager.fakeUpSession.minerNum -= e.DisconnectedMinerCounter
}
func (manager *UpSessionManager) updateFakeJob(e interface{}) {
manager.fakeUpSession.upSession.SendEvent(e)
}
func (manager *UpSessionManager) exit() {
manager.fakeUpSession.upSession.SendEvent(EventExit{})
for _, up := range manager.upSessions {
if up.ready {
up.upSession.SendEvent(EventExit{})
}
}
}
func (manager *UpSessionManager) tryPrintMinerNum() {
if manager.printingMinerNum {
return
}
manager.printingMinerNum = true
go func() {
time.Sleep(5 * time.Second)
manager.SendEvent(EventPrintMinerNum{})
}()
}
func (manager *UpSessionManager) printMinerNum() {
pools := 0
miners := manager.fakeUpSession.minerNum
for _, info := range manager.upSessions {
miners += info.minerNum
if info.ready {
pools++
}
}
glog.Info(manager.id, "connection number changed, pool servers: ", pools, ", miners: ", miners)
manager.printingMinerNum = false
}
func (manager *UpSessionManager) handleEvent() {
for {
event := <-manager.eventChannel
switch e := event.(type) {
case EventUpSessionReady:
manager.upSessionReady(e)
case EventUpSessionInitFailed:
manager.upSessionInitFailed(e)
case EventAddDownSession:
manager.addDownSession(e)
case EventUpSessionBroken:
manager.upSessionBroken(e)
case EventUpdateMinerNum:
manager.updateMinerNum(e)
case EventUpdateFakeMinerNum:
manager.updateFakeMinerNum(e)
case EventUpdateFakeJobBTC:
manager.updateFakeJob(e)
case EventUpdateFakeJobETH:
manager.updateFakeJob(e)
case EventPrintMinerNum:
manager.printMinerNum()
case EventExit:
manager.exit()
return
default:
glog.Error("[UpSessionManager] unknown event: ", event)
}
}
}