@cryptotaxi247 / kubo / commits / 809e58f0d

refac(blockservice) extract waitable

Brian Tiger Chow committed Jan 30, 2015 at 00:08 UTC 809e58f0dd22d22f68fa8a1531d53291b155dc4b
2 files changed +21 -17
blockservice/worker/worker.go
+2 -17
@@ -6,11 +6,11 @@ import (
6 "errors"
7 "time"
8
9 - context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9 process "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
10 ratelimit "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/ratelimit"
11 blocks "github.com/jbenet/go-ipfs/blocks"
12 exchange "github.com/jbenet/go-ipfs/exchange"
13 + waitable "github.com/jbenet/go-ipfs/thirdparty/waitable"
14 util "github.com/jbenet/go-ipfs/util"
15 )
16
@@ -119,7 +119,7 @@ func (w *Worker) start(c Config) {
119
120 // reads from |workerChan| until process closes
121 w.process.Go(func(proc process.Process) {
122 - ctx := childContext(proc) // shut down in-progress HasBlock when time to die
122 + ctx := waitable.Context(proc) // shut down in-progress HasBlock when time to die
123 limiter := ratelimit.NewRateLimiter(process.Background(), c.NumWorkers)
124 defer limiter.Close()
125 for {
@@ -181,18 +181,3 @@ func (s *BlockList) Pop() *blocks.Block {
181 func (s *BlockList) Len() int {
182 return s.list.Len()
183 }
184 -
185 -// TODO extract
186 -type waitable interface {
187 - Closing() <-chan struct{}
188 -}
189 -
190 -// TODO extract
191 -func childContext(w waitable) context.Context {
192 - ctx, cancel := context.WithCancel(context.Background())
193 - go func() {
194 - <-w.Closing()
195 - cancel()
196 - }()
197 - return ctx
198 -}
thirdparty/waitable/waitable.go new
+19
@@ -0,0 +1,19 @@
1 +package waitable
2 +
3 +import (
4 + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5 +)
6 +
7 +type Waitable interface {
8 + Closing() <-chan struct{}
9 +}
10 +
11 +// Context returns a context that cancels when the waitable is closing.
12 +func Context(w Waitable) context.Context {
13 + ctx, cancel := context.WithCancel(context.Background())
14 + go func() {
15 + <-w.Closing()
16 + cancel()
17 + }()
18 + return ctx
19 +}