@cryptotaxi247 / kubo / commits / c3280ce88

use new methods from goprocess/context, remove thirdparty/waitable

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Sep 8, 2015 at 21:15 UTC c3280ce88559d0ba1859fadd28ca70d04ef20d0c
10 files changed +79 -30
Godeps/Godeps.json
+1 -1
@@ -199,7 +199,7 @@
199 },
200 {
201 "ImportPath": "github.com/jbenet/goprocess",
202 - "Rev": "4562d0c5780b8f060df2b84a8945bb8678bfc023"
202 + "Rev": "64a8220330a485070813201cc05b0c6777f6a516"
203 },
204 {
205 "ImportPath": "github.com/kardianos/osext",
Godeps/_workspace/src/github.com/jbenet/goprocess/context/derive.go new
+59
@@ -0,0 +1,59 @@
1 +package goprocessctx
2 +
3 +import (
4 + "errors"
5 + "time"
6 +
7 + goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
8 + "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9 +)
10 +
11 +const (
12 + closing = iota
13 + closed
14 +)
15 +
16 +type procContext struct {
17 + done <-chan struct{}
18 + which int
19 +}
20 +
21 +// OnClosingContext derives a context from a given goprocess that will
22 +// be 'Done' when the process is closing
23 +func OnClosingContext(p goprocess.Process) context.Context {
24 + return &procContext{
25 + done: p.Closing(),
26 + which: closing,
27 + }
28 +}
29 +
30 +// OnClosedContext derives a context from a given goprocess that will
31 +// be 'Done' when the process is closed
32 +func OnClosedContext(p goprocess.Process) context.Context {
33 + return &procContext{
34 + done: p.Closed(),
35 + which: closed,
36 + }
37 +}
38 +
39 +func (c *procContext) Done() <-chan struct{} {
40 + return c.done
41 +}
42 +
43 +func (c *procContext) Deadline() (time.Time, bool) {
44 + return time.Time{}, false
45 +}
46 +
47 +func (c *procContext) Err() error {
48 + if c.which == closing {
49 + return errors.New("process closing")
50 + } else if c.which == closed {
51 + return errors.New("process closed")
52 + } else {
53 + panic("unrecognized process context type")
54 + }
55 +}
56 +
57 +func (c *procContext) Value(key interface{}) interface{} {
58 + return nil
59 +}
Godeps/_workspace/src/github.com/jbenet/goprocess/goprocess.go
+4
@@ -139,6 +139,10 @@ type Process interface {
139 // _after_ Close has completed; teardown has finished. The primary use case
140 // of Closed is waiting for a Process to Close without _causing_ the Close.
141 Closed() <-chan struct{}
142 +
143 + // Err waits until the process is closed, and then returns any error that
144 + // occurred during shutdown.
145 + Err() error
146 }
147
148 // TeardownFunc is a function used to cleanup state at the end of the
Godeps/_workspace/src/github.com/jbenet/goprocess/impl-mutex.go
+5
@@ -163,6 +163,11 @@ func (p *process) Closed() <-chan struct{} {
163 return p.closed
164 }
165
166 +func (p *process) Err() error {
167 + <-p.Closed()
168 + return p.closeErr
169 +}
170 +
171 // the _actual_ close process.
172 func (p *process) doClose() {
173 // this function is only be called once (protected by p.Lock()).
blockservice/worker/worker.go
+2 -2
@@ -7,11 +7,11 @@ import (
7 "time"
8
9 process "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
10 + procctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
11 ratelimit "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/ratelimit"
12 blocks "github.com/ipfs/go-ipfs/blocks"
13 key "github.com/ipfs/go-ipfs/blocks/key"
14 exchange "github.com/ipfs/go-ipfs/exchange"
14 - waitable "github.com/ipfs/go-ipfs/thirdparty/waitable"
15 util "github.com/ipfs/go-ipfs/util"
16 )
17
@@ -121,7 +121,7 @@ func (w *Worker) start(c Config) {
121 // reads from |workerChan| until w.process closes
122 limiter := ratelimit.NewRateLimiter(w.process, c.NumWorkers)
123 limiter.Go(func(proc process.Process) {
124 - ctx := waitable.Context(proc) // shut down in-progress HasBlock when time to die
124 + ctx := procctx.OnClosingContext(proc) // shut down in-progress HasBlock when time to die
125 for {
126 select {
127 case <-proc.Closing():
core/bootstrap.go
+2 -2
@@ -79,7 +79,7 @@ func Bootstrap(n *IpfsNode, cfg BootstrapConfig) (io.Closer, error) {
79
80 // the periodic bootstrap function -- the connection supervisor
81 periodic := func(worker goprocess.Process) {
82 - ctx := procctx.WithProcessClosing(context.Background(), worker)
82 + ctx := procctx.OnClosingContext(worker)
83 defer log.EventBegin(ctx, "periodicBootstrap", n.Identity).Done()
84
85 if err := bootstrapRound(ctx, n.PeerHost, cfg); err != nil {
@@ -96,7 +96,7 @@ func Bootstrap(n *IpfsNode, cfg BootstrapConfig) (io.Closer, error) {
96
97 // kick off Routing.Bootstrap
98 if n.Routing != nil {
99 - ctx := procctx.WithProcessClosing(context.Background(), proc)
99 + ctx := procctx.OnClosingContext(proc)
100 if err := n.Routing.Bootstrap(ctx); err != nil {
101 proc.Close()
102 return nil, err
exchange/bitswap/workers.go
+3 -3
@@ -4,9 +4,9 @@ import (
4 "time"
5
6 process "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
7 + procctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
8 ratelimit "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/ratelimit"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9 - waitable "github.com/ipfs/go-ipfs/thirdparty/waitable"
10
11 key "github.com/ipfs/go-ipfs/blocks/key"
12 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
@@ -80,7 +80,7 @@ func (bs *Bitswap) provideWorker(px process.Process) {
80 ev := eventlog.LoggableMap{"ID": wid}
81 limiter.LimitedGo(func(px process.Process) {
82
83 - ctx := waitable.Context(px) // derive ctx from px
83 + ctx := procctx.OnClosingContext(px) // derive ctx from px
84 defer log.EventBegin(ctx, "Bitswap.ProvideWorker.Work", ev, &k).Done()
85
86 ctx, cancel := context.WithTimeout(ctx, provideTimeout) // timeout ctx
@@ -97,7 +97,7 @@ func (bs *Bitswap) provideWorker(px process.Process) {
97 limiter.Go(func(px process.Process) {
98 for wid := 2; ; wid++ {
99 ev := eventlog.LoggableMap{"ID": 1}
100 - log.Event(waitable.Context(px), "Bitswap.ProvideWorker.Loop", ev)
100 + log.Event(procctx.OnClosingContext(px), "Bitswap.ProvideWorker.Loop", ev)
101
102 select {
103 case <-px.Closing():
routing/dht/ext_test.go
+1 -1
@@ -51,7 +51,7 @@ func TestGetFailures(t *testing.T) {
51 err = merr[0]
52 }
53
54 - if err != context.DeadlineExceeded && err != context.Canceled {
54 + if err.Error() != "process closing" {
55 t.Fatal("Got different error than we expected", err)
56 }
57 } else {
routing/dht/query.go
+2 -2
@@ -85,7 +85,7 @@ type dhtQueryRunner struct {
85
86 func newQueryRunner(q *dhtQuery) *dhtQueryRunner {
87 proc := process.WithParent(process.Background())
88 - ctx := ctxproc.WithProcessClosing(context.Background(), proc)
88 + ctx := ctxproc.OnClosingContext(proc)
89 return &dhtQueryRunner{
90 query: q,
91 peersToQuery: queue.NewChanQueue(ctx, queue.NewXORDistancePQ(q.key)),
@@ -210,7 +210,7 @@ func (r *dhtQueryRunner) queryPeer(proc process.Process, p peer.ID) {
210 // ok let's do this!
211
212 // create a context from our proc.
213 - ctx := ctxproc.WithProcessClosing(context.Background(), proc)
213 + ctx := ctxproc.OnClosingContext(proc)
214
215 // make sure we do this when we exit
216 defer func() {
thirdparty/waitable/waitable.go deleted
-19
@@ -1,19 +0,0 @@
1 -package waitable
2 -
3 -import (
4 - context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/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 -}