-// Determines interest and requests to send to a connected peer.
-func nextRequestState(
- networkingEnabled bool,
- currentRequests map[request]struct{},
- peerChoking bool,
- iterPendingRequests func(f func(request) bool),
- requestsLowWater int,
- requestsHighWater int,
- allowedFast bitmap.Bitmap,
-) (
- cancelExisting bool, // Cancel all our pending requests
- newRequests []request, // Chunks to request that we currently aren't
- interested bool, // Whether we should indicate interest, even if we don't request anything
-) {
- if !networkingEnabled {
- return true, nil, false
- }
- if len(currentRequests) > requestsLowWater {
- return false, nil, true
- }
- // If we have existing requests, better maintain interest to ensure we get
- // them. iterPendingRequests might not iterate over outstanding requests.
- interested = len(currentRequests) != 0
- iterPendingRequests(func(r request) bool {
- interested = true
- if peerChoking {
- if allowedFast.IsEmpty() {
- return false
- }
- if !allowedFast.Get(int(r.Index)) {
- return true
- }
- }
- if len(currentRequests)+len(newRequests) >= requestsHighWater {
- return false
- }
- if _, ok := currentRequests[r]; !ok {
- if newRequests == nil {
- newRequests = make([]request, 0, requestsHighWater-len(currentRequests))
- }
- newRequests = append(newRequests, r)
- }
- return true
- })
- return
-}
-