Skip to content

Commit

Permalink
Return HTTP response on websocket connect failure (#200)
Browse files Browse the repository at this point in the history
* Return HTTP response if error connecting to websocket, in `WebsocketError` type.
  • Loading branch information
gammazero authored Sep 15, 2019
1 parent 1942ef1 commit ac3f658
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions transport/websocketpeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ type WebsocketConfig struct {
EnableRequestCapture bool `json:"enable_request_capture"`
}

// WebsocketError is returned on failure to connect to a websocket, and
// contains the http response if one is available.
type WebsocketError struct {
Err error
Response *http.Response
}

// Error returns a string describing the failure to connect to a websocket.
func (e *WebsocketError) Error() string {
if e.Response == nil {
return e.Err.Error()
}
return fmt.Sprintf("%s: %s", e.Err, e.Response.Status)
}

// websocketPeer implements the Peer interface, connecting the Send and Recv
// methods to a websocket.
type websocketPeer struct {
Expand All @@ -60,7 +75,7 @@ type websocketPeer struct {
}

const (
// WAMP uses the following WebSocket subprotocol identifiers for unbatched
// WAMP uses the following websocket subprotocol identifiers for unbatched
// modes:
jsonWebsocketProtocol = "wamp.2.json"
msgpackWebsocketProtocol = "wamp.2.msgpack"
Expand Down Expand Up @@ -95,8 +110,6 @@ func ConnectWebsocketPeerContext(ctx context.Context, routerURL string, serializ
protocol string
payloadType int
serializer serialize.Serializer
conn *websocket.Conn
err error
)

switch serialization {
Expand Down Expand Up @@ -125,8 +138,7 @@ func ConnectWebsocketPeerContext(ctx context.Context, routerURL string, serializ

if wsCfg != nil {
if wsCfg.ProxyURL != "" {
var proxyURL *url.URL
proxyURL, err = url.Parse(wsCfg.ProxyURL)
proxyURL, err := url.Parse(wsCfg.ProxyURL)
if err != nil {
return nil, err
}
Expand All @@ -136,9 +148,12 @@ func ConnectWebsocketPeerContext(ctx context.Context, routerURL string, serializ
dialer.EnableCompression = true
}

conn, _, err = dialer.DialContext(ctx, routerURL, nil)
conn, rsp, err := dialer.DialContext(ctx, routerURL, nil)
if err != nil {
return nil, err
return nil, &WebsocketError{
Err: err,
Response: rsp,
}
}
return NewWebsocketPeer(conn, serializer, payloadType, logger, 0, 0), nil
}
Expand All @@ -148,7 +163,7 @@ func ConnectWebsocketPeerContext(ctx context.Context, routerURL string, serializ
// servers to handle connections from clients.
//
// A non-zero keepAlive value configures a websocket "ping/pong" heartbeat,
// sendings websocket "pings" every keepAlive interval. If a "pong" response
// sending websocket "pings" every keepAlive interval. If a "pong" response
// is not received after 2 intervals have elapsed then the websocket is closed.
func NewWebsocketPeer(conn *websocket.Conn, serializer serialize.Serializer, payloadType int, logger stdlog.StdLog, keepAlive time.Duration, outQueueSize int) wamp.Peer {
w := &websocketPeer{
Expand Down

0 comments on commit ac3f658

Please sign in to comment.