From 3233dfc63098fb9da4f021f962fedca4a7999052 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Mon, 11 May 2026 18:34:33 -0700 Subject: [PATCH] fix: httpClient being created for every request --- client.go | 4 ++++ remote.go | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 67c2ef8..5e17083 100644 --- a/client.go +++ b/client.go @@ -1,6 +1,7 @@ package client import ( + "net/http" "sync" "sync/atomic" ) @@ -17,6 +18,9 @@ type Client struct { authMu sync.Mutex authToken string authTokenExp int64 + + httpClientMu sync.Mutex + httpClient_ *http.Client } func NewClient(ctx Context) *Client { diff --git a/remote.go b/remote.go index 620ce50..b2c86c5 100644 --- a/remote.go +++ b/remote.go @@ -94,11 +94,16 @@ func (c *Client) checkCriteria(token string, switcher *Switcher, showDetails boo query.Set("showReason", strings.ToLower(strconvFormatBool(showDetails))) query.Set("key", switcher.key) + entries := switcher.entries + if entries == nil { + entries = []criteriaEntry{} + } + response, err := c.doJSONRequest( http.MethodPost, endpoint+"?"+query.Encode(), map[string]any{ - "entry": switcher.entries, + "entry": entries, }, map[string]string{ "Authorization": "Bearer " + token, @@ -147,6 +152,13 @@ func (c *Client) doJSONRequest(method, endpoint string, payload any, headers map } func (c *Client) httpClient() *http.Client { + c.httpClientMu.Lock() + defer c.httpClientMu.Unlock() + + if c.httpClient_ != nil { + return c.httpClient_ + } + ctx := c.Context() dialer := &net.Dialer{ Timeout: ctx.Options.Remote.ConnectTimeout, @@ -162,10 +174,12 @@ func (c *Client) httpClient() *http.Client { }, } - return &http.Client{ + c.httpClient_ = &http.Client{ Transport: transport, Timeout: requestTimeout(ctx.Options.Remote), } + + return c.httpClient_ } func requestTimeout(options RemoteOptions) time.Duration {