OrangePi3588Media/agent/internal/mediaserver/client.go

204 lines
4.7 KiB
Go

package mediaserver
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"path"
"encoding/json"
"strings"
"time"
)
type RetryPolicy struct {
MaxAttempts int
Backoff []time.Duration
}
type Client struct {
base *url.URL
http *http.Client
ctrlTO time.Duration
readTO time.Duration
retry RetryPolicy
}
func New(baseURL string, ctrlTimeoutMS int, retryMaxAttempts int, retryBackoffMS []int) (*Client, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
u.Path = strings.TrimRight(u.Path, "/")
pol := RetryPolicy{MaxAttempts: retryMaxAttempts}
for _, ms := range retryBackoffMS {
pol.Backoff = append(pol.Backoff, time.Duration(ms)*time.Millisecond)
}
return &Client{
base: u,
http: &http.Client{},
ctrlTO: time.Duration(ctrlTimeoutMS) * time.Millisecond,
readTO: 1000 * time.Millisecond,
retry: pol,
}, nil
}
func (c *Client) GetGraphs(ctx context.Context) (int, []byte, error) {
return c.doRead(ctx, http.MethodGet, "/api/graphs", nil, nil)
}
func (c *Client) GetGraph(ctx context.Context, name string) (int, []byte, error) {
return c.doRead(ctx, http.MethodGet, "/api/graphs/"+url.PathEscape(name), nil, nil)
}
func (c *Client) GetLogsRecent(ctx context.Context, limit int) (int, []byte, error) {
q := url.Values{}
if limit > 0 {
q.Set("limit", fmt.Sprintf("%d", limit))
}
return c.doRead(ctx, http.MethodGet, "/api/logs/recent", q, nil)
}
func (c *Client) Reload(ctx context.Context) error {
_, _, err := c.doControl(ctx, http.MethodPost, "/api/config/reload", nil)
return err
}
func (c *Client) UpdateNodeConfig(ctx context.Context, nodeID string, graph string, patch any) error {
if strings.TrimSpace(nodeID) == "" {
return errors.New("node id is empty")
}
b, err := json.Marshal(patch)
if err != nil {
return err
}
q := url.Values{}
if strings.TrimSpace(graph) != "" {
q.Set("graph", graph)
}
_, _, err = c.doControlQ(ctx, http.MethodPost, "/api/nodes/"+url.PathEscape(nodeID)+"/config", q, b)
return err
}
func (c *Client) doRead(ctx context.Context, method, p string, q url.Values, body []byte) (int, []byte, error) {
ctx, cancel := context.WithTimeout(ctx, c.readTO)
defer cancel()
return c.doOnce(ctx, method, p, q, body)
}
func (c *Client) doControl(ctx context.Context, method, p string, body []byte) (int, []byte, error) {
return c.doControlQ(ctx, method, p, nil, body)
}
func (c *Client) doControlQ(ctx context.Context, method, p string, q url.Values, body []byte) (int, []byte, error) {
if c.retry.MaxAttempts <= 1 {
ctx, cancel := context.WithTimeout(ctx, c.ctrlTO)
defer cancel()
st, b, err := c.doOnce(ctx, method, p, q, body)
return st, b, classifyControlErr(st, b, err)
}
var lastErr error
var lastStatus int
var lastBody []byte
for attempt := 1; attempt <= c.retry.MaxAttempts; attempt++ {
ctxAttempt, cancel := context.WithTimeout(ctx, c.ctrlTO)
st, b, err := c.doOnce(ctxAttempt, method, p, q, body)
cancel()
lastStatus, lastBody = st, b
lastErr = classifyControlErr(st, b, err)
if lastErr == nil {
return st, b, nil
}
if !isRetryableControl(st, lastErr) {
break
}
if attempt < c.retry.MaxAttempts {
bi := attempt - 1
if bi >= 0 && bi < len(c.retry.Backoff) {
t := time.NewTimer(c.retry.Backoff[bi])
select {
case <-ctx.Done():
t.Stop()
return lastStatus, lastBody, lastErr
case <-t.C:
}
}
}
}
return lastStatus, lastBody, lastErr
}
func (c *Client) doOnce(ctx context.Context, method, p string, q url.Values, body []byte) (int, []byte, error) {
u := *c.base
bp := strings.TrimRight(c.base.Path, "/")
rel := strings.TrimLeft(p, "/")
if bp == "" {
u.Path = "/" + rel
} else {
u.Path = path.Join(bp, rel)
}
u.RawQuery = q.Encode()
var r io.Reader
if body != nil {
r = bytes.NewReader(body)
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), r)
if err != nil {
return 0, nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.http.Do(req)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
b, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
return resp.StatusCode, b, nil
}
func classifyControlErr(status int, body []byte, err error) error {
if err != nil {
return err
}
if status >= 200 && status <= 299 {
return nil
}
return fmt.Errorf("media-server status=%d body=%s", status, strings.TrimSpace(string(body)))
}
func isRetryableControl(status int, err error) bool {
if err == nil {
return false
}
if status >= 400 && status <= 499 {
return false
}
var ne net.Error
if errors.As(err, &ne) {
return true
}
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
return true
}
if status >= 500 {
return true
}
return false
}