45 lines
662 B
Go
45 lines
662 B
Go
package pool_test
|
|
|
|
import (
|
|
"code.squareroundforest.org/arpio/pool"
|
|
"time"
|
|
)
|
|
|
|
type maxAlgo int
|
|
|
|
type timeoutAlgo struct {
|
|
to time.Duration
|
|
items []time.Time
|
|
}
|
|
|
|
func (a maxAlgo) Target(s pool.Stats) (int, time.Duration) {
|
|
if s.Idle <= int(a) {
|
|
return s.Idle, 0
|
|
}
|
|
|
|
return int(a), 0
|
|
}
|
|
|
|
func (a *timeoutAlgo) Target(s pool.Stats) (int, time.Duration) {
|
|
now := time.Now()
|
|
for len(a.items) < s.Idle {
|
|
a.items = append(a.items, now)
|
|
}
|
|
|
|
t := s.Idle
|
|
for len(a.items) > 0 && a.items[0].Add(a.to).Before(now) {
|
|
t--
|
|
a.items = a.items[1:]
|
|
}
|
|
|
|
if t < 0 {
|
|
t = 0
|
|
}
|
|
|
|
if len(a.items) == 0 {
|
|
return t, 0
|
|
}
|
|
|
|
return t, a.items[0].Add(a.to).Sub(now)
|
|
}
|