From 971f05c0051085f0bd70f80e872da54f5d04826f Mon Sep 17 00:00:00 2001 From: maddalax Date: Tue, 12 Nov 2024 18:16:20 -0600 Subject: [PATCH] Revert "move socket manager" This reverts commit 423fd3f42985c9ff3b3734043738ab507c2821e6. --- extensions/websocket/init.go | 4 ++-- extensions/websocket/internal/wsutil/handler.go | 5 ++--- extensions/websocket/{ws => internal/wsutil}/manager.go | 2 +- .../websocket/{ws => internal/wsutil}/manager_test.go | 2 +- extensions/websocket/ws/access.go | 5 +++-- extensions/websocket/ws/dispatch.go | 3 ++- extensions/websocket/ws/every.go | 3 ++- extensions/websocket/ws/handler.go | 7 ++++--- extensions/websocket/ws/listener.go | 7 ++++--- extensions/websocket/ws/metrics.go | 3 ++- extensions/websocket/ws/register.go | 7 ++++--- 11 files changed, 27 insertions(+), 21 deletions(-) rename extensions/websocket/{ws => internal/wsutil}/manager.go (99%) rename extensions/websocket/{ws => internal/wsutil}/manager_test.go (99%) diff --git a/extensions/websocket/init.go b/extensions/websocket/init.go index 980691e..9261063 100644 --- a/extensions/websocket/init.go +++ b/extensions/websocket/init.go @@ -21,8 +21,8 @@ func EnableExtension(app *h.App, opts opts.ExtensionOpts) { panic("websocket: SessionId func is required") } - service.Set[ws.SocketManager](app.Opts.ServiceLocator, service.Singleton, func() *ws.SocketManager { - manager := ws.NewSocketManager(&opts) + service.Set[wsutil.SocketManager](app.Opts.ServiceLocator, service.Singleton, func() *wsutil.SocketManager { + manager := wsutil.NewSocketManager(&opts) manager.StartMetrics() return manager }) diff --git a/extensions/websocket/internal/wsutil/handler.go b/extensions/websocket/internal/wsutil/handler.go index 0b320b9..1096dd5 100644 --- a/extensions/websocket/internal/wsutil/handler.go +++ b/extensions/websocket/internal/wsutil/handler.go @@ -6,7 +6,6 @@ import ( "github.com/gobwas/ws" "github.com/gobwas/ws/wsutil" ws2 "github.com/maddalax/htmgo/extensions/websocket/opts" - ws3 "github.com/maddalax/htmgo/extensions/websocket/ws" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" "log/slog" @@ -26,7 +25,7 @@ func WsHttpHandler(opts *ws2.ExtensionOpts) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { cc := r.Context().Value(h.RequestContextKey).(*h.RequestContext) locator := cc.ServiceLocator() - manager := service.Get[ws3.SocketManager](locator) + manager := service.Get[SocketManager](locator) sessionId := opts.SessionId(cc) @@ -47,7 +46,7 @@ func WsHttpHandler(opts *ws2.ExtensionOpts) http.HandlerFunc { we don't want to block the writer */ done := make(chan bool, 1000) - writer := make(ws3.WriterChan, 1000) + writer := make(WriterChan, 1000) wg := sync.WaitGroup{} diff --git a/extensions/websocket/ws/manager.go b/extensions/websocket/internal/wsutil/manager.go similarity index 99% rename from extensions/websocket/ws/manager.go rename to extensions/websocket/internal/wsutil/manager.go index 6fd6db6..d274fe1 100644 --- a/extensions/websocket/ws/manager.go +++ b/extensions/websocket/internal/wsutil/manager.go @@ -1,4 +1,4 @@ -package ws +package wsutil import ( "fmt" diff --git a/extensions/websocket/ws/manager_test.go b/extensions/websocket/internal/wsutil/manager_test.go similarity index 99% rename from extensions/websocket/ws/manager_test.go rename to extensions/websocket/internal/wsutil/manager_test.go index 3c05a6f..0ba95d4 100644 --- a/extensions/websocket/ws/manager_test.go +++ b/extensions/websocket/internal/wsutil/manager_test.go @@ -1,4 +1,4 @@ -package ws +package wsutil import ( ws2 "github.com/maddalax/htmgo/extensions/websocket/opts" diff --git a/extensions/websocket/ws/access.go b/extensions/websocket/ws/access.go index 012d859..efa63c5 100644 --- a/extensions/websocket/ws/access.go +++ b/extensions/websocket/ws/access.go @@ -1,9 +1,10 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/framework/h" ) -func ManagerFromCtx(ctx *h.RequestContext) *SocketManager { - return SocketManagerFromCtx(ctx) +func ManagerFromCtx(ctx *h.RequestContext) *wsutil.SocketManager { + return wsutil.SocketManagerFromCtx(ctx) } diff --git a/extensions/websocket/ws/dispatch.go b/extensions/websocket/ws/dispatch.go index 53ed0f3..248b649 100644 --- a/extensions/websocket/ws/dispatch.go +++ b/extensions/websocket/ws/dispatch.go @@ -1,6 +1,7 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" @@ -32,7 +33,7 @@ func PushElement(data HandlerData, el *h.Element) bool { // PushElementCtx sends an element to the current session and swaps it into the page func PushElementCtx(ctx *h.RequestContext, el *h.Element) bool { locator := ctx.ServiceLocator() - socketManager := service.Get[SocketManager](locator) + socketManager := service.Get[wsutil.SocketManager](locator) socketId := session.GetSessionId(ctx) socket := socketManager.Get(string(socketId)) if socket == nil { diff --git a/extensions/websocket/ws/every.go b/extensions/websocket/ws/every.go index d549c53..679d3d5 100644 --- a/extensions/websocket/ws/every.go +++ b/extensions/websocket/ws/every.go @@ -1,6 +1,7 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" @@ -11,7 +12,7 @@ import ( func Every(ctx *h.RequestContext, interval time.Duration, cb func() bool) { socketId := session.GetSessionId(ctx) locator := ctx.ServiceLocator() - manager := service.Get[SocketManager](locator) + manager := service.Get[wsutil.SocketManager](locator) manager.RunIntervalWithSocket(string(socketId), interval, cb) } diff --git a/extensions/websocket/ws/handler.go b/extensions/websocket/ws/handler.go index 07c3080..2d0bbb7 100644 --- a/extensions/websocket/ws/handler.go +++ b/extensions/websocket/ws/handler.go @@ -2,15 +2,16 @@ package ws import ( "fmt" + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "sync" ) type MessageHandler struct { - manager *SocketManager + manager *wsutil.SocketManager } -func NewMessageHandler(manager *SocketManager) *MessageHandler { +func NewMessageHandler(manager *wsutil.SocketManager) *MessageHandler { return &MessageHandler{manager: manager} } @@ -76,7 +77,7 @@ func (h *MessageHandler) OnDomElementRemoved(handlerId string) { handlers.Delete(handlerId) } -func (h *MessageHandler) OnSocketDisconnected(event SocketEvent) { +func (h *MessageHandler) OnSocketDisconnected(event wsutil.SocketEvent) { sessionId := session.Id(event.SessionId) hashes, ok := sessionIdToHashes.Load(sessionId) if ok { diff --git a/extensions/websocket/ws/listener.go b/extensions/websocket/ws/listener.go index 9b4965b..5556331 100644 --- a/extensions/websocket/ws/listener.go +++ b/extensions/websocket/ws/listener.go @@ -1,12 +1,13 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/framework/service" ) func StartListener(locator *service.Locator) { - manager := service.Get[SocketManager](locator) + manager := service.Get[wsutil.SocketManager](locator) manager.Listen(socketMessageListener) handler := NewMessageHandler(manager) go func() { @@ -22,9 +23,9 @@ func handle(handler *MessageHandler) { handler.OnServerSideEvent(event) case event := <-socketMessageListener: switch event.Type { - case DisconnectedEvent: + case wsutil.DisconnectedEvent: handler.OnSocketDisconnected(event) - case MessageEvent: + case wsutil.MessageEvent: handlerId, ok := event.Payload["id"].(string) eventName, ok2 := event.Payload["event"].(string) diff --git a/extensions/websocket/ws/metrics.go b/extensions/websocket/ws/metrics.go index 29bbe79..258493b 100644 --- a/extensions/websocket/ws/metrics.go +++ b/extensions/websocket/ws/metrics.go @@ -1,11 +1,12 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/framework/h" ) type Metrics struct { - Manager ManagerMetrics + Manager wsutil.ManagerMetrics Handler HandlerMetrics } diff --git a/extensions/websocket/ws/register.go b/extensions/websocket/ws/register.go index d24d0d1..b7350fd 100644 --- a/extensions/websocket/ws/register.go +++ b/extensions/websocket/ws/register.go @@ -1,6 +1,7 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/framework/h" "github.com/puzpuzpuz/xsync/v3" @@ -10,8 +11,8 @@ import ( type HandlerData struct { SessionId session.Id - Socket *SocketConnection - Manager *SocketManager + Socket *wsutil.SocketConnection + Manager *wsutil.SocketManager } type Handler func(data HandlerData) @@ -28,7 +29,7 @@ var sessionIdToHashes = xsync.NewMapOf[session.Id, map[KeyHash]bool]() var hashesToSessionId = xsync.NewMapOf[KeyHash, session.Id]() var serverEventNamesToHash = xsync.NewMapOf[string, map[KeyHash]bool]() -var socketMessageListener = make(chan SocketEvent, 100) +var socketMessageListener = make(chan wsutil.SocketEvent, 100) var serverSideMessageListener = make(chan ServerSideEvent, 100) var lock = sync.Mutex{} var callingHandler = atomic.Bool{}