bigbluebutton-Github/bbb-graphql-middleware/internal/common/SafeChannel.go
Gustavo Trott a2e2d0516c
fix (graphql-middleware): Client can't send new graphql queries after a while (#18895)
* fix: client errors with graphql

* Use NewSafeChannel to handle msgs from browser
2023-10-04 17:11:12 -03:00

44 lines
599 B
Go

package common
import (
"sync"
)
type SafeChannel struct {
ch chan interface{}
closed bool
mux sync.Mutex
}
func NewSafeChannel(size int) *SafeChannel {
return &SafeChannel{
ch: make(chan interface{}, size),
}
}
func (s *SafeChannel) Send(value interface{}) bool {
s.mux.Lock()
defer s.mux.Unlock()
if s.closed {
return false
}
s.ch <- value
return true
}
func (s *SafeChannel) Receive() (interface{}, bool) {
val, ok := <-s.ch
return val, ok
}
func (s *SafeChannel) Close() {
s.mux.Lock()
defer s.mux.Unlock()
if !s.closed {
close(s.ch)
s.closed = true
}
}