Skip to content

Commit

Permalink
catch specific errors on websocket read
Browse files Browse the repository at this point in the history
  • Loading branch information
kian99 committed Sep 4, 2024
1 parent 22edc4c commit 914ef05
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions internal/rpc/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/gorilla/websocket"
"github.com/juju/juju/rpc/params"
"github.com/juju/names/v5"
"github.com/juju/zaputil/zapctx"
Expand Down Expand Up @@ -312,6 +313,15 @@ func (p *modelProxy) auditLogMessage(msg *message, isResponse bool) error {
return nil
}

func unexpectedReadError(err error) bool {
closeError := websocket.IsUnexpectedCloseError(err,
websocket.CloseNormalClosure,
websocket.CloseNoStatusReceived,
websocket.CloseAbnormalClosure)
_, unmarshalError := err.(*json.InvalidUnmarshalError)
return closeError || unmarshalError
}

// clientProxy proxies messages from client->controller.
type clientProxy struct {
modelProxy
Expand All @@ -332,7 +342,10 @@ func (p *clientProxy) start(ctx context.Context) error {
zapctx.Debug(ctx, "Reading on client connection")
msg := new(message)
if err := p.src.readJson(&msg); err != nil {
//lint:ignore nilerr an error reading on the socket implies the client closed their connection, return without error.
if unexpectedReadError(err) {
zapctx.Error(ctx, "unexpected client read error", zap.Error(err))
return err
}
return nil
}
zapctx.Debug(ctx, "Read message from client", zap.Any("message", msg))
Expand Down Expand Up @@ -423,7 +436,10 @@ func (p *controllerProxy) start(ctx context.Context) error {
zapctx.Debug(ctx, "Reading on controller connection")
msg := new(message)
if err := p.src.readJson(msg); err != nil {
//lint:ignore nilerr an error reading on the socket implies we've closed the connection to the controller, return without error.
if unexpectedReadError(err) {
zapctx.Error(ctx, "unexpected controller read error", zap.Error(err))
return err
}
return nil
}
zapctx.Debug(ctx, "Received message from controller", zap.Any("Message", msg))
Expand Down

0 comments on commit 914ef05

Please sign in to comment.