Move clamp and max, and add min to misc.go
This commit is contained in:
parent
dbb2936cdf
commit
646622b8f1
@ -314,29 +314,6 @@ func (cn *connection) requestedMetadataPiece(index int) bool {
|
|||||||
return index < len(cn.metadataRequests) && cn.metadataRequests[index]
|
return index < len(cn.metadataRequests) && cn.metadataRequests[index]
|
||||||
}
|
}
|
||||||
|
|
||||||
func clamp(min, value, max int64) int64 {
|
|
||||||
if min > max {
|
|
||||||
panic("harumph")
|
|
||||||
}
|
|
||||||
if value < min {
|
|
||||||
value = min
|
|
||||||
}
|
|
||||||
if value > max {
|
|
||||||
value = max
|
|
||||||
}
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
func max(as ...int64) int64 {
|
|
||||||
ret := as[0]
|
|
||||||
for _, a := range as[1:] {
|
|
||||||
if a > ret {
|
|
||||||
ret = a
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
// The actual value to use as the maximum outbound requests.
|
// The actual value to use as the maximum outbound requests.
|
||||||
func (cn *connection) nominalMaxRequests() (ret int) {
|
func (cn *connection) nominalMaxRequests() (ret int) {
|
||||||
return int(clamp(1, int64(cn.PeerMaxRequests), max(64, cn.stats.ChunksReadUseful-(cn.stats.ChunksRead-cn.stats.ChunksReadUseful))))
|
return int(clamp(1, int64(cn.PeerMaxRequests), max(64, cn.stats.ChunksReadUseful-(cn.stats.ChunksRead-cn.stats.ChunksReadUseful))))
|
||||||
|
33
misc.go
33
misc.go
@ -130,3 +130,36 @@ func connIsIpv6(nc net.Conn) bool {
|
|||||||
rip := missinggo.AddrIP(ra)
|
rip := missinggo.AddrIP(ra)
|
||||||
return rip.To4() == nil && rip.To16() != nil
|
return rip.To4() == nil && rip.To16() != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func clamp(min, value, max int64) int64 {
|
||||||
|
if min > max {
|
||||||
|
panic("harumph")
|
||||||
|
}
|
||||||
|
if value < min {
|
||||||
|
value = min
|
||||||
|
}
|
||||||
|
if value > max {
|
||||||
|
value = max
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(as ...int64) int64 {
|
||||||
|
ret := as[0]
|
||||||
|
for _, a := range as[1:] {
|
||||||
|
if a > ret {
|
||||||
|
ret = a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(as ...int64) int64 {
|
||||||
|
ret := as[0]
|
||||||
|
for _, a := range as[1:] {
|
||||||
|
if a < ret {
|
||||||
|
ret = a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user