summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/olahol/melody.v1/hub.go
diff options
context:
space:
mode:
authorNikkyAI <root@nikky.moe>2020-08-26 22:27:00 +0200
committerGitHub <noreply@github.com>2020-08-26 22:27:00 +0200
commit27c02549c870680ea57a05757810ade80db42929 (patch)
tree011862f50e0101205afdb786ee3ff2503a677f22 /vendor/gopkg.in/olahol/melody.v1/hub.go
parent88d371c71c61aa8b68033b9c2dd4d7a8fc36e991 (diff)
downloadmatterbridge-msglm-27c02549c870680ea57a05757810ade80db42929.tar.gz
matterbridge-msglm-27c02549c870680ea57a05757810ade80db42929.tar.bz2
matterbridge-msglm-27c02549c870680ea57a05757810ade80db42929.zip
Replace gorilla with melody for websocket API (#1205)
Diffstat (limited to 'vendor/gopkg.in/olahol/melody.v1/hub.go')
-rw-r--r--vendor/gopkg.in/olahol/melody.v1/hub.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/vendor/gopkg.in/olahol/melody.v1/hub.go b/vendor/gopkg.in/olahol/melody.v1/hub.go
new file mode 100644
index 00000000..edc6337d
--- /dev/null
+++ b/vendor/gopkg.in/olahol/melody.v1/hub.go
@@ -0,0 +1,80 @@
+package melody
+
+import (
+ "sync"
+)
+
+type hub struct {
+ sessions map[*Session]bool
+ broadcast chan *envelope
+ register chan *Session
+ unregister chan *Session
+ exit chan *envelope
+ open bool
+ rwmutex *sync.RWMutex
+}
+
+func newHub() *hub {
+ return &hub{
+ sessions: make(map[*Session]bool),
+ broadcast: make(chan *envelope),
+ register: make(chan *Session),
+ unregister: make(chan *Session),
+ exit: make(chan *envelope),
+ open: true,
+ rwmutex: &sync.RWMutex{},
+ }
+}
+
+func (h *hub) run() {
+loop:
+ for {
+ select {
+ case s := <-h.register:
+ h.rwmutex.Lock()
+ h.sessions[s] = true
+ h.rwmutex.Unlock()
+ case s := <-h.unregister:
+ if _, ok := h.sessions[s]; ok {
+ h.rwmutex.Lock()
+ delete(h.sessions, s)
+ h.rwmutex.Unlock()
+ }
+ case m := <-h.broadcast:
+ h.rwmutex.RLock()
+ for s := range h.sessions {
+ if m.filter != nil {
+ if m.filter(s) {
+ s.writeMessage(m)
+ }
+ } else {
+ s.writeMessage(m)
+ }
+ }
+ h.rwmutex.RUnlock()
+ case m := <-h.exit:
+ h.rwmutex.Lock()
+ for s := range h.sessions {
+ s.writeMessage(m)
+ delete(h.sessions, s)
+ s.Close()
+ }
+ h.open = false
+ h.rwmutex.Unlock()
+ break loop
+ }
+ }
+}
+
+func (h *hub) closed() bool {
+ h.rwmutex.RLock()
+ defer h.rwmutex.RUnlock()
+ return !h.open
+}
+
+func (h *hub) len() int {
+ h.rwmutex.RLock()
+ defer h.rwmutex.RUnlock()
+
+ return len(h.sessions)
+}