@cryptotaxi247 / kubo / commits / bfee4894b

simplify provideCollector

Jeromy committed Mar 5, 2015 at 16:27 UTC bfee4894b4ad734202b14cccb2b718de081f9edc
1 file changed +9 -22
exchange/bitswap/workers.go
+9 -22
@@ -83,17 +83,7 @@ func (bs *Bitswap) provideCollector(ctx context.Context) {
83 defer close(bs.provideKeys)
84 var toprovide []u.Key
85 var nextKey u.Key
86 -
87 - select {
88 - case blk, ok := <-bs.newBlocks:
89 - if !ok {
90 - log.Debug("newBlocks channel closed")
91 - return
92 - }
93 - nextKey = blk.Key()
94 - case <-ctx.Done():
95 - return
96 - }
86 + var keysOut chan u.Key
87
88 for {
89 select {
@@ -102,21 +92,18 @@ func (bs *Bitswap) provideCollector(ctx context.Context) {
92 log.Debug("newBlocks channel closed")
93 return
94 }
105 - toprovide = append(toprovide, blk.Key())
106 - case bs.provideKeys <- nextKey:
95 + if keysOut == nil {
96 + nextKey = blk.Key()
97 + keysOut = bs.provideKeys
98 + } else {
99 + toprovide = append(toprovide, blk.Key())
100 + }
101 + case keysOut <- nextKey:
102 if len(toprovide) > 0 {
103 nextKey = toprovide[0]
104 toprovide = toprovide[1:]
105 } else {
111 - select {
112 - case blk, ok := <-bs.newBlocks:
113 - if !ok {
114 - return
115 - }
116 - nextKey = blk.Key()
117 - case <-ctx.Done():
118 - return
119 - }
106 + keysOut = nil
107 }
108 case <-ctx.Done():
109 return