Skip to content

Commit

Permalink
Revert "move socket manager"
Browse files Browse the repository at this point in the history
This reverts commit 423fd3f.
  • Loading branch information
maddalax committed Nov 13, 2024
1 parent b06d1b1 commit 971f05c
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions extensions/websocket/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
5 changes: 2 additions & 3 deletions extensions/websocket/internal/wsutil/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand All @@ -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{}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ws
package wsutil

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ws
package wsutil

import (
ws2 "github.com/maddalax/htmgo/extensions/websocket/opts"
Expand Down
5 changes: 3 additions & 2 deletions extensions/websocket/ws/access.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 2 additions & 1 deletion extensions/websocket/ws/dispatch.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion extensions/websocket/ws/every.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions extensions/websocket/ws/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions extensions/websocket/ws/listener.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion extensions/websocket/ws/metrics.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions extensions/websocket/ws/register.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand All @@ -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{}
Expand Down

0 comments on commit 971f05c

Please sign in to comment.