src.dualinventive.com/go/websocketserver/internal/wsconn/clientlist.go

48 lines
818 B
Go

package wsconn
import (
"container/list"
"sync"
)
// clientList is a list of clients
type clientList struct {
mu sync.RWMutex
list *list.List
}
// newClientList creates a empty list for storing clients
func newClientList() *clientList {
return &clientList{list: list.New()}
}
// Add appends a client to the list
func (l *clientList) Add(c *client) {
l.mu.Lock()
l.list.PushBack(c)
l.mu.Unlock()
}
// Remove a client from the list
func (l *clientList) Remove(c *client) {
l.mu.Lock()
for e := l.list.Front(); e != nil; e = e.Next() {
if e.Value != c {
continue
}
l.list.Remove(e)
break
}
l.mu.Unlock()
}
// RLock locks the list for read access
func (l *clientList) RLock() {
l.mu.RLock()
}
// RUnlock unlocks the list from read access
func (l *clientList) RUnlock() {
l.mu.RUnlock()
}