1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-02 11:52:56 +00:00

Compare commits

...

3 commits

Author SHA1 Message Date
diamondburned abd5bba9c3 CI: Fixed typo
My high level of competence is very arguable.
2020-10-31 17:29:59 -07:00
diamondburned a46d3c2c8d wsutil: Added possibly missing Reset cases and more debug logs 2020-10-31 15:54:51 -07:00
diamondburned bbd4e57cb1 CI: Fixed invalid unit test command 2020-10-31 12:05:11 -07:00
4 changed files with 15 additions and 2 deletions

View file

@ -30,7 +30,7 @@
"variables": [ "$BOT_TOKEN" ]
},
"script": [
"go test -coverprofile -race -v $COV ./...",
"go test -coverprofile $COV -race -v ./...",
"go tool cover -func $COV"
]
},

View file

@ -325,6 +325,8 @@ func (g *Gateway) start(ctx context.Context) error {
return errors.Wrap(ctx.Err(), "failed to wait for Hello event")
}
wsutil.WSDebug("Hello received; duration:", hello.HeartbeatInterval)
// Send Discord either the Identify packet (if it's a fresh connection), or
// a Resume packet (if it's a dead connection).
if g.SessionID == "" {

View file

@ -91,6 +91,9 @@ func (c *Conn) Dial(ctx context.Context, addr string) (err error) {
return errors.Wrap(err, "failed to dial WS")
}
// Reset the deadline.
c.Conn.SetWriteDeadline(resetDeadline)
c.events = make(chan Event, WSBuffer)
go startReadLoop(c.Conn, c.events)
@ -113,7 +116,6 @@ func (c *Conn) Send(ctx context.Context, b []byte) error {
defer c.Conn.SetWriteDeadline(resetDeadline)
}
// We need to clean up ourselves if things are erroring out.
if err := c.Conn.WriteMessage(websocket.TextMessage, b); err != nil {
return err
}
@ -122,6 +124,8 @@ func (c *Conn) Send(ctx context.Context, b []byte) error {
}
func (c *Conn) Close() error {
WSDebug("Conn: Close is called; shutting down the Websocket connection.")
// Have a deadline before closing.
var deadline = time.Now().Add(5 * time.Second)
c.Conn.SetWriteDeadline(deadline)
@ -129,6 +133,8 @@ func (c *Conn) Close() error {
// Close the WS.
err := c.Conn.Close()
c.Conn.SetWriteDeadline(resetDeadline)
WSDebug("Conn: Websocket closed; error:", err)
WSDebug("Conn: Flusing events...")
@ -162,6 +168,8 @@ func startReadLoop(conn *websocket.Conn, eventCh chan<- Event) {
for {
b, err := state.handle()
if err != nil {
WSDebug("Conn: Read error:", err)
// Is the error an EOF?
if errors.Is(err, io.EOF) {
// Yes it is, exit.

View file

@ -135,6 +135,8 @@ func (ws *Websocket) SendCtx(ctx context.Context, b []byte) error {
}
if err := ws.conn.Send(ctx, b); err != nil {
// We need to clean up ourselves if things are erroring out.
WSDebug("Conn: Error while sending; closing the connection. Error:", err)
ws.close()
return err
}
@ -160,6 +162,7 @@ func (ws *Websocket) Close() error {
// more information.
func (ws *Websocket) close() error {
if ws.closed {
WSDebug("Conn: Websocket is already closed.")
return ErrWebsocketClosed
}