@@ -117,6 +117,17 @@ func getOperations(dequeued [][]byte) ([][]mh.Multihash, error) {
117
117
return ops [:], nil
118
118
}
119
119
120
+ // executeOperation executes a provider operation on the underlying provider
121
+ // with the given multihashes, logging any errors encountered.
122
+ func executeOperation (f func (... mh.Multihash ) error , keys []mh.Multihash ) {
123
+ if len (keys ) == 0 {
124
+ return
125
+ }
126
+ if err := f (keys ... ); err != nil {
127
+ logger .Warn (err )
128
+ }
129
+ }
130
+
120
131
// worker processes operations from the queue in batches.
121
132
// It runs in a separate goroutine and continues until the provider is closed.
122
133
func (s * SweepingProvider ) worker () {
@@ -142,26 +153,14 @@ func (s *SweepingProvider) worker() {
142
153
// Process `StartProviding` (force=true) ops first, so that if
143
154
// `StartProviding` (force=false) is called after, there is no need to
144
155
// enqueue the multihash a second time to the provide queue.
145
- err = s .provider .StartProviding (true , ops [forceStartProvidingOp ]... )
146
- if err != nil {
147
- logger .Warnf ("BufferedSweepingProvider unable to start providing (force): %v" , err )
148
- }
149
- err = s .provider .StartProviding (false , ops [startProvidingOp ]... )
150
- if err != nil {
151
- logger .Warnf ("BufferedSweepingProvider unable to start providing: %v" , err )
152
- }
153
- err = s .provider .ProvideOnce (ops [provideOnceOp ]... )
154
- if err != nil {
155
- logger .Warnf ("BufferedSweepingProvider unable to provide once: %v" , err )
156
- }
156
+ executeOperation (func (keys ... mh.Multihash ) error { return s .provider .StartProviding (true , keys ... ) }, ops [forceStartProvidingOp ])
157
+ executeOperation (func (keys ... mh.Multihash ) error { return s .provider .StartProviding (false , keys ... ) }, ops [startProvidingOp ])
158
+ executeOperation (s .provider .ProvideOnce , ops [provideOnceOp ])
157
159
// Process `StopProviding` last, so that multihashes that should have been
158
160
// provided, and then stopped provided in the same batch are provided only
159
161
// once. Don't `StopProviding` multihashes, for which `StartProviding` has
160
162
// been called after `StopProviding`.
161
- err = s .provider .StopProviding (ops [stopProvidingOp ]... )
162
- if err != nil {
163
- logger .Warnf ("BufferedSweepingProvider unable to stop providing: %v" , err )
164
- }
163
+ executeOperation (s .provider .StopProviding , ops [stopProvidingOp ])
165
164
}
166
165
}
167
166
0 commit comments