Skip to content

Commit 0551bf2

Browse files
removing redundant code
1 parent e10840a commit 0551bf2

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

provider/buffered/provider.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ func getOperations(dequeued [][]byte) ([][]mh.Multihash, error) {
117117
return ops[:], nil
118118
}
119119

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+
120131
// worker processes operations from the queue in batches.
121132
// It runs in a separate goroutine and continues until the provider is closed.
122133
func (s *SweepingProvider) worker() {
@@ -142,26 +153,14 @@ func (s *SweepingProvider) worker() {
142153
// Process `StartProviding` (force=true) ops first, so that if
143154
// `StartProviding` (force=false) is called after, there is no need to
144155
// 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])
157159
// Process `StopProviding` last, so that multihashes that should have been
158160
// provided, and then stopped provided in the same batch are provided only
159161
// once. Don't `StopProviding` multihashes, for which `StartProviding` has
160162
// 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])
165164
}
166165
}
167166

0 commit comments

Comments
 (0)