Skip to content

Commit a13d356

Browse files
author
Jenita
committed
feat: added timeout for kupo client
Signed-off-by: Jenita <jkawan@blinklabs.io>
1 parent a68f8c1 commit a13d356

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

input/chainsync/chainsync.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,15 @@ func getKupoClient(c *ChainSync) (*kugo.Client, error) {
509509
return c.kupoClient, nil
510510
}
511511

512-
// First validate the URL
512+
// Validate URL first
513513
_, err := url.ParseRequestURI(c.kupoUrl)
514514
if err != nil {
515515
return nil, fmt.Errorf("invalid kupo URL: %w", err)
516516
}
517517

518518
KugoCustomLogger := logging.NewKugoCustomLogger(logging.LevelInfo)
519519

520+
// Create client with timeout
520521
k := kugo.New(
521522
kugo.WithEndpoint(c.kupoUrl),
522523
kugo.WithLogger(KugoCustomLogger),
@@ -528,29 +529,29 @@ func getKupoClient(c *ChainSync) (*kugo.Client, error) {
528529
}
529530

530531
healthUrl := strings.TrimRight(c.kupoUrl, "/") + "/health"
531-
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
532+
533+
// Create context with timeout
534+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
535+
defer cancel()
536+
537+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthUrl, nil)
532538
if err != nil {
533539
return nil, fmt.Errorf("failed to create health check request: %w", err)
534540
}
535541

536542
resp, err := httpClient.Do(req)
537543
if err != nil {
538-
var urlErr *url.Error
539-
if errors.As(err, &urlErr) {
540-
if urlErr.Timeout() {
541-
return nil, fmt.Errorf("kupo health check timed out after 2 seconds")
542-
}
543-
if strings.Contains(err.Error(), "no such host") {
544-
return nil, fmt.Errorf("failed to connect to kupo: %w", err)
545-
}
544+
// Handle different error types
545+
switch {
546+
case errors.Is(err, context.DeadlineExceeded):
547+
return nil, fmt.Errorf("kupo health check timed out after 3 seconds")
548+
case strings.Contains(err.Error(), "no such host"):
549+
return nil, fmt.Errorf("failed to resolve kupo host: %w", err)
550+
default:
551+
return nil, fmt.Errorf("failed to perform health check: %w", err)
546552
}
547-
return nil, fmt.Errorf("failed to perform health check: %w", err)
548553
}
549-
defer func() {
550-
if resp != nil && resp.Body != nil {
551-
resp.Body.Close()
552-
}
553-
}()
554+
defer resp.Body.Close()
554555

555556
if resp.StatusCode != http.StatusOK {
556557
return nil, fmt.Errorf("health check failed with status code: %d", resp.StatusCode)

input/chainsync/chainsync_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package chainsync
22

33
import (
44
"encoding/hex"
5+
//"fmt"
56
"net/http"
67
"net/http/httptest"
78
"strings"
@@ -110,7 +111,7 @@ func TestGetKupoClient(t *testing.T) {
110111

111112
t.Run("health check timeout", func(t *testing.T) {
112113
slowTS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
113-
time.Sleep(3 * time.Second)
114+
time.Sleep(4 * time.Second) // Longer than the 3s context timeout
114115
w.WriteHeader(http.StatusOK)
115116
}))
116117
defer slowTS.Close()
@@ -121,7 +122,7 @@ func TestGetKupoClient(t *testing.T) {
121122

122123
_, err := getKupoClient(c)
123124
require.Error(t, err)
124-
assert.Contains(t, err.Error(), "kupo health check timed out after 2 seconds")
125+
assert.Contains(t, err.Error(), "kupo health check timed out after 3 seconds")
125126
})
126127

127128
t.Run("failed health check status", func(t *testing.T) {
@@ -156,10 +157,9 @@ func TestGetKupoClient(t *testing.T) {
156157

157158
_, err := getKupoClient(c)
158159
require.Error(t, err)
159-
// Accept either timeout or connection error
160160
assert.True(t,
161-
strings.Contains(err.Error(), "kupo health check timed out") ||
162-
strings.Contains(err.Error(), "failed to connect"),
161+
strings.Contains(err.Error(), "failed to resolve kupo host") ||
162+
strings.Contains(err.Error(), "failed to perform health check"),
163163
"unexpected error: %v", err)
164164
})
165165
}

0 commit comments

Comments
 (0)