Temporary websocket disconnect handling (#6)

* fix websocket close log error.

* logger session interface no pointer.

* websocket delayet disconnect.

* session host: save id not pointer to a session.

* fix if hostId not stored.
This commit is contained in:
Miroslav Šedivý
2022-08-26 20:16:40 +02:00
committed by GitHub
parent 5612b80634
commit 691150900b
9 changed files with 122 additions and 61 deletions

View File

@ -3,6 +3,7 @@ package session
import (
"errors"
"sync"
"sync/atomic"
"github.com/kataras/go-events"
"github.com/rs/zerolog"
@ -62,8 +63,7 @@ type SessionManagerCtx struct {
sessions map[string]*SessionCtx
sessionsMu sync.Mutex
host types.Session
hostMu sync.Mutex
hostId atomic.Value
cursors map[types.Session][]types.Cursor
cursorsMu sync.Mutex
@ -188,24 +188,33 @@ func (manager *SessionManagerCtx) List() []types.Session {
// ---
func (manager *SessionManagerCtx) SetHost(host types.Session) {
manager.hostMu.Lock()
manager.host = host
manager.hostMu.Unlock()
var hostId string
if host != nil {
hostId = host.ID()
}
manager.hostId.Store(hostId)
manager.emmiter.Emit("host_changed", host)
}
func (manager *SessionManagerCtx) GetHost() types.Session {
manager.hostMu.Lock()
defer manager.hostMu.Unlock()
func (manager *SessionManagerCtx) GetHost() (types.Session, bool) {
hostId, ok := manager.hostId.Load().(string)
if !ok || hostId == "" {
return nil, false
}
return manager.host
return manager.Get(hostId)
}
func (manager *SessionManagerCtx) ClearHost() {
manager.SetHost(nil)
}
func (manager *SessionManagerCtx) isHost(host types.Session) bool {
hostId, ok := manager.hostId.Load().(string)
return ok && hostId == host.ID()
}
// ---
// cursors
// ---