peer/queue: close fix, and logging
Juan Batiz-Benet committed
Jan 5, 2015 at 04:33 UTC
f25dfb68b98a3fe78d9184b30414daa07ab0ed12
1 file changed
+16
-3
p2p/peer/queue/sync.go
+16
-3
@@ -4,8 +4,11 @@ import (
4
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
6
peer "github.com/jbenet/go-ipfs/p2p/peer"
7
+ eventlog "github.com/jbenet/go-ipfs/util/eventlog"
8
)
9
10
+var log = eventlog.Logger("peerqueue")
11
+
12
// ChanQueue makes any PeerQueue synchronizable through channels.
13
type ChanQueue struct {
14
Queue PeerQueue
@@ -21,6 +24,7 @@ func NewChanQueue(ctx context.Context, pq PeerQueue) *ChanQueue {
24
}
25
26
func (cq *ChanQueue) process(ctx context.Context) {
27
+ log := log.Prefix("<ChanQueue %p>", cq)
28
29
// construct the channels here to be able to use them bidirectionally
30
enqChan := make(chan peer.ID)
@@ -30,6 +34,8 @@ func (cq *ChanQueue) process(ctx context.Context) {
34
cq.DeqChan = deqChan
35
36
go func() {
37
+ log.Debug("processing")
38
+ defer log.Debug("closed")
39
defer close(deqChan)
40
41
var next peer.ID
@@ -38,11 +44,13 @@ func (cq *ChanQueue) process(ctx context.Context) {
44
45
for {
46
if cq.Queue.Len() == 0 {
47
+ // log.Debug("wait for enqueue")
48
select {
49
case next, more = <-enqChan:
50
if !more {
51
return
52
}
53
+ // log.Debug("got", next)
54
55
case <-ctx.Done():
56
return
@@ -50,19 +58,24 @@ func (cq *ChanQueue) process(ctx context.Context) {
58
59
} else {
60
next = cq.Queue.Dequeue()
61
+ // log.Debug("peek", next)
62
}
63
64
select {
65
case item, more = <-enqChan:
66
if !more {
58
- return
67
+ if cq.Queue.Len() > 0 {
68
+ return // we're done done.
69
+ }
70
+ enqChan = nil // closed, so no use.
71
}
60
-
72
+ // log.Debug("got", item)
73
cq.Queue.Enqueue(item)
62
- cq.Queue.Enqueue(next)
74
+ cq.Queue.Enqueue(next) // order may have changed.
75
next = ""
76
77
case deqChan <- next:
78
+ // log.Debug("dequeued", next)
79
next = ""
80
81
case <-ctx.Done():