Skip to content

Commit

Permalink
transports/websocket: Rewrite from scratch
Browse files Browse the repository at this point in the history
Use github.com/sourcegraph/jsonrpc2 and github.com/gorilla/websocket.

The transport package should be more robust now. The code is much less
messy and it uses up-to-date packages.

The transport API is also changes. You can now pass multiple WebSocket
endpoint URLs and the auto-reconnect logic will use round-robin to
rotate between the URLs provided.
  • Loading branch information
tchap committed Sep 19, 2017
1 parent 79c5181 commit f4bba9c
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 385 deletions.
4 changes: 1 addition & 3 deletions transports/websocket/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ package websocket

import "errors"

var (
ErrClosing = errors.New("closing")
)
var ErrClosing = errors.New("closing")
12 changes: 0 additions & 12 deletions transports/websocket/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package websocket

import (
"fmt"
"time"
)

// ConnectingEvent is emitted when a new connection is being established.
Expand Down Expand Up @@ -32,14 +31,3 @@ type DisconnectedEvent struct {
func (e *DisconnectedEvent) String() string {
return fmt.Sprintf("DISCONNECTED [url=%v, err=%v]", e.URL, e.Err)
}

// DialTimeoutEvent is emitted when establishing a new connection times out.
type DialTimeoutEvent struct {
URL string
Err error
Timeout time.Duration
}

func (e *DialTimeoutEvent) String() string {
return fmt.Sprintf("DIAL_TIMEOUT [url=%v, err=%v, timeout=%v]", e.URL, e.Err, e.Timeout)
}
36 changes: 36 additions & 0 deletions transports/websocket/object_stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package websocket

import (
"time"

"github.com/gorilla/websocket"
jsonrpc2websocket "github.com/sourcegraph/jsonrpc2/websocket"
)

// ObjectStream implements jsonrpc2.ObjectStream that uses a WebSocket.
// It extends jsonrpc2/websocket.ObjectStream with read/write timeouts.
type ObjectStream struct {
conn *websocket.Conn
stream jsonrpc2websocket.ObjectStream

writeTimeout time.Duration
readTimeout time.Duration
}

func NewObjectStream(conn *websocket.Conn, writeTimeout, readTimeout time.Duration) *ObjectStream {
return &ObjectStream{conn, jsonrpc2websocket.NewObjectStream(conn), writeTimeout, readTimeout}
}

func (stream *ObjectStream) WriteObject(v interface{}) error {
stream.conn.SetWriteDeadline(time.Now().Add(stream.writeTimeout))
return stream.stream.WriteObject(v)
}

func (stream *ObjectStream) ReadObject(v interface{}) error {
stream.conn.SetReadDeadline(time.Now().Add(stream.readTimeout))
return stream.stream.ReadObject(v)
}

func (stream *ObjectStream) Close() error {
return stream.stream.Close()
}
Loading

0 comments on commit f4bba9c

Please sign in to comment.