refactor(http): move retry logic to utils package
Extract HTTP retry functionality from skills package to utils for better reusability Add context-aware sleep function and comprehensive tests
This commit is contained in:
parent
35c1c4bfa4
commit
1aa556829d
3 changed files with 75 additions and 56 deletions
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/sipeed/picoclaw/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SkillInstaller struct {
|
type SkillInstaller struct {
|
||||||
|
|
@ -23,39 +25,6 @@ type AvailableSkill struct {
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxRetries = 3
|
|
||||||
|
|
||||||
func shouldRetry(statusCode int) bool {
|
|
||||||
return statusCode == http.StatusTooManyRequests ||
|
|
||||||
statusCode >= 500
|
|
||||||
}
|
|
||||||
|
|
||||||
func doRequestWithRetry(client *http.Client, req *http.Request) (*http.Response, error) {
|
|
||||||
var resp *http.Response
|
|
||||||
var err error
|
|
||||||
|
|
||||||
for i := range maxRetries {
|
|
||||||
if i > 0 && resp != nil {
|
|
||||||
resp.Body.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err = client.Do(req)
|
|
||||||
if err == nil {
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if !shouldRetry(resp.StatusCode) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if i < maxRetries-1 {
|
|
||||||
time.Sleep(time.Second * time.Duration(i+1))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resp, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSkillInstaller(workspace string) *SkillInstaller {
|
func NewSkillInstaller(workspace string) *SkillInstaller {
|
||||||
return &SkillInstaller{
|
return &SkillInstaller{
|
||||||
workspace: workspace,
|
workspace: workspace,
|
||||||
|
|
@ -77,7 +46,7 @@ func (si *SkillInstaller) InstallFromGitHub(ctx context.Context, repo string) er
|
||||||
return fmt.Errorf("failed to create request: %w", err)
|
return fmt.Errorf("failed to create request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := doRequestWithRetry(client, req)
|
resp, err := utils.DoRequestWithRetry(client, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to fetch skill: %w", err)
|
return fmt.Errorf("failed to fetch skill: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -127,7 +96,7 @@ func (si *SkillInstaller) ListAvailableSkills(ctx context.Context) ([]AvailableS
|
||||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := doRequestWithRetry(client, req)
|
resp, err := utils.DoRequestWithRetry(client, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to fetch skills list: %w", err)
|
return nil, fmt.Errorf("failed to fetch skills list: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
55
pkg/utils/http_retry.go
Normal file
55
pkg/utils/http_retry.go
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const maxRetries = 3
|
||||||
|
|
||||||
|
func shouldRetry(statusCode int) bool {
|
||||||
|
return statusCode == http.StatusTooManyRequests ||
|
||||||
|
statusCode >= 500
|
||||||
|
}
|
||||||
|
|
||||||
|
func DoRequestWithRetry(client *http.Client, req *http.Request) (*http.Response, error) {
|
||||||
|
var resp *http.Response
|
||||||
|
var err error
|
||||||
|
|
||||||
|
for i := range maxRetries {
|
||||||
|
if i > 0 && resp != nil {
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = client.Do(req)
|
||||||
|
if err == nil {
|
||||||
|
if resp.StatusCode == http.StatusOK {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if !shouldRetry(resp.StatusCode) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if i < maxRetries-1 {
|
||||||
|
if err = sleepWithCtx(req.Context(), time.Second*time.Duration(i+1)); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to sleep: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func sleepWithCtx(ctx context.Context, d time.Duration) error {
|
||||||
|
timer := time.NewTimer(d)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
case <-timer.C:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package skills
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDoRequestWithRetry(t *testing.T) {
|
func TestDoRequestWithRetry(t *testing.T) {
|
||||||
|
|
@ -48,14 +49,6 @@ func TestDoRequestWithRetry(t *testing.T) {
|
||||||
wantSuccess: false,
|
wantSuccess: false,
|
||||||
wantAttempts: 3,
|
wantAttempts: 3,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "non-ok-status-code",
|
|
||||||
serverBehavior: func(server *httptest.Server) int {
|
|
||||||
return 4
|
|
||||||
},
|
|
||||||
wantSuccess: false,
|
|
||||||
wantAttempts: 3,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
|
|
@ -71,21 +64,23 @@ func TestDoRequestWithRetry(t *testing.T) {
|
||||||
w.Write([]byte("success"))
|
w.Write([]byte("success"))
|
||||||
}))
|
}))
|
||||||
|
|
||||||
defer server.Close()
|
t.Cleanup(func() {
|
||||||
|
server.Close()
|
||||||
|
})
|
||||||
|
|
||||||
client := &http.Client{Timeout: 5 * time.Second}
|
client := &http.Client{Timeout: 5 * time.Second}
|
||||||
req, err := http.NewRequest("GET", server.URL, nil)
|
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
resp, err := doRequestWithRetry(client, req)
|
resp, err := DoRequestWithRetry(client, req)
|
||||||
|
|
||||||
if tc.wantSuccess {
|
if tc.wantSuccess {
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.NotNil(t, resp)
|
require.NotNil(t, resp)
|
||||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
} else {
|
} else {
|
||||||
assert.NotNil(t, resp)
|
require.NotNil(t, resp)
|
||||||
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
|
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
}
|
}
|
||||||
|
|
@ -120,12 +115,12 @@ func TestDoRequestWithRetry_Delay(t *testing.T) {
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
client := &http.Client{Timeout: 10 * time.Second}
|
client := &http.Client{Timeout: 10 * time.Second}
|
||||||
req, err := http.NewRequest("GET", server.URL, nil)
|
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
resp, err := doRequestWithRetry(client, req)
|
resp, err := DoRequestWithRetry(client, req)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.NotNil(t, resp)
|
require.NotNil(t, resp)
|
||||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue