L: cl.locker(),
},
}
- t.requestStrategy = cl.config.DefaultRequestStrategy(t.requestStrategyCallbacks())
+ t.requestStrategy = cl.config.DefaultRequestStrategy(t.requestStrategyCallbacks(), &cl._mu)
t.logger = cl.logger.WithValues(t).WithText(func(m log.Msg) string {
return fmt.Sprintf("%v: %s", t, m.Text())
})
func (rs requestStrategyThree) onSentRequest(r request) {
rs.lastRequested[r] = time.AfterFunc(rs.duplicateRequestTimeout, func() {
+ rs.timeoutLocker.Lock()
delete(rs.lastRequested, r)
+ rs.timeoutLocker.Unlock()
rs.callbacks.requestTimedOut(r)
})
}
package torrent
import (
+ "sync"
"time"
"github.com/anacrolix/missinggo/v2/bitmap"
type requestStrategyThree struct {
// How long to avoid duplicating a pending request.
duplicateRequestTimeout time.Duration
+
+ callbacks requestStrategyCallbacks
+
// The last time we requested a chunk. Deleting the request from any connection will clear this
// value.
lastRequested map[request]*time.Timer
- callbacks requestStrategyCallbacks
+ // The lock to take when running a request timeout handler.
+ timeoutLocker sync.Locker
}
-type requestStrategyMaker func(callbacks requestStrategyCallbacks) requestStrategy
+type requestStrategyMaker func(callbacks requestStrategyCallbacks, clientLocker sync.Locker) requestStrategy
func requestStrategyThreeMaker(duplicateRequestTimeout time.Duration) requestStrategyMaker {
- return func(callbacks requestStrategyCallbacks) requestStrategy {
+ return func(callbacks requestStrategyCallbacks, clientLocker sync.Locker) requestStrategy {
return requestStrategyThree{
duplicateRequestTimeout: duplicateRequestTimeout,
callbacks: callbacks,
lastRequested: make(map[request]*time.Timer),
+ timeoutLocker: clientLocker,
}
}
}