-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors conn.WebSocket into its own package and adds websocket dial options.
- Loading branch information
1 parent
ac91a54
commit 26d9f06
Showing
6 changed files
with
129 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package websocket | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/andydunstall/piko/pkg/conn" | ||
"github.com/gorilla/websocket" | ||
) | ||
|
||
// retryableStatusCodes contains a set of HTTP status codes that should be | ||
// retried. | ||
var retryableStatusCodes = map[int]struct{}{ | ||
http.StatusRequestTimeout: {}, | ||
http.StatusTooManyRequests: {}, | ||
http.StatusInternalServerError: {}, | ||
http.StatusBadGateway: {}, | ||
http.StatusServiceUnavailable: {}, | ||
http.StatusGatewayTimeout: {}, | ||
} | ||
|
||
type Options struct { | ||
token string | ||
} | ||
|
||
type Option interface { | ||
apply(*Options) | ||
} | ||
|
||
type tokenOption string | ||
|
||
func (o tokenOption) apply(opts *Options) { | ||
opts.token = string(o) | ||
} | ||
|
||
func WithToken(token string) Option { | ||
return tokenOption(token) | ||
} | ||
|
||
type Conn struct { | ||
wsConn *websocket.Conn | ||
} | ||
|
||
func NewConn(wsConn *websocket.Conn) *Conn { | ||
return &Conn{ | ||
wsConn: wsConn, | ||
} | ||
} | ||
|
||
func Dial(ctx context.Context, url string, opts ...Option) (*Conn, error) { | ||
options := Options{} | ||
for _, o := range opts { | ||
o.apply(&options) | ||
} | ||
|
||
header := make(http.Header) | ||
if options.token != "" { | ||
header.Set("Authorization", "Bearer "+options.token) | ||
} | ||
wsConn, resp, err := websocket.DefaultDialer.DialContext( | ||
ctx, url, header, | ||
) | ||
if err != nil { | ||
if resp != nil { | ||
if _, ok := retryableStatusCodes[resp.StatusCode]; ok { | ||
return nil, conn.NewRetryableError(err) | ||
} | ||
return nil, fmt.Errorf("%d: %w", resp.StatusCode, err) | ||
} | ||
return nil, conn.NewRetryableError(err) | ||
} | ||
return NewConn(wsConn), nil | ||
} | ||
|
||
func (c *Conn) ReadMessage() ([]byte, error) { | ||
mt, message, err := c.wsConn.ReadMessage() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if mt != websocket.BinaryMessage { | ||
return nil, fmt.Errorf("unexpected websocket message type: %d", mt) | ||
} | ||
return message, nil | ||
} | ||
|
||
func (c *Conn) NextReader() (io.Reader, error) { | ||
mt, r, err := c.wsConn.NextReader() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if mt != websocket.BinaryMessage { | ||
return nil, fmt.Errorf("unexpected websocket message type: %d", mt) | ||
} | ||
return r, nil | ||
} | ||
|
||
func (c *Conn) WriteMessage(b []byte) error { | ||
return c.wsConn.WriteMessage(websocket.BinaryMessage, b) | ||
} | ||
|
||
func (c *Conn) NextWriter() (io.WriteCloser, error) { | ||
return c.wsConn.NextWriter(websocket.BinaryMessage) | ||
} | ||
|
||
func (c *Conn) Addr() string { | ||
return c.wsConn.RemoteAddr().String() | ||
} | ||
|
||
func (c *Conn) Close() error { | ||
return c.wsConn.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters