muun-recovery/electrum/pool.go

29 lines
759 B
Go
Raw Permalink Normal View History

2021-01-29 16:51:08 -05:00
package electrum
// Pool provides a shared pool of Clients that callers can acquire and release, limiting
// the amount of concurrent Clients in active use.
type Pool struct {
nextClient chan *Client
}
// NewPool creates an initialized Pool with a `size` number of clients.
2023-02-20 14:57:43 -05:00
func NewPool(size int, requireTls bool) *Pool {
2021-01-29 16:51:08 -05:00
nextClient := make(chan *Client, size)
for i := 0; i < size; i++ {
2023-02-20 14:57:43 -05:00
nextClient <- NewClient(requireTls)
2021-01-29 16:51:08 -05:00
}
return &Pool{nextClient}
}
// Acquire obtains an unused Client, blocking until one is released.
func (p *Pool) Acquire() <-chan *Client {
return p.nextClient
}
// Release returns a Client to the pool, unblocking the next caller trying to `Acquire()`.
func (p *Pool) Release(client *Client) {
p.nextClient <- client
}