From 766692bfe637c321e5715d51ba2c447c7c91cb64 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 22 Jan 2022 10:16:56 -0700 Subject: [PATCH] websocket: add small buffer to listener channels --- router/websocket/listeners.go | 6 +++--- router/websocket/websocket.go | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/router/websocket/listeners.go b/router/websocket/listeners.go index aae8855..828e470 100644 --- a/router/websocket/listeners.go +++ b/router/websocket/listeners.go @@ -87,9 +87,9 @@ func (h *Handler) listenForServerEvents(ctx context.Context) error { ctx, cancel := context.WithCancel(ctx) defer cancel() - eventChan := make(chan events.Event) - logOutput := make(chan []byte) - installOutput := make(chan []byte) + eventChan := make(chan events.Event, 8) + logOutput := make(chan []byte, 8) + installOutput := make(chan []byte, 8) h.server.Events().On(eventChan, e...) h.server.LogSink().On(logOutput) h.server.InstallSink().On(installOutput) diff --git a/router/websocket/websocket.go b/router/websocket/websocket.go index 92548ef..6ac344d 100644 --- a/router/websocket/websocket.go +++ b/router/websocket/websocket.go @@ -267,11 +267,18 @@ func (h *Handler) setJwt(token *tokens.WebsocketPayload) { h.Unlock() } +var actions = map[server.PowerAction]string{ + server.PowerActionStart: PermissionSendPowerStart, + server.PowerActionStop: PermissionSendPowerStop, + server.PowerActionRestart: PermissionSendPowerRestart, + server.PowerActionTerminate: PermissionSendPowerStop, +} + // HandleInbound handles an inbound socket request and route it to the proper action. func (h *Handler) HandleInbound(ctx context.Context, m Message) error { if m.Event != AuthenticationEvent { if err := h.TokenValid(); err != nil { - h.unsafeSendJson(Message{ + _ = h.unsafeSendJson(Message{ Event: JwtErrorEvent, Args: []string{err.Error()}, }) @@ -339,12 +346,6 @@ func (h *Handler) HandleInbound(ctx context.Context, m Message) error { { action := server.PowerAction(strings.Join(m.Args, "")) - actions := make(map[server.PowerAction]string) - actions[server.PowerActionStart] = PermissionSendPowerStart - actions[server.PowerActionStop] = PermissionSendPowerStop - actions[server.PowerActionRestart] = PermissionSendPowerRestart - actions[server.PowerActionTerminate] = PermissionSendPowerStop - // Check that they have permission to perform this action if it is needed. if permission, exists := actions[action]; exists { if !h.GetJwt().HasPermission(permission) {