Replace ctxgroup.ContextGroup -> goprocess.Process
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Jun 17, 2015 at 19:18 UTC
330b2137770b04556b6cba21fa21c576986aa370
19 files changed
+72
-588
Godeps/Godeps.json
-4
@@ -137,10 +137,6 @@
137
"ImportPath": "github.com/jbenet/go-base58",
138
"Rev": "6237cf65f3a6f7111cd8a42be3590df99a66bc7d"
139
},
140
- {
141
- "ImportPath": "github.com/jbenet/go-ctxgroup",
142
- "Rev": "c14598396fa31465dc558b176c7976606f95a49d"
143
- },
140
{
141
"ImportPath": "github.com/jbenet/go-datastore",
142
"Rev": "245a981af3750d7710db13dca731ba8461aa1095"
Godeps/_workspace/src/github.com/jbenet/go-ctxgroup/Godeps/Godeps.json
deleted
-13
@@ -1,13 +0,0 @@
1
-{
2
- "ImportPath": "github.com/jbenet/go-ctxgroup",
3
- "GoVersion": "go1.4.2",
4
- "Packages": [
5
- "./..."
6
- ],
7
- "Deps": [
8
- {
9
- "ImportPath": "golang.org/x/net/context",
10
- "Rev": "b6fdb7d8a4ccefede406f8fe0f017fb58265054c"
11
- }
12
- ]
13
-}
Godeps/_workspace/src/github.com/jbenet/go-ctxgroup/Godeps/Readme
deleted
-5
@@ -1,5 +0,0 @@
1
-This directory tree is generated automatically by godep.
2
-
3
-Please do not edit.
4
-
5
-See https://github.com/tools/godep for more information.
Godeps/_workspace/src/github.com/jbenet/go-ctxgroup/Makefile
deleted
-16
@@ -1,16 +0,0 @@
1
-all:
2
- # no-op
3
-
4
-GODEP=$(which godep)
5
-
6
-godep: ${GODEP}
7
-
8
-${GODEP}:
9
- echo ${GODEP}
10
- go get github.com/tools/godep
11
-
12
-# saves/vendors third-party dependencies to Godeps/_workspace
13
-# -r flag rewrites import paths to use the vendored path
14
-# ./... performs operation on all packages in tree
15
-vendor: godep
16
- godep save -r ./...
Godeps/_workspace/src/github.com/jbenet/go-ctxgroup/README.md
deleted
-35
@@ -1,35 +0,0 @@
1
-# ContextGroup
2
-
3
-
4
-- Godoc: https://godoc.org/github.com/jbenet/go-ctxgroup
5
-
6
-ContextGroup is an interface for services able to be opened and closed.
7
-It has a parent Context, and Children. But ContextGroup is not a proper
8
-"tree" like the Context tree. It is more like a Context-WaitGroup hybrid.
9
-It models a main object with a few children objects -- and, unlike the
10
-context -- concerns itself with the parent-child closing semantics:
11
-
12
-- Can define an optional TeardownFunc (func() error) to be run at Closetime.
13
-- Children call Children().Add(1) to be waited upon
14
-- Children can select on <-Closing() to know when they should shut down.
15
-- Close() will wait until all children call Children().Done()
16
-- <-Closed() signals when the service is completely closed.
17
-
18
-ContextGroup can be embedded into the main object itself. In that case,
19
-the teardownFunc (if a member function) has to be set after the struct
20
-is intialized:
21
-
22
-```Go
23
-type service struct {
24
- ContextGroup
25
- net.Conn
26
-}
27
-func (s *service) close() error {
28
- return s.Conn.Close()
29
-}
30
-func newService(ctx context.Context, c net.Conn) *service {
31
- s := &service{c}
32
- s.ContextGroup = NewContextGroup(ctx, s.close)
33
- return s
34
-}
35
-```
Godeps/_workspace/src/github.com/jbenet/go-ctxgroup/ctxgroup.go
deleted
-257
@@ -1,257 +0,0 @@
1
-// package ctxgroup provides the ContextGroup, a hybrid between the
2
-// context.Context and sync.WaitGroup, which models process trees.
3
-package ctxgroup
4
-
5
-import (
6
- "io"
7
- "sync"
8
-
9
- context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10
-)
11
-
12
-// TeardownFunc is a function used to cleanup state at the end of the
13
-// lifecycle of a process.
14
-type TeardownFunc func() error
15
-
16
-// ChildFunc is a function to register as a child. It will be automatically
17
-// tracked.
18
-type ChildFunc func(parent ContextGroup)
19
-
20
-var nilTeardownFunc = func() error { return nil }
21
-
22
-// ContextGroup is an interface for services able to be opened and closed.
23
-// It has a parent Context, and Children. But ContextGroup is not a proper
24
-// "tree" like the Context tree. It is more like a Context-WaitGroup hybrid.
25
-// It models a main object with a few children objects -- and, unlike the
26
-// context -- concerns itself with the parent-child closing semantics:
27
-//
28
-// - Can define an optional TeardownFunc (func() error) to be run at Close time.
29
-// - Children call Children().Add(1) to be waited upon
30
-// - Children can select on <-Closing() to know when they should shut down.
31
-// - Close() will wait until all children call Children().Done()
32
-// - <-Closed() signals when the service is completely closed.
33
-//
34
-// ContextGroup can be embedded into the main object itself. In that case,
35
-// the teardownFunc (if a member function) has to be set after the struct
36
-// is intialized:
37
-//
38
-// type service struct {
39
-// ContextGroup
40
-// net.Conn
41
-// }
42
-//
43
-// func (s *service) close() error {
44
-// return s.Conn.Close()
45
-// }
46
-//
47
-// func newService(ctx context.Context, c net.Conn) *service {
48
-// s := &service{c}
49
-// s.ContextGroup = NewContextGroup(ctx, s.close)
50
-// return s
51
-// }
52
-//
53
-type ContextGroup interface {
54
-
55
- // Context is the context of this ContextGroup. It is "sort of" a parent.
56
- Context() context.Context
57
-
58
- // SetTeardown assigns the teardown function.
59
- // It is called exactly _once_ when the ContextGroup is Closed.
60
- SetTeardown(tf TeardownFunc)
61
-
62
- // Children is a sync.Waitgroup for all children goroutines that should
63
- // shut down completely before this service is said to be "closed".
64
- // Follows the semantics of WaitGroup:
65
- //
66
- // Children().Add(1) // add one more dependent child
67
- // Children().Done() // child signals it is done
68
- //
69
- // WARNING: this is deprecated and will go away soon.
70
- Children() *sync.WaitGroup
71
-
72
- // AddChild gives ownership of a child io.Closer. The child will be closed
73
- // when the context group is closed.
74
- AddChild(io.Closer)
75
-
76
- // AddChildFunc registers a dependent ChildFund. The child will receive
77
- // its parent ContextGroup, and can wait on its signals. Child references
78
- // tracked automatically. It equivalent to the following:
79
- //
80
- // go func(parent, child ContextGroup) {
81
- //
82
- // <-parent.Closing() // wait until parent is closing
83
- // child.Close() // signal child to close
84
- // parent.Children().Done() // child signals it is done
85
- // }(a, b)
86
- //
87
- AddChildFunc(c ChildFunc)
88
-
89
- // Close is a method to call when you wish to stop this ContextGroup
90
- Close() error
91
-
92
- // Closing is a signal to wait upon, like Context.Done().
93
- // It fires when the object should be closing (but hasn't yet fully closed).
94
- // The primary use case is for child goroutines who need to know when
95
- // they should shut down. (equivalent to Context().Done())
96
- Closing() <-chan struct{}
97
-
98
- // Closed is a method to wait upon, like Context.Done().
99
- // It fires when the entire object is fully closed.
100
- // The primary use case is for external listeners who need to know when
101
- // this object is completly done, and all its children closed.
102
- Closed() <-chan struct{}
103
-}
104
-
105
-// contextGroup is a Closer with a cancellable context
106
-type contextGroup struct {
107
- ctx context.Context
108
- cancel context.CancelFunc
109
-
110
- // called to run the teardown logic.
111
- teardownFunc TeardownFunc
112
-
113
- // closed is released once the close function is done.
114
- closed chan struct{}
115
-
116
- // wait group for child goroutines
117
- children sync.WaitGroup
118
-
119
- // sync primitive to ensure the close logic is only called once.
120
- closeOnce sync.Once
121
-
122
- // error to return to clients of Close().
123
- closeErr error
124
-}
125
-
126
-// newContextGroup constructs and returns a ContextGroup. It will call
127
-// cf TeardownFunc before its Done() Wait signals fire.
128
-func newContextGroup(ctx context.Context, cf TeardownFunc) ContextGroup {
129
- ctx, cancel := context.WithCancel(ctx)
130
- c := &contextGroup{
131
- ctx: ctx,
132
- cancel: cancel,
133
- closed: make(chan struct{}),
134
- }
135
- c.SetTeardown(cf)
136
-
137
- c.Children().Add(1) // initialize with 1. calling Close will decrement it.
138
- go c.closeOnContextDone()
139
- return c
140
-}
141
-
142
-// SetTeardown assigns the teardown function.
143
-func (c *contextGroup) SetTeardown(cf TeardownFunc) {
144
- if cf == nil {
145
- cf = nilTeardownFunc
146
- }
147
- c.teardownFunc = cf
148
-}
149
-
150
-func (c *contextGroup) Context() context.Context {
151
- return c.ctx
152
-}
153
-
154
-func (c *contextGroup) Children() *sync.WaitGroup {
155
- return &c.children
156
-}
157
-
158
-func (c *contextGroup) AddChild(child io.Closer) {
159
- c.children.Add(1)
160
- go func(parent ContextGroup, child io.Closer) {
161
- <-parent.Closing() // wait until parent is closing
162
- child.Close() // signal child to close
163
- parent.Children().Done() // child signals it is done
164
- }(c, child)
165
-}
166
-
167
-func (c *contextGroup) AddChildFunc(child ChildFunc) {
168
- c.children.Add(1)
169
- go func(parent ContextGroup, child ChildFunc) {
170
- child(parent)
171
- parent.Children().Done() // child signals it is done
172
- }(c, child)
173
-}
174
-
175
-// Close is the external close function. it's a wrapper around internalClose
176
-// that waits on Closed()
177
-func (c *contextGroup) Close() error {
178
- c.internalClose()
179
- <-c.Closed() // wait until we're totally done.
180
- return c.closeErr
181
-}
182
-
183
-func (c *contextGroup) Closing() <-chan struct{} {
184
- return c.Context().Done()
185
-}
186
-
187
-func (c *contextGroup) Closed() <-chan struct{} {
188
- return c.closed
189
-}
190
-
191
-func (c *contextGroup) internalClose() {
192
- go c.closeOnce.Do(c.closeLogic)
193
-}
194
-
195
-// the _actual_ close process.
196
-func (c *contextGroup) closeLogic() {
197
- // this function should only be called once (hence the sync.Once).
198
- // and it will panic at the bottom (on close(c.closed)) otherwise.
199
-
200
- c.cancel() // signal that we're shutting down (Closing)
201
- c.closeErr = c.teardownFunc() // actually run the close logic
202
- c.children.Wait() // wait till all children are done.
203
- close(c.closed) // signal that we're shut down (Closed)
204
-}
205
-
206
-// if parent context is shut down before we call Close explicitly,
207
-// we need to go through the Close motions anyway. Hence all the sync
208
-// stuff all over the place...
209
-func (c *contextGroup) closeOnContextDone() {
210
- <-c.Context().Done() // wait until parent (context) is done.
211
- c.internalClose()
212
- c.Children().Done()
213
-}
214
-
215
-// WithTeardown constructs and returns a ContextGroup with
216
-// cf TeardownFunc (and context.Background)
217
-func WithTeardown(cf TeardownFunc) ContextGroup {
218
- if cf == nil {
219
- panic("nil TeardownFunc")
220
- }
221
- return newContextGroup(context.Background(), cf)
222
-}
223
-
224
-// WithContext constructs and returns a ContextGroup with given context
225
-func WithContext(ctx context.Context) ContextGroup {
226
- if ctx == nil {
227
- panic("nil Context")
228
- }
229
- return newContextGroup(ctx, nil)
230
-}
231
-
232
-// WithContextAndTeardown constructs and returns a ContextGroup with
233
-// cf TeardownFunc (and context.Background)
234
-func WithContextAndTeardown(ctx context.Context, cf TeardownFunc) ContextGroup {
235
- if ctx == nil {
236
- panic("nil Context")
237
- }
238
- if cf == nil {
239
- panic("nil TeardownFunc")
240
- }
241
- return newContextGroup(ctx, cf)
242
-}
243
-
244
-// WithParent constructs and returns a ContextGroup with given parent
245
-func WithParent(p ContextGroup) ContextGroup {
246
- if p == nil {
247
- panic("nil ContextGroup")
248
- }
249
- c := newContextGroup(p.Context(), nil)
250
- p.AddChild(c)
251
- return c
252
-}
253
-
254
-// WithBackground returns a ContextGroup with context.Background()
255
-func WithBackground() ContextGroup {
256
- return newContextGroup(context.Background(), nil)
257
-}
Godeps/_workspace/src/github.com/jbenet/go-ctxgroup/ctxgroup_test.go
deleted
-187
@@ -1,187 +0,0 @@
1
-package ctxgroup
2
-
3
-import (
4
- "testing"
5
- "time"
6
-
7
- context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8
-)
9
-
10
-type tree struct {
11
- ContextGroup
12
- c []tree
13
-}
14
-
15
-func setupCGHierarchy(ctx context.Context) tree {
16
- t := func(n ContextGroup, ts ...tree) tree {
17
- return tree{n, ts}
18
- }
19
-
20
- if ctx == nil {
21
- ctx = context.Background()
22
- }
23
- a := WithContext(ctx)
24
- b1 := WithParent(a)
25
- b2 := WithParent(a)
26
- c1 := WithParent(b1)
27
- c2 := WithParent(b1)
28
- c3 := WithParent(b2)
29
- c4 := WithParent(b2)
30
-
31
- return t(a, t(b1, t(c1), t(c2)), t(b2, t(c3), t(c4)))
32
-}
33
-
34
-func TestClosingClosed(t *testing.T) {
35
-
36
- a := WithBackground()
37
- Q := make(chan string)
38
-
39
- go func() {
40
- <-a.Closing()
41
- Q <- "closing"
42
- }()
43
-
44
- go func() {
45
- <-a.Closed()
46
- Q <- "closed"
47
- }()
48
-
49
- go func() {
50
- a.Close()
51
- Q <- "closed"
52
- }()
53
-
54
- if q := <-Q; q != "closing" {
55
- t.Error("order incorrect. closing not first")
56
- }
57
- if q := <-Q; q != "closed" {
58
- t.Error("order incorrect. closing not first")
59
- }
60
- if q := <-Q; q != "closed" {
61
- t.Error("order incorrect. closing not first")
62
- }
63
-}
64
-
65
-func TestChildFunc(t *testing.T) {
66
- a := WithBackground()
67
-
68
- wait1 := make(chan struct{})
69
- wait2 := make(chan struct{})
70
- wait3 := make(chan struct{})
71
- wait4 := make(chan struct{})
72
- go func() {
73
- a.Close()
74
- wait4 <- struct{}{}
75
- }()
76
-
77
- a.AddChildFunc(func(parent ContextGroup) {
78
- wait1 <- struct{}{}
79
- <-wait2
80
- wait3 <- struct{}{}
81
- })
82
-
83
- <-wait1
84
- select {
85
- case <-wait3:
86
- t.Error("should not be closed yet")
87
- case <-wait4:
88
- t.Error("should not be closed yet")
89
- case <-a.Closed():
90
- t.Error("should not be closed yet")
91
- default:
92
- }
93
-
94
- wait2 <- struct{}{}
95
-
96
- select {
97
- case <-wait3:
98
- case <-time.After(time.Second):
99
- t.Error("should be closed now")
100
- }
101
-
102
- select {
103
- case <-wait4:
104
- case <-time.After(time.Second):
105
- t.Error("should be closed now")
106
- }
107
-}
108
-
109
-func TestTeardownCalledOnce(t *testing.T) {
110
- a := setupCGHierarchy(nil)
111
-
112
- onlyOnce := func() func() error {
113
- count := 0
114
- return func() error {
115
- count++
116
- if count > 1 {
117
- t.Error("called", count, "times")
118
- }
119
- return nil
120
- }
121
- }
122
-
123
- a.SetTeardown(onlyOnce())
124
- a.c[0].SetTeardown(onlyOnce())
125
- a.c[0].c[0].SetTeardown(onlyOnce())
126
- a.c[0].c[1].SetTeardown(onlyOnce())
127
- a.c[1].SetTeardown(onlyOnce())
128
- a.c[1].c[0].SetTeardown(onlyOnce())
129
- a.c[1].c[1].SetTeardown(onlyOnce())
130
-
131
- a.c[0].c[0].Close()
132
- a.c[0].c[0].Close()
133
- a.c[0].c[0].Close()
134
- a.c[0].c[0].Close()
135
- a.c[0].Close()
136
- a.c[0].Close()
137
- a.c[0].Close()
138
- a.c[0].Close()
139
- a.Close()
140
- a.Close()
141
- a.Close()
142
- a.Close()
143
- a.c[1].Close()
144
- a.c[1].Close()
145
- a.c[1].Close()
146
- a.c[1].Close()
147
-}
148
-
149
-func TestOnClosed(t *testing.T) {
150
-
151
- ctx, cancel := context.WithCancel(context.Background())
152
- a := setupCGHierarchy(ctx)
153
- Q := make(chan string, 10)
154
-
155
- onClosed := func(s string, c ContextGroup) {
156
- <-c.Closed()
157
- Q <- s
158
- }
159
-
160
- go onClosed("0", a.c[0])
161
- go onClosed("10", a.c[1].c[0])
162
- go onClosed("", a)
163
- go onClosed("00", a.c[0].c[0])
164
- go onClosed("1", a.c[1])
165
- go onClosed("01", a.c[0].c[1])
166
- go onClosed("11", a.c[1].c[1])
167
-
168
- test := func(ss ...string) {
169
- s1 := <-Q
170
- for _, s2 := range ss {
171
- if s1 == s2 {
172
- return
173
- }
174
- }
175
- t.Error("context not in group", s1, ss)
176
- }
177
-
178
- cancel()
179
-
180
- test("00", "01", "10", "11")
181
- test("00", "01", "10", "11")
182
- test("00", "01", "10", "11")
183
- test("00", "01", "10", "11")
184
- test("0", "1")
185
- test("0", "1")
186
- test("")
187
-}
core/core.go
+7
-6
@@ -17,9 +17,10 @@ import (
17
"time"
18
19
b58 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
20
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
20
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
21
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
22
+ goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
23
+ goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
24
mamask "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/multiaddr-filter"
25
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
26
diag "github.com/ipfs/go-ipfs/diagnostics"
@@ -105,7 +106,7 @@ type IpfsNode struct {
106
107
IpnsFs *ipnsfs.Filesystem
108
108
- ctxgroup.ContextGroup
109
+ goprocess.Process
110
111
mode mode
112
}
@@ -121,12 +122,12 @@ type Mounts struct {
122
type ConfigOption func(ctx context.Context) (*IpfsNode, error)
123
124
func NewIPFSNode(parent context.Context, option ConfigOption) (*IpfsNode, error) {
124
- ctxg := ctxgroup.WithContext(parent)
125
- ctx := ctxg.Context()
125
+ procctx := goprocessctx.WithContext(parent)
126
+ ctx := parent
127
success := false // flip to true after all sub-system inits succeed
128
defer func() {
129
if !success {
129
- ctxg.Close()
130
+ procctx.Close()
131
}
132
}()
133
@@ -134,7 +135,7 @@ func NewIPFSNode(parent context.Context, option ConfigOption) (*IpfsNode, error)
135
if err != nil {
136
return nil, err
137
}
137
- node.ContextGroup = ctxg
138
+ node.Process = procctx
139
ctxg.SetTeardown(node.teardown)
140
141
// Need to make sure it's perfectly clear 1) which variables are expected
core/mock/mock.go
+2
-2
@@ -1,9 +1,9 @@
1
package coremock
2
3
import (
4
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
4
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
5
syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
6
+ goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
7
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8
"github.com/ipfs/go-ipfs/blocks/blockstore"
9
blockservice "github.com/ipfs/go-ipfs/blockservice"
@@ -42,7 +42,7 @@ func NewMockNode() (*core.IpfsNode, error) {
42
nd.Peerstore = peer.NewPeerstore()
43
nd.Peerstore.AddPrivKey(p, ident.PrivateKey())
44
nd.Peerstore.AddPubKey(p, ident.PublicKey())
45
- nd.ContextGroup = ctxgroup.WithContext(ctx)
45
+ nd.Process = goprocessctx.WithContext(ctx)
46
47
nd.PeerHost, err = mocknet.New(ctx).AddPeer(ident.PrivateKey(), ident.Address()) // effectively offline
48
if err != nil {
fuse/mount/fuse.go
+9
-9
@@ -8,7 +8,7 @@ import (
8
9
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/bazil.org/fuse"
10
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs"
11
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
11
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
12
)
13
14
// mount implements go-ipfs/fuse/mount
@@ -18,12 +18,12 @@ type mount struct {
18
fuseConn *fuse.Conn
19
// closeErr error
20
21
- cg ctxgroup.ContextGroup
21
+ proc goprocess.Process
22
}
23
24
// Mount mounts a fuse fs.FS at a given location, and returns a Mount instance.
25
// parent is a ContextGroup to bind the mount's ContextGroup to.
26
-func NewMount(p ctxgroup.ContextGroup, fsys fs.FS, mountpoint string, allow_other bool) (Mount, error) {
26
+func NewMount(p goprocess.Process, fsys fs.FS, mountpoint string, allow_other bool) (Mount, error) {
27
var conn *fuse.Conn
28
var err error
29
@@ -41,9 +41,9 @@ func NewMount(p ctxgroup.ContextGroup, fsys fs.FS, mountpoint string, allow_othe
41
mpoint: mountpoint,
42
fuseConn: conn,
43
filesys: fsys,
44
- cg: ctxgroup.WithParent(p), // link it to parent.
44
+ proc: goprocess.WithParent(p), // link it to parent.
45
}
46
- m.cg.SetTeardown(m.unmount)
46
+ m.proc.SetTeardown(m.unmount)
47
48
// launch the mounting process.
49
if err := m.mount(); err != nil {
@@ -116,8 +116,8 @@ func (m *mount) unmount() error {
116
return nil
117
}
118
119
-func (m *mount) CtxGroup() ctxgroup.ContextGroup {
120
- return m.cg
119
+func (m *mount) Process() goprocess.Process {
120
+ return m.proc
121
}
122
123
func (m *mount) MountPoint() string {
@@ -125,6 +125,6 @@ func (m *mount) MountPoint() string {
125
}
126
127
func (m *mount) Unmount() error {
128
- // call ContextCloser Close(), which calls unmount() exactly once.
129
- return m.cg.Close()
128
+ // call Process Close(), which calls unmount() exactly once.
129
+ return m.proc.Close()
130
}
fuse/mount/mount.go
+3
-3
@@ -7,7 +7,7 @@ import (
7
"runtime"
8
"time"
9
10
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
10
+ goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
11
12
u "github.com/ipfs/go-ipfs/util"
13
)
@@ -24,9 +24,9 @@ type Mount interface {
24
// Unmounts the mount
25
Unmount() error
26
27
- // CtxGroup returns the mount's CtxGroup to be able to link it
27
+ // Process returns the mount's Process to be able to link it
28
// to other processes. Unmount upon closing.
29
- CtxGroup() ctxgroup.ContextGroup
29
+ Process() goprocess.Process
30
}
31
32
// ForceUnmount attempts to forcibly unmount a given mount.
p2p/net/conn/listen.go
+6
-5
@@ -5,11 +5,12 @@ import (
5
"io"
6
"net"
7
8
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
8
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9
manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
10
reuseport "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-reuseport"
11
tec "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-temp-err-catcher"
12
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
13
+ goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
14
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
15
16
ic "github.com/ipfs/go-ipfs/p2p/crypto"
@@ -31,7 +32,7 @@ type listener struct {
32
33
wrapper ConnWrapper
34
34
- cg ctxgroup.ContextGroup
35
+ proc goprocess.Process
36
}
37
38
func (l *listener) teardown() error {
@@ -41,7 +42,7 @@ func (l *listener) teardown() error {
42
43
func (l *listener) Close() error {
44
log.Debugf("listener closing: %s %s", l.local, l.Multiaddr())
44
- return l.cg.Close()
45
+ return l.proc.Close()
46
}
47
48
func (l *listener) String() string {
@@ -157,9 +158,9 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local peer.ID, sk ic.PrivKey
158
Listener: ml,
159
local: local,
160
privk: sk,
160
- cg: ctxgroup.WithContext(ctx),
161
+ proc: goprocessctx.WithContext(ctx),
162
}
162
- l.cg.SetTeardown(l.teardown)
163
+ l.proc.SetTeardown(l.teardown)
164
165
log.Debugf("Conn Listener on %s", l.Multiaddr())
166
log.Event(ctx, "swarmListen", l)
p2p/net/interface.go
+3
-3
@@ -6,8 +6,8 @@ import (
6
conn "github.com/ipfs/go-ipfs/p2p/net/conn"
7
peer "github.com/ipfs/go-ipfs/p2p/peer"
8
9
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
9
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
11
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
12
)
13
@@ -80,8 +80,8 @@ type Network interface {
80
// use the known local interfaces.
81
InterfaceListenAddresses() ([]ma.Multiaddr, error)
82
83
- // CtxGroup returns the network's contextGroup
84
- CtxGroup() ctxgroup.ContextGroup
83
+ // Process returns the network's Process
84
+ Process() goprocess.Process
85
}
86
87
// Dialer represents a service that can dial out to peers
p2p/net/mock/mock_net.go
+4
-3
@@ -13,8 +13,9 @@ import (
13
p2putil "github.com/ipfs/go-ipfs/p2p/test/util"
14
testutil "github.com/ipfs/go-ipfs/util/testutil"
15
16
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
16
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
17
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
18
+ goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
19
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
20
)
21
@@ -31,7 +32,7 @@ type mocknet struct {
32
33
linkDefaults LinkOptions
34
34
- cg ctxgroup.ContextGroup // for Context closing
35
+ proc goprocess.Process // for Context closing
36
sync.RWMutex
37
}
38
@@ -40,7 +41,7 @@ func New(ctx context.Context) Mocknet {
41
nets: map[peer.ID]*peernet{},
42
hosts: map[peer.ID]*bhost.BasicHost{},
43
links: map[peer.ID]map[peer.ID]map[*link]struct{}{},
43
- cg: ctxgroup.WithContext(ctx),
44
+ proc: goprocessctx.WithContext(ctx),
45
}
46
}
47
p2p/net/mock/mock_peernet.go
+7
-6
@@ -9,8 +9,9 @@ import (
9
inet "github.com/ipfs/go-ipfs/p2p/net"
10
peer "github.com/ipfs/go-ipfs/p2p/peer"
11
12
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
12
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
13
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
14
+ goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
15
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
16
)
17
@@ -34,7 +35,7 @@ type peernet struct {
35
notifmu sync.RWMutex
36
notifs map[inet.Notifiee]struct{}
37
37
- cg ctxgroup.ContextGroup
38
+ proc goprocess.Process
39
sync.RWMutex
40
}
41
@@ -57,7 +58,7 @@ func newPeernet(ctx context.Context, m *mocknet, k ic.PrivKey,
58
mocknet: m,
59
peer: p,
60
ps: ps,
60
- cg: ctxgroup.WithContext(ctx),
61
+ proc: goprocessctx.WithContext(ctx),
62
63
connsByPeer: map[peer.ID]map[*conn]struct{}{},
64
connsByLink: map[*link]map[*conn]struct{}{},
@@ -223,9 +224,9 @@ func (pn *peernet) removeConn(c *conn) {
224
delete(cs, c)
225
}
226
226
-// CtxGroup returns the network's ContextGroup
227
-func (pn *peernet) CtxGroup() ctxgroup.ContextGroup {
228
- return pn.cg
227
+// Process returns the network's Process
228
+func (pn *peernet) Process() goprocess.Process {
229
+ return pn.proc
230
}
231
232
// LocalPeer the network's LocalPeer
p2p/net/swarm/swarm.go
+10
-12
@@ -14,11 +14,12 @@ import (
14
peer "github.com/ipfs/go-ipfs/p2p/peer"
15
eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
16
17
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
17
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
18
ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
19
pst "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport"
20
psy "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport/yamux"
21
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
22
+ goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
23
prom "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
24
mafilter "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/multiaddr-filter"
25
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
@@ -63,8 +64,8 @@ type Swarm struct {
64
// filters for addresses that shouldnt be dialed
65
Filters *filter.Filters
66
66
- cg ctxgroup.ContextGroup
67
- bwc metrics.Reporter
67
+ proc goprocess.Process
68
+ bwc metrics.Reporter
69
}
70
71
// NewSwarm constructs a Swarm, with a Chan.
@@ -80,7 +81,7 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
81
swarm: ps.NewSwarm(PSTransport),
82
local: local,
83
peers: peers,
83
- cg: ctxgroup.WithContext(ctx),
84
+ proc: goprocessctx.WithContext(ctx),
85
dialT: DialTimeout,
86
notifs: make(map[inet.Notifiee]ps.Notifiee),
87
bwc: bwc,
@@ -88,7 +89,7 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
89
}
90
91
// configure Swarm
91
- s.cg.SetTeardown(s.teardown)
92
+ s.proc.SetTeardown(s.teardown)
93
s.SetConnHandler(nil) // make sure to setup our own conn handler.
94
95
// setup swarm metrics
@@ -111,8 +112,6 @@ func (s *Swarm) AddAddrFilter(f string) error {
112
s.Filters.AddDialFilter(m)
113
return nil
114
}
114
-
115
-// CtxGroup returns the Context Group of the swarm
115
func filterAddrs(listenAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
116
if len(listenAddrs) > 0 {
117
filtered := addrutil.FilterUsableAddrs(listenAddrs)
@@ -124,7 +123,6 @@ func filterAddrs(listenAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
123
return listenAddrs, nil
124
}
125
127
-// CtxGroup returns the Context Group of the swarm
126
func (s *Swarm) Listen(addrs ...ma.Multiaddr) error {
127
addrs, err := filterAddrs(addrs)
128
if err != nil {
@@ -134,14 +132,14 @@ func (s *Swarm) Listen(addrs ...ma.Multiaddr) error {
132
return s.listen(addrs)
133
}
134
137
-// CtxGroup returns the Context Group of the swarm
138
-func (s *Swarm) CtxGroup() ctxgroup.ContextGroup {
139
- return s.cg
135
+// Process returns the Process of the swarm
136
+func (s *Swarm) Process() goprocess.Process {
137
+ return s.proc
138
}
139
140
// Close stops the Swarm.
141
func (s *Swarm) Close() error {
144
- return s.cg.Close()
142
+ return s.proc.Close()
143
}
144
145
// StreamSwarm returns the underlying peerstream.Swarm
p2p/net/swarm/swarm_net.go
+5
-5
@@ -8,8 +8,8 @@ import (
8
metrics "github.com/ipfs/go-ipfs/metrics"
9
inet "github.com/ipfs/go-ipfs/p2p/net"
10
11
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
11
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
12
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
13
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
14
)
15
@@ -43,9 +43,9 @@ func (n *Network) DialPeer(ctx context.Context, p peer.ID) (inet.Conn, error) {
43
return inet.Conn(sc), nil
44
}
45
46
-// CtxGroup returns the network's ContextGroup
47
-func (n *Network) CtxGroup() ctxgroup.ContextGroup {
48
- return n.cg
46
+// Process returns the network's Process
47
+func (n *Network) Process() goprocess.Process {
48
+ return n.proc
49
}
50
51
// Swarm returns the network's peerstream.Swarm
@@ -100,7 +100,7 @@ func (n *Network) close() error {
100
101
// Close calls the ContextCloser func
102
func (n *Network) Close() error {
103
- return n.Swarm().cg.Close()
103
+ return n.Swarm().proc.Close()
104
}
105
106
// Listen tells the network to start listening on given multiaddrs.
routing/dht/dht.go
+11
-9
@@ -23,8 +23,9 @@ import (
23
u "github.com/ipfs/go-ipfs/util"
24
25
proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
26
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
26
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
27
+ goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
28
+ goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
29
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
30
)
31
@@ -57,7 +58,8 @@ type IpfsDHT struct {
58
59
Validator record.Validator // record validator funcs
60
60
- ctxgroup.ContextGroup
61
+ Context context.Context
62
+ goprocess.Process
63
}
64
65
// NewDHT creates a new DHT object with the given peer as the 'local' host
@@ -71,14 +73,17 @@ func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *Ip
73
// register for network notifs.
74
dht.host.Network().Notify((*netNotifiee)(dht))
75
74
- dht.ContextGroup = ctxgroup.WithContextAndTeardown(ctx, func() error {
76
+ procctx = goprocessctx.WithContext(ctx)
77
+ procctx.SetTeardown(func() error {
78
// remove ourselves from network notifs.
79
dht.host.Network().StopNotify((*netNotifiee)(dht))
80
return nil
81
})
82
+ dht.Process = procctx
83
+ dht.Context = ctx
84
85
h.SetStreamHandler(ProtocolDHT, dht.handleNewStream)
81
- dht.providers = NewProviderManager(dht.Context(), dht.self)
86
+ dht.providers = NewProviderManager(dht.Context, dht.self)
87
dht.AddChild(dht.providers)
88
89
dht.routingTable = kb.NewRoutingTable(20, kb.ConvertPeerID(dht.self), time.Minute, dht.peerstore)
@@ -88,8 +93,7 @@ func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *Ip
93
dht.Validator["pk"] = record.PublicKeyValidator
94
95
if doPinging {
91
- dht.Children().Add(1)
92
- go dht.PingRoutine(time.Second * 10)
96
+ dht.Go(func() { dht.PingRoutine(time.Second * 10) })
97
}
98
return dht
99
}
@@ -348,8 +352,6 @@ func (dht *IpfsDHT) ensureConnectedToPeer(ctx context.Context, p peer.ID) error
352
353
// PingRoutine periodically pings nearest neighbors.
354
func (dht *IpfsDHT) PingRoutine(t time.Duration) {
351
- defer dht.Children().Done()
352
-
355
tick := time.Tick(t)
356
for {
357
select {
@@ -358,7 +360,7 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) {
360
rand.Read(id)
361
peers := dht.routingTable.NearestPeers(kb.ConvertKey(key.Key(id)), 5)
362
for _, p := range peers {
361
- ctx, cancel := context.WithTimeout(dht.Context(), time.Second*5)
363
+ ctx, cancel := context.WithTimeout(dht.Context, time.Second*5)
364
_, err := dht.Ping(ctx, p)
365
if err != nil {
366
log.Debugf("Ping error: %s", err)
routing/dht/providers.go
+5
-8
@@ -3,7 +3,8 @@ package dht
3
import (
4
"time"
5
6
- ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
6
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
7
+ goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
8
key "github.com/ipfs/go-ipfs/blocks/key"
9
peer "github.com/ipfs/go-ipfs/p2p/peer"
10
@@ -21,7 +22,7 @@ type ProviderManager struct {
22
newprovs chan *addProv
23
getprovs chan *getProv
24
period time.Duration
24
- ctxgroup.ContextGroup
25
+ goprocess.Process
26
}
27
28
type providerSet struct {
@@ -46,17 +47,13 @@ func NewProviderManager(ctx context.Context, local peer.ID) *ProviderManager {
47
pm.providers = make(map[key.Key]*providerSet)
48
pm.getlocal = make(chan chan []key.Key)
49
pm.local = make(map[key.Key]struct{})
49
- pm.ContextGroup = ctxgroup.WithContext(ctx)
50
-
51
- pm.Children().Add(1)
52
- go pm.run()
50
+ pm.Process = goprocessctx.WithContext(ctx)
51
+ pm.Go(pm.run)
52
53
return pm
54
}
55
56
func (pm *ProviderManager) run() {
58
- defer pm.Children().Done()
59
-
57
tick := time.NewTicker(time.Hour)
58
for {
59
select {