perf(radio): reuse shared HTTP client for Radio Browser API - #5239

Use a package-level http.Client for connection reuse instead of
allocating a new client on every get().

Signed-off-by: elpatron <m.busche@gmail.com>
This commit is contained in:
elpatron 2026-03-22 13:28:53 +01:00
parent 884a2d3c15
commit f3545e798e

View File

@ -152,9 +152,11 @@ func NotifyClick(ctx context.Context, streamURL string) {
_, _ = get(ctx, path)
}
// Shared client for Radio Browser requests: safe for concurrent use and reuses TCP connections.
var apiHTTPClient = &http.Client{Timeout: 20 * time.Second}
func get(ctx context.Context, path string) ([]byte, error) {
hosts := shuffleHosts(APIHosts())
client := &http.Client{Timeout: 20 * time.Second}
var lastErr error
for _, host := range hosts {
reqURL := "https://" + host + path
@ -165,7 +167,7 @@ func get(ctx context.Context, path string) ([]byte, error) {
}
req.Header.Set("User-Agent", consts.HTTPUserAgent)
resp, err := client.Do(req)
resp, err := apiHTTPClient.Do(req)
if err != nil {
lastErr = err
continue