2019-11-27 17:12:07 +00:00
|
|
|
package socket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Pool holds the socket pool
|
2019-11-27 17:12:07 +00:00
|
|
|
type Pool struct {
|
2022-03-30 15:37:02 +03:00
|
|
|
pool map[string]*Socket
|
2024-12-09 16:23:25 +03:00
|
|
|
sync.RWMutex
|
2019-11-27 17:12:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Get socket from pool
|
2019-11-27 17:12:07 +00:00
|
|
|
func (p *Pool) Get(id string) (*Socket, bool) {
|
|
|
|
// attempt to get existing socket
|
|
|
|
p.RLock()
|
|
|
|
socket, ok := p.pool[id]
|
|
|
|
if ok {
|
|
|
|
p.RUnlock()
|
|
|
|
return socket, ok
|
|
|
|
}
|
|
|
|
p.RUnlock()
|
|
|
|
|
|
|
|
// save socket
|
|
|
|
p.Lock()
|
2020-05-27 20:18:26 +01:00
|
|
|
defer p.Unlock()
|
|
|
|
// double checked locking
|
|
|
|
socket, ok = p.pool[id]
|
|
|
|
if ok {
|
|
|
|
return socket, ok
|
|
|
|
}
|
|
|
|
// create new socket
|
|
|
|
socket = New(id)
|
2019-11-27 17:12:07 +00:00
|
|
|
p.pool[id] = socket
|
2020-05-27 20:18:26 +01:00
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// return socket
|
|
|
|
return socket, false
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Release close the socket and delete from pool
|
2019-11-27 17:12:07 +00:00
|
|
|
func (p *Pool) Release(s *Socket) {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
// close the socket
|
|
|
|
s.Close()
|
|
|
|
delete(p.pool, s.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the pool and delete all the sockets
|
|
|
|
func (p *Pool) Close() {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
for id, sock := range p.pool {
|
|
|
|
sock.Close()
|
|
|
|
delete(p.pool, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPool returns a new socket pool
|
|
|
|
func NewPool() *Pool {
|
|
|
|
return &Pool{
|
|
|
|
pool: make(map[string]*Socket),
|
|
|
|
}
|
|
|
|
}
|