websocket: add small buffer to listener channels

This commit is contained in:
Matthew Penner 2022-01-22 10:16:56 -07:00
parent 764aed89ae
commit 766692bfe6
No known key found for this signature in database
GPG Key ID: 31311906AD4CF6D6
2 changed files with 11 additions and 10 deletions

View File

@ -87,9 +87,9 @@ func (h *Handler) listenForServerEvents(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
eventChan := make(chan events.Event) eventChan := make(chan events.Event, 8)
logOutput := make(chan []byte) logOutput := make(chan []byte, 8)
installOutput := make(chan []byte) installOutput := make(chan []byte, 8)
h.server.Events().On(eventChan, e...) h.server.Events().On(eventChan, e...)
h.server.LogSink().On(logOutput) h.server.LogSink().On(logOutput)
h.server.InstallSink().On(installOutput) h.server.InstallSink().On(installOutput)

View File

@ -267,11 +267,18 @@ func (h *Handler) setJwt(token *tokens.WebsocketPayload) {
h.Unlock() 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. // HandleInbound handles an inbound socket request and route it to the proper action.
func (h *Handler) HandleInbound(ctx context.Context, m Message) error { func (h *Handler) HandleInbound(ctx context.Context, m Message) error {
if m.Event != AuthenticationEvent { if m.Event != AuthenticationEvent {
if err := h.TokenValid(); err != nil { if err := h.TokenValid(); err != nil {
h.unsafeSendJson(Message{ _ = h.unsafeSendJson(Message{
Event: JwtErrorEvent, Event: JwtErrorEvent,
Args: []string{err.Error()}, 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, "")) 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. // Check that they have permission to perform this action if it is needed.
if permission, exists := actions[action]; exists { if permission, exists := actions[action]; exists {
if !h.GetJwt().HasPermission(permission) { if !h.GetJwt().HasPermission(permission) {