Update dependencies (#2180)
Some checks failed
Development / golangci-lint (push) Has been cancelled
Development / test-build-upload (1.22.x, ubuntu-latest) (push) Has been cancelled

* Update dependencies

* Fix whatsmeow API changes
This commit is contained in:
Wim
2024-08-27 19:04:05 +02:00
committed by GitHub
parent d16645c952
commit c4157a4d5b
589 changed files with 681707 additions and 198856 deletions

View File

@@ -4,18 +4,19 @@ import "time"
// Config melody configuration struct.
type Config struct {
WriteWait time.Duration // Milliseconds until write times out.
PongWait time.Duration // Timeout for waiting on pong.
PingPeriod time.Duration // Milliseconds between pings.
MaxMessageSize int64 // Maximum size in bytes of a message.
MessageBufferSize int // The max amount of messages that can be in a sessions buffer before it starts dropping them.
WriteWait time.Duration // Duration until write times out.
PongWait time.Duration // Timeout for waiting on pong.
PingPeriod time.Duration // Duration between pings.
MaxMessageSize int64 // Maximum size in bytes of a message.
MessageBufferSize int // The max amount of messages that can be in a sessions buffer before it starts dropping them.
ConcurrentMessageHandling bool // Handle messages from sessions concurrently.
}
func newConfig() *Config {
return &Config{
WriteWait: 10 * time.Second,
PongWait: 60 * time.Second,
PingPeriod: (60 * time.Second * 9) / 10,
PingPeriod: 54 * time.Second,
MaxMessageSize: 512,
MessageBufferSize: 256,
}

View File

@@ -2,90 +2,125 @@ package melody
import (
"sync"
"sync/atomic"
)
type sessionSet struct {
mu sync.RWMutex
members map[*Session]struct{}
}
func (ss *sessionSet) add(s *Session) {
ss.mu.Lock()
defer ss.mu.Unlock()
ss.members[s] = struct{}{}
}
func (ss *sessionSet) del(s *Session) {
ss.mu.Lock()
defer ss.mu.Unlock()
delete(ss.members, s)
}
func (ss *sessionSet) clear() {
ss.mu.Lock()
defer ss.mu.Unlock()
ss.members = make(map[*Session]struct{})
}
func (ss *sessionSet) each(cb func(*Session)) {
ss.mu.RLock()
defer ss.mu.RUnlock()
for s := range ss.members {
cb(s)
}
}
func (ss *sessionSet) len() int {
ss.mu.RLock()
defer ss.mu.RUnlock()
return len(ss.members)
}
func (ss *sessionSet) all() []*Session {
ss.mu.RLock()
defer ss.mu.RUnlock()
s := make([]*Session, 0, len(ss.members))
for k := range ss.members {
s = append(s, k)
}
return s
}
type hub struct {
sessions map[*Session]bool
broadcast chan *envelope
sessions sessionSet
broadcast chan envelope
register chan *Session
unregister chan *Session
exit chan *envelope
open bool
rwmutex *sync.RWMutex
exit chan envelope
open atomic.Bool
}
func newHub() *hub {
return &hub{
sessions: make(map[*Session]bool),
broadcast: make(chan *envelope),
sessions: sessionSet{
members: make(map[*Session]struct{}),
},
broadcast: make(chan envelope),
register: make(chan *Session),
unregister: make(chan *Session),
exit: make(chan *envelope),
open: true,
rwmutex: &sync.RWMutex{},
exit: make(chan envelope),
}
}
func (h *hub) run() {
h.open.Store(true)
loop:
for {
select {
case s := <-h.register:
h.rwmutex.Lock()
h.sessions[s] = true
h.rwmutex.Unlock()
h.sessions.add(s)
case s := <-h.unregister:
if _, ok := h.sessions[s]; ok {
h.rwmutex.Lock()
delete(h.sessions, s)
h.rwmutex.Unlock()
}
h.sessions.del(s)
case m := <-h.broadcast:
h.rwmutex.RLock()
for s := range h.sessions {
if m.filter != nil {
if m.filter(s) {
s.writeMessage(m)
}
} else {
h.sessions.each(func(s *Session) {
if m.filter == nil {
s.writeMessage(m)
} else if m.filter(s) {
s.writeMessage(m)
}
}
h.rwmutex.RUnlock()
})
case m := <-h.exit:
h.rwmutex.Lock()
for s := range h.sessions {
h.open.Store(false)
h.sessions.each(func(s *Session) {
s.writeMessage(m)
delete(h.sessions, s)
s.Close()
}
h.open = false
h.rwmutex.Unlock()
})
h.sessions.clear()
break loop
}
}
}
func (h *hub) closed() bool {
h.rwmutex.RLock()
defer h.rwmutex.RUnlock()
return !h.open
return !h.open.Load()
}
func (h *hub) len() int {
h.rwmutex.RLock()
defer h.rwmutex.RUnlock()
return len(h.sessions)
return h.sessions.len()
}
func (h *hub) all() []*Session {
h.rwmutex.RLock()
defer h.rwmutex.RUnlock()
s := make([]*Session, 0, len(h.sessions))
for k := range h.sessions {
s = append(s, k)
}
return s
return h.sessions.all()
}

View File

@@ -26,26 +26,6 @@ const (
CloseTLSHandshake = 1015
)
// Duplicate of codes from gorilla/websocket for convenience.
var validReceivedCloseCodes = map[int]bool{
// see http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
CloseNormalClosure: true,
CloseGoingAway: true,
CloseProtocolError: true,
CloseUnsupportedData: true,
CloseNoStatusReceived: false,
CloseAbnormalClosure: false,
CloseInvalidFramePayloadData: true,
ClosePolicyViolation: true,
CloseMessageTooBig: true,
CloseMandatoryExtension: true,
CloseInternalServerErr: true,
CloseServiceRestart: true,
CloseTryAgainLater: true,
CloseTLSHandshake: false,
}
type handleMessageFunc func(*Session, []byte)
type handleErrorFunc func(*Session, error)
type handleCloseFunc func(*Session, int, string) error
@@ -112,6 +92,11 @@ func (m *Melody) HandlePong(fn func(*Session)) {
}
// HandleMessage fires fn when a text message comes in.
// NOTE: by default Melody handles messages sequentially for each
// session. This has the effect that a message handler exceeding the
// read deadline (Config.PongWait, by default 1 minute) will time out
// the session. Concurrent message handling can be turned on by setting
// Config.ConcurrentMessageHandling to true.
func (m *Melody) HandleMessage(fn func(*Session, []byte)) {
m.messageHandler = fn
}
@@ -161,7 +146,7 @@ func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error {
}
// HandleRequestWithKeys does the same as HandleRequest but populates session.Keys with keys.
func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, keys map[string]interface{}) error {
func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, keys map[string]any) error {
if m.hub.closed() {
return ErrClosed
}
@@ -176,7 +161,7 @@ func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, k
Request: r,
Keys: keys,
conn: conn,
output: make(chan *envelope, m.Config.MessageBufferSize),
output: make(chan envelope, m.Config.MessageBufferSize),
outputDone: make(chan struct{}),
melody: m,
open: true,
@@ -208,7 +193,7 @@ func (m *Melody) Broadcast(msg []byte) error {
return ErrClosed
}
message := &envelope{t: websocket.TextMessage, msg: msg}
message := envelope{t: websocket.TextMessage, msg: msg}
m.hub.broadcast <- message
return nil
@@ -220,7 +205,7 @@ func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) error {
return ErrClosed
}
message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn}
message := envelope{t: websocket.TextMessage, msg: msg, filter: fn}
m.hub.broadcast <- message
return nil
@@ -249,7 +234,7 @@ func (m *Melody) BroadcastBinary(msg []byte) error {
return ErrClosed
}
message := &envelope{t: websocket.BinaryMessage, msg: msg}
message := envelope{t: websocket.BinaryMessage, msg: msg}
m.hub.broadcast <- message
return nil
@@ -261,7 +246,7 @@ func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) error
return ErrClosed
}
message := &envelope{t: websocket.BinaryMessage, msg: msg, filter: fn}
message := envelope{t: websocket.BinaryMessage, msg: msg, filter: fn}
m.hub.broadcast <- message
return nil
@@ -288,7 +273,7 @@ func (m *Melody) Close() error {
return ErrClosed
}
m.hub.exit <- &envelope{t: websocket.CloseMessage, msg: []byte{}}
m.hub.exit <- envelope{t: websocket.CloseMessage, msg: []byte{}}
return nil
}
@@ -300,7 +285,7 @@ func (m *Melody) CloseWithMsg(msg []byte) error {
return ErrClosed
}
m.hub.exit <- &envelope{t: websocket.CloseMessage, msg: msg}
m.hub.exit <- envelope{t: websocket.CloseMessage, msg: msg}
return nil
}

View File

@@ -12,16 +12,16 @@ import (
// Session wrapper around websocket connections.
type Session struct {
Request *http.Request
Keys map[string]interface{}
Keys map[string]any
conn *websocket.Conn
output chan *envelope
output chan envelope
outputDone chan struct{}
melody *Melody
open bool
rwmutex *sync.RWMutex
}
func (s *Session) writeMessage(message *envelope) {
func (s *Session) writeMessage(message envelope) {
if s.closed() {
s.melody.errorHandler(s, ErrWriteClosed)
return
@@ -34,7 +34,7 @@ func (s *Session) writeMessage(message *envelope) {
}
}
func (s *Session) writeRaw(message *envelope) error {
func (s *Session) writeRaw(message envelope) error {
if s.closed() {
return ErrWriteClosed
}
@@ -68,7 +68,7 @@ func (s *Session) close() {
}
func (s *Session) ping() {
s.writeRaw(&envelope{t: websocket.PingMessage, msg: []byte{}})
s.writeRaw(envelope{t: websocket.PingMessage, msg: []byte{}})
}
func (s *Session) writePump() {
@@ -133,13 +133,20 @@ func (s *Session) readPump() {
break
}
if t == websocket.TextMessage {
s.melody.messageHandler(s, message)
if s.melody.Config.ConcurrentMessageHandling {
go s.handleMessage(t, message)
} else {
s.handleMessage(t, message)
}
}
}
if t == websocket.BinaryMessage {
s.melody.messageHandlerBinary(s, message)
}
func (s *Session) handleMessage(t int, message []byte) {
switch t {
case websocket.TextMessage:
s.melody.messageHandler(s, message)
case websocket.BinaryMessage:
s.melody.messageHandlerBinary(s, message)
}
}
@@ -149,7 +156,7 @@ func (s *Session) Write(msg []byte) error {
return ErrSessionClosed
}
s.writeMessage(&envelope{t: websocket.TextMessage, msg: msg})
s.writeMessage(envelope{t: websocket.TextMessage, msg: msg})
return nil
}
@@ -160,7 +167,7 @@ func (s *Session) WriteBinary(msg []byte) error {
return ErrSessionClosed
}
s.writeMessage(&envelope{t: websocket.BinaryMessage, msg: msg})
s.writeMessage(envelope{t: websocket.BinaryMessage, msg: msg})
return nil
}
@@ -171,7 +178,7 @@ func (s *Session) Close() error {
return ErrSessionClosed
}
s.writeMessage(&envelope{t: websocket.CloseMessage, msg: []byte{}})
s.writeMessage(envelope{t: websocket.CloseMessage, msg: []byte{}})
return nil
}
@@ -183,19 +190,19 @@ func (s *Session) CloseWithMsg(msg []byte) error {
return ErrSessionClosed
}
s.writeMessage(&envelope{t: websocket.CloseMessage, msg: msg})
s.writeMessage(envelope{t: websocket.CloseMessage, msg: msg})
return nil
}
// Set is used to store a new key/value pair exclusively for this session.
// It also lazy initializes s.Keys if it was not used previously.
func (s *Session) Set(key string, value interface{}) {
func (s *Session) Set(key string, value any) {
s.rwmutex.Lock()
defer s.rwmutex.Unlock()
if s.Keys == nil {
s.Keys = make(map[string]interface{})
s.Keys = make(map[string]any)
}
s.Keys[key] = value
@@ -203,7 +210,7 @@ func (s *Session) Set(key string, value interface{}) {
// Get returns the value for the given key, ie: (value, true).
// If the value does not exists it returns (nil, false)
func (s *Session) Get(key string) (value interface{}, exists bool) {
func (s *Session) Get(key string) (value any, exists bool) {
s.rwmutex.RLock()
defer s.rwmutex.RUnlock()
@@ -215,7 +222,7 @@ func (s *Session) Get(key string) (value interface{}, exists bool) {
}
// MustGet returns the value for the given key if it exists, otherwise it panics.
func (s *Session) MustGet(key string) interface{} {
func (s *Session) MustGet(key string) any {
if value, exists := s.Get(key); exists {
return value
}
@@ -246,3 +253,9 @@ func (s *Session) LocalAddr() net.Addr {
func (s *Session) RemoteAddr() net.Addr {
return s.conn.RemoteAddr()
}
// WebsocketConnection returns the underlying websocket connection.
// This can be used to e.g. set/read additional websocket options or to write sychronous messages.
func (s *Session) WebsocketConnection() *websocket.Conn {
return s.conn
}