A package inspired by go-hostpool, to manage efficiently pools of connection across multiple hosts. Pooly uses multi-armed bandit algorithms for host selection in order to maximize service connectivity by avoiding faulty hosts.
Currently supported bandit strategies:
- Softmax
- Epsilon-greedy
- Round-robin
Start two dummy netcat servers.
nc -l -p 1234 &
nc -l -p 12345 &
Get two connections from the "netcat" service using the NetDriver and the RoundRobin strategy (defaults).
package main
import "fmt"
import "github.com/3XX0/pooly"
func ping(s *pooly.Service) error {
c, err := s.GetConn()
if err != nil {
return err
}
defer c.Release(&err, pooly.HostUp)
_, err = c.NetConn().Write([]byte("ping\n"))
return err
}
func main() {
s := pooly.NewService("netcat", nil)
defer s.Close()
s.Add("127.0.0.1:1234")
s.Add("127.0.0.1:12345")
if err := ping(s); err != nil {
fmt.Println(err)
return
}
if err := ping(s); err != nil {
fmt.Println(err)
return
}
}
Pooly exports useful service metrics provided that a statsd server is running and the option ServiceConfig.StatsdAddr is set accordingly.
The following metrics are available:
metric | description |
---|---|
hosts.score | average score of the service hosts (in percentage) |
hosts.count | number of hosts registered to the service |
conns.count | number of connections spawned by the service |
conns.fails | number of connection failures (temporary included) |
conns.put.count | number of Release performed |
conns.get.count | number of GetConn performed |
conns.get.delay | average delay before receiving a connection from the service (in millisecond) |
conns.get.fails | number of GetConn failures |
conns.active.period | average time during which a connection was active (in millisecond) |
Example of a grafana dashboard using vizu
In order to test the behavior of different strategies, Monte Carlo simulations may be run through the Go testing framework.
go test -tags 'montecarlo_simulation' -v pooly
Softmax strategy sample output
Epsilon-greedy strategy sample output