Skip to content

Commit

Permalink
Merge pull request #39 from thiagokokada/add-errcheck
Browse files Browse the repository at this point in the history
Add errcheck linter, fix issues
  • Loading branch information
thiagokokada authored Sep 12, 2024
2 parents 44fa8dc + e25cb6e commit fbd5013
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
- name: Staticcheck
run: go run honnef.co/go/tools/cmd/[email protected] ./...

- name: Errcheck
run: go run github.com/kisielk/[email protected] -ignoretests ./ ./event

- name: Build
run: go build -v ./...

Expand Down
18 changes: 12 additions & 6 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package event

import (
"context"
"errors"
"fmt"
"net"
"strings"
Expand Down Expand Up @@ -95,10 +96,8 @@ func (c *EventClient) Subscribe(ctx context.Context, ev EventHandler, events ...
}
}

func readWithContext(ctx context.Context, conn net.Conn, buf []byte) (int, error) {
func readWithContext(ctx context.Context, conn net.Conn, buf []byte) (n int, err error) {
done := make(chan struct{})
var n int
var err error

// Start a goroutine to perform the read
go func() {
Expand All @@ -111,12 +110,19 @@ func readWithContext(ctx context.Context, conn net.Conn, buf []byte) (int, error
return n, err
case <-ctx.Done():
// Set a short deadline to unblock the Read()
conn.SetReadDeadline(time.Now())
err = conn.SetReadDeadline(time.Now())
if err != nil {
return 0, err
}
// Reset read deadline
defer conn.SetReadDeadline(time.Time{})
defer func() {
if e := conn.SetReadDeadline(time.Time{}); e != nil {
err = errors.Join(err, e)
}
}()
// Make sure that the goroutine is done to avoid leaks
<-done
return 0, ctx.Err()
return 0, errors.Join(err, ctx.Err())
}
}

Expand Down
5 changes: 4 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, er
if err != nil {
return nil, fmt.Errorf("error while writing to socket: %w", err)
}
writer.Flush()
err = writer.Flush()
if err != nil {
return nil, fmt.Errorf("error while flushing to socket: %w", err)
}

// Get the response back
rbuf := bytes.NewBuffer(nil)
Expand Down

0 comments on commit fbd5013

Please sign in to comment.