From c354e2f8f94ccd039cec402abd77bdafc8fba2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Insaurralde?= Date: Wed, 22 Apr 2026 04:03:25 -0300 Subject: [PATCH] knotmirror/resyncer: reuse http.Client for knot reachability checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid allocating a new http.Client on every resyncRepo call. http.Client is safe for concurrent use and reusing it enables TCP connection pooling across checks to the same knot. Signed-off-by: Matías Insaurralde --- knotmirror/resyncer.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/knotmirror/resyncer.go b/knotmirror/resyncer.go index 4357e8a0..97023799 100644 --- a/knotmirror/resyncer.go +++ b/knotmirror/resyncer.go @@ -37,6 +37,8 @@ type Resyncer struct { knotBackoff map[string]time.Time knotBackoffMu sync.RWMutex + + httpClient *http.Client } func NewResyncer(l *slog.Logger, db *sql.DB, gitm GitMirrorManager, cfg *config.Config) *Resyncer { @@ -53,6 +55,8 @@ func NewResyncer(l *slog.Logger, db *sql.DB, gitm GitMirrorManager, cfg *config. parallelism: cfg.ResyncParallelism, knotBackoff: make(map[string]time.Time), + + httpClient: &http.Client{Timeout: 30 * time.Second}, } } @@ -283,9 +287,6 @@ func (r *Resyncer) checkKnotReachability(ctx context.Context, repo *models.Repo) r.logger.Debug("checking knot reachability", "url", repoUrl) - client := http.Client{ - Timeout: 30 * time.Second, - } req, err := http.NewRequestWithContext(ctx, "GET", repoUrl, nil) if err != nil { return err @@ -293,7 +294,7 @@ func (r *Resyncer) checkKnotReachability(ctx context.Context, repo *models.Repo) req.Header.Set("User-Agent", "git/2.x") req.Header.Set("Accept", "*/*") - resp, err := client.Do(req) + resp, err := r.httpClient.Do(req) if err != nil { var uerr *url.Error if errors.As(err, &uerr) { -- 2.51.2