extract context func
@jbenet would like it to work this way
Brian Tiger Chow committed
Jan 20, 2015 at 23:22 UTC
e3c9f6db50828d414d3609b9c35c8cc11dd550e5
1 file changed
+16
-8
blockservice/worker/worker.go
+16
-8
@@ -117,14 +117,7 @@ func (w *Worker) start(c Config) {
117
118
// reads from |workerChan| until process closes
119
w.process.Go(func(proc process.Process) {
120
- ctx, cancel := context.WithCancel(context.Background())
121
-
122
- // shuts down an in-progress HasBlock operation
123
- proc.Go(func(proc process.Process) {
124
- <-proc.Closing()
125
- cancel()
126
- })
127
-
120
+ ctx := childContext(proc) // shut down in-progress HasBlock when time to die
121
limiter := ratelimit.NewRateLimiter(proc, c.NumWorkers)
122
for {
123
select {
@@ -169,3 +162,18 @@ func (s *BlockList) Pop() *blocks.Block {
162
func (s *BlockList) Len() int {
163
return s.list.Len()
164
}
165
+
166
+// TODO extract
167
+type waitable interface {
168
+ Closing() <-chan struct{}
169
+}
170
+
171
+// TODO extract
172
+func childContext(w waitable) context.Context {
173
+ ctx, cancel := context.WithCancel(context.Background())
174
+ go func() {
175
+ <-w.Closing()
176
+ cancel()
177
+ }()
178
+ return ctx
179
+}