@@ -37,7 +37,7 @@ type hashrate struct {
37
37
type RemoteAgent struct {
38
38
mu sync.Mutex
39
39
40
- quit chan struct {}
40
+ quitCh chan struct {}
41
41
workCh chan * Work
42
42
returnCh chan <- * Result
43
43
@@ -76,18 +76,16 @@ func (a *RemoteAgent) Start() {
76
76
if ! atomic .CompareAndSwapInt32 (& a .running , 0 , 1 ) {
77
77
return
78
78
}
79
-
80
- a .quit = make (chan struct {})
79
+ a .quitCh = make (chan struct {})
81
80
a .workCh = make (chan * Work , 1 )
82
- go a .maintainLoop ( )
81
+ go a .loop ( a . workCh , a . quitCh )
83
82
}
84
83
85
84
func (a * RemoteAgent ) Stop () {
86
85
if ! atomic .CompareAndSwapInt32 (& a .running , 1 , 0 ) {
87
86
return
88
87
}
89
-
90
- close (a .quit )
88
+ close (a .quitCh )
91
89
close (a .workCh )
92
90
}
93
91
@@ -148,15 +146,20 @@ func (a *RemoteAgent) SubmitWork(nonce uint64, mixDigest, hash common.Hash) bool
148
146
return false
149
147
}
150
148
151
- func (a * RemoteAgent ) maintainLoop () {
149
+ // loop monitors mining events on the work and quit channels, updating the internal
150
+ // state of the rmeote miner until a termination is requested.
151
+ //
152
+ // Note, the reason the work and quit channels are passed as parameters is because
153
+ // RemoteAgent.Start() constantly recreates these channels, so the loop code cannot
154
+ // assume data stability in these member fields.
155
+ func (a * RemoteAgent ) loop (workCh chan * Work , quitCh chan struct {}) {
152
156
ticker := time .Tick (5 * time .Second )
153
157
154
- out:
155
158
for {
156
159
select {
157
- case <- a . quit :
158
- break out
159
- case work := <- a . workCh :
160
+ case <- quitCh :
161
+ return
162
+ case work := <- workCh :
160
163
a .mu .Lock ()
161
164
a .currentWork = work
162
165
a .mu .Unlock ()
0 commit comments