Deploy managed portal with Docker
This commit is contained in:
@@ -18,13 +18,19 @@ type HTTPDoer interface {
|
||||
|
||||
type RemoteClient struct {
|
||||
httpClient HTTPDoer
|
||||
attempts int
|
||||
retryDelay time.Duration
|
||||
}
|
||||
|
||||
func NewRemoteClient(client HTTPDoer) *RemoteClient {
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: 5 * time.Second}
|
||||
}
|
||||
return &RemoteClient{httpClient: client}
|
||||
return &RemoteClient{
|
||||
httpClient: client,
|
||||
attempts: 5,
|
||||
retryDelay: 200 * time.Millisecond,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RemoteClient) GetConfig(ctx context.Context, service Service) (map[string]any, error) {
|
||||
@@ -36,17 +42,18 @@ func (c *RemoteClient) GetConfig(ctx context.Context, service Service) (map[stri
|
||||
}
|
||||
|
||||
func (c *RemoteClient) UpdateRTSP(ctx context.Context, service Service, rtsp string) (map[string]any, error) {
|
||||
body := strings.NewReader(fmt.Sprintf(`{"rtsp_url":%q}`, rtsp))
|
||||
req, err := c.newRequest(ctx, http.MethodPut, service, "/api/manage/config", body)
|
||||
resp, err := c.doRequest(ctx, func() (*http.Request, error) {
|
||||
body := strings.NewReader(fmt.Sprintf(`{"rtsp_url":%q}`, rtsp))
|
||||
req, err := c.newRequest(ctx, http.MethodPut, service, "/api/manage/config", body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
return req, nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request %s %s: %w", req.Method, req.URL.String(), err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
@@ -55,7 +62,7 @@ func (c *RemoteClient) UpdateRTSP(ctx context.Context, service Service, rtsp str
|
||||
|
||||
var payload map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
||||
return nil, fmt.Errorf("decode response %s: %w", req.URL.String(), err)
|
||||
return nil, fmt.Errorf("decode response %s: %w", responseURL(resp, service.APIBaseURL+"/api/manage/config"), err)
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
@@ -93,14 +100,13 @@ func (c *RemoteClient) PreviewFile(ctx context.Context, service Service, path st
|
||||
func (c *RemoteClient) Download(ctx context.Context, service Service, path string) (*http.Response, error) {
|
||||
query := url.Values{}
|
||||
query.Set("path", path)
|
||||
req, err := c.newRequest(ctx, http.MethodGet, service, "/api/manage/files/download?"+query.Encode(), nil)
|
||||
|
||||
resp, err := c.doRequest(ctx, func() (*http.Request, error) {
|
||||
return c.newRequest(ctx, http.MethodGet, service, "/api/manage/files/download?"+query.Encode(), nil)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request %s %s: %w", req.Method, req.URL.String(), err)
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
defer resp.Body.Close()
|
||||
return nil, decodeAPIError(resp)
|
||||
@@ -109,26 +115,79 @@ func (c *RemoteClient) Download(ctx context.Context, service Service, path strin
|
||||
}
|
||||
|
||||
func (c *RemoteClient) getJSON(ctx context.Context, service Service, endpoint string, target any) error {
|
||||
req, err := c.newRequest(ctx, http.MethodGet, service, endpoint, nil)
|
||||
resp, err := c.doRequest(ctx, func() (*http.Request, error) {
|
||||
return c.newRequest(ctx, http.MethodGet, service, endpoint, nil)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("request %s %s: %w", req.Method, req.URL.String(), err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
return decodeAPIError(resp)
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(target); err != nil {
|
||||
return fmt.Errorf("decode response %s: %w", req.URL.String(), err)
|
||||
return fmt.Errorf("decode response %s: %w", responseURL(resp, strings.TrimRight(service.APIBaseURL, "/")+endpoint), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func responseURL(resp *http.Response, fallback string) string {
|
||||
if resp != nil && resp.Request != nil && resp.Request.URL != nil {
|
||||
return resp.Request.URL.String()
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func (c *RemoteClient) doRequest(ctx context.Context, newReq func() (*http.Request, error)) (*http.Response, error) {
|
||||
attempts := c.attempts
|
||||
if attempts <= 0 {
|
||||
attempts = 1
|
||||
}
|
||||
delay := c.retryDelay
|
||||
if delay <= 0 {
|
||||
delay = 100 * time.Millisecond
|
||||
}
|
||||
|
||||
var lastReq *http.Request
|
||||
var lastErr error
|
||||
for attempt := 1; attempt <= attempts; attempt++ {
|
||||
req, err := newReq()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lastReq = req
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err == nil {
|
||||
return resp, nil
|
||||
}
|
||||
lastErr = err
|
||||
if attempt == attempts {
|
||||
break
|
||||
}
|
||||
if err := sleepWithContext(ctx, delay); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
delay *= 2
|
||||
}
|
||||
|
||||
if lastReq != nil {
|
||||
return nil, fmt.Errorf("request %s %s: %w", lastReq.Method, lastReq.URL.String(), lastErr)
|
||||
}
|
||||
return nil, lastErr
|
||||
}
|
||||
|
||||
func sleepWithContext(ctx context.Context, delay time.Duration) error {
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RemoteClient) newRequest(ctx context.Context, method string, service Service, endpoint string, body io.Reader) (*http.Request, error) {
|
||||
base := strings.TrimRight(service.APIBaseURL, "/")
|
||||
req, err := http.NewRequestWithContext(ctx, method, base+endpoint, body)
|
||||
|
||||
@@ -4,10 +4,12 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type roundTripFunc func(req *http.Request) (*http.Response, error)
|
||||
@@ -139,3 +141,37 @@ func TestRemoteClientRoundTrip(t *testing.T) {
|
||||
t.Fatalf("Content-Disposition = %q", resp.Header.Get("Content-Disposition"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteClientRetriesTransientRequestErrors(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
attempts := 0
|
||||
client := NewRemoteClient(&http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
attempts++
|
||||
if attempts < 3 {
|
||||
return nil, errors.New("connect: connection refused")
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(strings.NewReader(`{"config_path":"/srv/store/config/local.yaml"}`)),
|
||||
}, nil
|
||||
})})
|
||||
client.retryDelay = time.Millisecond
|
||||
|
||||
service := Service{
|
||||
ID: "store_dwell_alert",
|
||||
APIBaseURL: "http://managed.invalid/store",
|
||||
}
|
||||
|
||||
configPayload, err := client.GetConfig(context.Background(), service)
|
||||
if err != nil {
|
||||
t.Fatalf("GetConfig() error = %v", err)
|
||||
}
|
||||
if attempts != 3 {
|
||||
t.Fatalf("attempts = %d", attempts)
|
||||
}
|
||||
if got := configPayload["config_path"]; got != "/srv/store/config/local.yaml" {
|
||||
t.Fatalf("config_path = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user