Skip to content

Commit 83dc52b

Browse files
committed
fix: address golangci-lint errcheck and bodyclose issues
- Close resp.Body in ws/client.go Connect method - Check error returns for all Close() calls - Check error returns for SetReadDeadline/SetWriteDeadline - Use assignOp for delay *= 2 (gocritic)
1 parent 223fb96 commit 83dc52b

5 files changed

Lines changed: 35 additions & 23 deletions

File tree

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func runRunner() error {
153153
}
154154
}
155155

156-
client.Close()
156+
_ = client.Close()
157157
}()
158158

159159
// Run with reconnection

workspace/webfetch.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func (w *Workspace) WebFetch(ctx context.Context, args *protocol.WebFetchArgs) (
4444
if err != nil {
4545
return nil, err
4646
}
47-
defer resp.Body.Close()
47+
defer func() {
48+
_ = resp.Body.Close()
49+
}()
4850

4951
body, err := readResponseBody(resp)
5052
if err != nil {

workspace/workspace.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ func (w *Workspace) readFileContent(path string, size, offset, limit int64) (*pr
119119
if err != nil {
120120
return nil, fmt.Errorf("failed to open file: %w", err)
121121
}
122-
defer file.Close()
122+
defer func() {
123+
_ = file.Close()
124+
}()
123125

124126
if offset < 0 {
125127
offset = 0
@@ -337,7 +339,7 @@ func (w *Workspace) grepWithGo(ctx context.Context, args *protocol.GrepArgs) (*p
337339
}
338340
lineNum++
339341
}
340-
file.Close()
342+
_ = file.Close()
341343
}
342344
}
343345
return &protocol.GrepResult{Matches: results}, nil
@@ -618,13 +620,17 @@ func (w *Workspace) extractZipFile(f *zip.File, targetPath string) error {
618620
if err != nil {
619621
return err
620622
}
621-
defer rc.Close()
623+
defer func() {
624+
_ = rc.Close()
625+
}()
622626

623627
dst, err := os.OpenFile(targetPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
624628
if err != nil {
625629
return err
626630
}
627-
defer dst.Close()
631+
defer func() {
632+
_ = dst.Close()
633+
}()
628634

629635
_, err = io.Copy(dst, rc)
630636
return err

workspace/workspace_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ func TestWorkspace_List(t *testing.T) {
9090
ctx := context.Background()
9191

9292
// Create test structure
93-
os.MkdirAll(filepath.Join(ws.Root(), "dir1"), 0o755)
94-
os.MkdirAll(filepath.Join(ws.Root(), "dir2"), 0o755)
95-
os.WriteFile(filepath.Join(ws.Root(), "file1.txt"), []byte("content"), 0o644)
96-
os.WriteFile(filepath.Join(ws.Root(), "dir1", "file2.txt"), []byte("content"), 0o644)
93+
_ = os.MkdirAll(filepath.Join(ws.Root(), "dir1"), 0o755)
94+
_ = os.MkdirAll(filepath.Join(ws.Root(), "dir2"), 0o755)
95+
_ = os.WriteFile(filepath.Join(ws.Root(), "file1.txt"), []byte("content"), 0o644)
96+
_ = os.WriteFile(filepath.Join(ws.Root(), "dir1", "file2.txt"), []byte("content"), 0o644)
9797

9898
// List non-recursive
9999
result, err := ws.List(ctx, &protocol.ListArgs{Path: ".", Recursive: false})
@@ -111,9 +111,9 @@ func TestWorkspace_Glob(t *testing.T) {
111111
ctx := context.Background()
112112

113113
// Create test files
114-
os.WriteFile(filepath.Join(ws.Root(), "file1.txt"), []byte("content"), 0o644)
115-
os.WriteFile(filepath.Join(ws.Root(), "file2.txt"), []byte("content"), 0o644)
116-
os.WriteFile(filepath.Join(ws.Root(), "file3.log"), []byte("content"), 0o644)
114+
_ = os.WriteFile(filepath.Join(ws.Root(), "file1.txt"), []byte("content"), 0o644)
115+
_ = os.WriteFile(filepath.Join(ws.Root(), "file2.txt"), []byte("content"), 0o644)
116+
_ = os.WriteFile(filepath.Join(ws.Root(), "file3.log"), []byte("content"), 0o644)
117117

118118
result, err := ws.Glob(ctx, &protocol.GlobArgs{Pattern: "*.txt"})
119119
require.NoError(t, err)
@@ -127,8 +127,8 @@ func TestWorkspace_Grep(t *testing.T) {
127127
ctx := context.Background()
128128

129129
// Create test files
130-
os.WriteFile(filepath.Join(ws.Root(), "file1.txt"), []byte("hello world\nfoo bar\nhello again"), 0o644)
131-
os.WriteFile(filepath.Join(ws.Root(), "file2.txt"), []byte("no match here"), 0o644)
130+
_ = os.WriteFile(filepath.Join(ws.Root(), "file1.txt"), []byte("hello world\nfoo bar\nhello again"), 0o644)
131+
_ = os.WriteFile(filepath.Join(ws.Root(), "file2.txt"), []byte("no match here"), 0o644)
132132

133133
result, err := ws.Grep(ctx, &protocol.GrepArgs{Pattern: "hello"})
134134
require.NoError(t, err)

ws/client.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (c *Client) Connect(ctx context.Context) error {
9393
conn, resp, err := websocket.DefaultDialer.DialContext(ctx, c.cfg.APIURL, header)
9494
if err != nil {
9595
if resp != nil {
96+
_ = resp.Body.Close()
9697
return fmt.Errorf("failed to connect: %w (status: %d)", err, resp.StatusCode)
9798
}
9899
return fmt.Errorf("failed to connect: %w", err)
@@ -101,6 +102,7 @@ func (c *Client) Connect(ctx context.Context) error {
101102
// Get worknode ID from response header
102103
if resp != nil {
103104
c.worknodeID = resp.Header.Get("X-Worknode-ID")
105+
_ = resp.Body.Close()
104106
}
105107

106108
c.mu.Lock()
@@ -130,8 +132,10 @@ func (c *Client) readWelcomeMessage() error {
130132
}
131133

132134
// Set a short deadline for the welcome message
133-
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
134-
defer conn.SetReadDeadline(time.Time{})
135+
_ = conn.SetReadDeadline(time.Now().Add(5 * time.Second))
136+
defer func() {
137+
_ = conn.SetReadDeadline(time.Time{})
138+
}()
135139

136140
_, data, err := conn.ReadMessage()
137141
if err != nil {
@@ -201,7 +205,7 @@ func (c *Client) RunWithReconnect(ctx context.Context) error {
201205
}
202206

203207
// Exponential backoff
204-
delay = delay * 2
208+
delay *= 2
205209
if delay > maxReconnectDelay {
206210
delay = maxReconnectDelay
207211
}
@@ -287,14 +291,14 @@ func (c *Client) readLoop(ctx context.Context) error {
287291
return fmt.Errorf("not connected")
288292
}
289293

290-
conn.SetReadDeadline(time.Now().Add(pongWait))
294+
_ = conn.SetReadDeadline(time.Now().Add(pongWait))
291295
conn.SetPongHandler(func(string) error {
292-
conn.SetReadDeadline(time.Now().Add(pongWait))
296+
_ = conn.SetReadDeadline(time.Now().Add(pongWait))
293297
return nil
294298
})
295299
// Handle ping messages from server - must reply with pong and reset deadline
296300
conn.SetPingHandler(func(appData string) error {
297-
conn.SetReadDeadline(time.Now().Add(pongWait))
301+
_ = conn.SetReadDeadline(time.Now().Add(pongWait))
298302
// Must reply with pong - WriteControl is safe to call from handler
299303
return conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(writeWait))
300304
})
@@ -314,7 +318,7 @@ func (c *Client) readLoop(ctx context.Context) error {
314318
}
315319

316320
// Reset deadline on any message received
317-
conn.SetReadDeadline(time.Now().Add(pongWait))
321+
_ = conn.SetReadDeadline(time.Now().Add(pongWait))
318322

319323
var msg protocol.Message
320324
if err := json.Unmarshal(data, &msg); err != nil {
@@ -354,7 +358,7 @@ func (c *Client) sendLoop(ctx context.Context) {
354358
continue
355359
}
356360

357-
conn.SetWriteDeadline(time.Now().Add(writeWait))
361+
_ = conn.SetWriteDeadline(time.Now().Add(writeWait))
358362
data, err := json.Marshal(msg)
359363
if err != nil {
360364
slog.Error("failed to marshal message",

0 commit comments

Comments
 (0)