Skip to content

Commit

Permalink
Replace deprecated nhooyr.io/websocket with github.com/coder/websocket (
Browse files Browse the repository at this point in the history
#301)

- Updated import statements to use github.com/coder/websocket
- Updated relevant function calls, type references, file names and dependency files
  • Loading branch information
Newtoniano authored Sep 16, 2024
1 parent 1c54c5a commit db57756
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.19
require (
cloud.google.com/go v0.99.0
github.com/RobinUS2/golang-moving-average v1.0.0
github.com/coder/websocket v1.8.12
github.com/mailru/easyjson v0.7.7
github.com/shopspring/decimal v1.3.1
github.com/stretchr/testify v1.7.0
github.com/vmihailenco/msgpack/v5 v5.3.0
nhooyr.io/websocket v1.8.10
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -561,8 +563,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
2 changes: 1 addition & 1 deletion marketdata/stream/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func isErrorIrrecoverableAtInit(err error) bool {
}

func isHTTP4xx(err error) bool {
// Unfortunately the nhoory error is a simple formatted string, created by fmt.Errorf,
// Unfortunately the coder/websocket error is a simple formatted string, created by fmt.Errorf,
// so the only check we can do is string matching
pattern := `expected handshake response status code 101 but got 4\d\d`
ok, _ := regexp.MatchString(pattern, err.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"net/url"
"time"

"nhooyr.io/websocket"
"github.com/coder/websocket"

"github.com/alpacahq/alpaca-trade-api-go/v3/alpaca"
)

type nhooyrWebsocketConn struct {
type coderWebsocketConn struct {
conn *websocket.Conn
msgType websocket.MessageType
}

// newNhooyrWebsocketConn creates a new nhooyr websocket connection
func newNhooyrWebsocketConn(ctx context.Context, u url.URL) (conn, error) {
// newCoderWebsocketConn creates a new coder websocket connection
func newCoderWebsocketConn(ctx context.Context, u url.URL) (conn, error) {
ctxWithTimeout, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
reqHeader := http.Header{}
Expand All @@ -35,33 +35,33 @@ func newNhooyrWebsocketConn(ctx context.Context, u url.URL) (conn, error) {
// Disable read limit: especially news messages can be huge.
conn.SetReadLimit(-1)

return &nhooyrWebsocketConn{
return &coderWebsocketConn{
conn: conn,
msgType: websocket.MessageBinary,
}, nil
}

// close closes the websocket connection
func (c *nhooyrWebsocketConn) close() error {
func (c *coderWebsocketConn) close() error {
return c.conn.Close(websocket.StatusNormalClosure, "")
}

// ping sends a ping to the client
func (c *nhooyrWebsocketConn) ping(ctx context.Context) error {
func (c *coderWebsocketConn) ping(ctx context.Context) error {
pingCtx, cancel := context.WithTimeout(ctx, pongWait)
defer cancel()

return c.conn.Ping(pingCtx)
}

// readMessage blocks until it reads a single message
func (c *nhooyrWebsocketConn) readMessage(ctx context.Context) (data []byte, err error) {
func (c *coderWebsocketConn) readMessage(ctx context.Context) (data []byte, err error) {
_, data, err = c.conn.Read(ctx)
return data, err
}

// writeMessage writes a single message
func (c *nhooyrWebsocketConn) writeMessage(ctx context.Context, data []byte) error {
func (c *coderWebsocketConn) writeMessage(ctx context.Context, data []byte) error {
writeCtx, cancel := context.WithTimeout(ctx, writeWait)
defer cancel()

Expand Down
8 changes: 4 additions & 4 deletions marketdata/stream/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func defaultStockOptions() *stockOptions {
cancelErrors: []string{},
corrections: []string{},
},
connCreator: newNhooyrWebsocketConn,
connCreator: newCoderWebsocketConn,
},
tradeHandler: func(t Trade) {},
quoteHandler: func(q Quote) {},
Expand Down Expand Up @@ -355,7 +355,7 @@ func defaultCryptoOptions() *cryptoOptions {
dailyBars: []string{},
orderbooks: []string{},
},
connCreator: newNhooyrWebsocketConn,
connCreator: newCoderWebsocketConn,
},
tradeHandler: func(t CryptoTrade) {},
quoteHandler: func(q CryptoQuote) {},
Expand Down Expand Up @@ -466,7 +466,7 @@ func defaultOptionOptions() *optionOptions {
updatedBars: []string{},
dailyBars: []string{},
},
connCreator: newNhooyrWebsocketConn,
connCreator: newCoderWebsocketConn,
},
tradeHandler: func(t OptionTrade) {},
quoteHandler: func(q OptionQuote) {},
Expand Down Expand Up @@ -530,7 +530,7 @@ func defaultNewsOptions() *newsOptions {
sub: subscriptions{
news: []string{},
},
connCreator: newNhooyrWebsocketConn,
connCreator: newCoderWebsocketConn,
},
newsHandler: func(n News) {},
}
Expand Down

0 comments on commit db57756

Please sign in to comment.