Use the built-in max/min to simplify the code (#7081)

Signed-off-by: xiaoxiangirl <caojiaqiao@outlook.com>
This commit is contained in:
曹家巧 2025-06-20 06:39:48 +08:00 committed by GitHub
parent 2f0fc62b34
commit 070d454c0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 2 additions and 8 deletions

View file

@ -236,10 +236,7 @@ func (c *Cache) makeRoom() {
// the cache is on a long tail, we can save a lot of CPU // the cache is on a long tail, we can save a lot of CPU
// time by doing a whole bunch of deletions now and then // time by doing a whole bunch of deletions now and then
// we won't have to do them again for a while // we won't have to do them again for a while
numToDelete := len(c.cache) / 10 numToDelete := max(len(c.cache)/10, 1)
if numToDelete < 1 {
numToDelete = 1
}
for deleted := 0; deleted <= numToDelete; deleted++ { for deleted := 0; deleted <= numToDelete; deleted++ {
// Go maps are "nondeterministic" not actually random, // Go maps are "nondeterministic" not actually random,
// so although we could just chop off the "front" of the // so although we could just chop off the "front" of the

View file

@ -219,10 +219,7 @@ func (r RandomChoiceSelection) Validate() error {
// Select returns an available host, if any. // Select returns an available host, if any.
func (r RandomChoiceSelection) Select(pool UpstreamPool, _ *http.Request, _ http.ResponseWriter) *Upstream { func (r RandomChoiceSelection) Select(pool UpstreamPool, _ *http.Request, _ http.ResponseWriter) *Upstream {
k := r.Choose k := min(r.Choose, len(pool))
if k > len(pool) {
k = len(pool)
}
choices := make([]*Upstream, k) choices := make([]*Upstream, k)
for i, upstream := range pool { for i, upstream := range pool {
if !upstream.Available() { if !upstream.Available() {