Copyright 2020 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package fetch

import (
	
)

The maximum size of requests that can be processed at once. If an incoming request would cause sizeInFlight to exceed this value, it won't be processed.
Protects the variables below, and also serializes shedding decisions so multiple simultaneous requests are handled properly.
	mu sync.Mutex

	sizeInFlight     uint64 // size of requests currently in progress.
	requestsInFlight int    // number of request currently in progress
	requestsTotal    int    // total fetch requests ever seen
	requestsShed     int    // number of requests that were shedded
}
shouldShed reports whether a request of size should be shed (not processed). Its second return value is a function that should be deferred by the caller.
func ( *loadShedder) ( uint64) ( bool,  func()) {
	.mu.Lock()
	defer .mu.Unlock()

Shed if size exceeds our limit--except that if nothing is being processed, accept this request to avoid starving it forever.
	if .sizeInFlight > 0 && .sizeInFlight+ > .maxSizeInFlight {
		.requestsShed++
		return true, func() {}
	}
	.sizeInFlight += 
	.requestsInFlight++
	return false, func() {
		.mu.Lock()
		defer .mu.Unlock()
		.sizeInFlight -= 
		.requestsInFlight--
	}
}