Skip to content

Commit 868ccc2

Browse files
Copilotmcpherrinm
andcommitted
Use sync.Once for thread-safe client initialization
- Use sync.Once to ensure thread-safe HTTP client initialization - Remove timeout from client (apply per-request via context instead) - Prevents race conditions when multiple goroutines call execute() Co-authored-by: mcpherrinm <47425+mcpherrinm@users.noreply.github.com>
1 parent 86c1eb9 commit 868ccc2

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

linter/lints/rfc/lint_cert_via_pkimetal.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/url"
1212
"slices"
1313
"strings"
14+
"sync"
1415
"time"
1516

1617
"github.com/zmap/zcrypto/x509"
@@ -27,7 +28,8 @@ type PKIMetalConfig struct {
2728
Timeout time.Duration `toml:"timeout" comment:"How long, in nanoseconds, to wait before giving up."`
2829
IgnoreLints []string `toml:"ignore_lints" comment:"The unique Validator:Code IDs of lint findings which should be ignored."`
2930

30-
client *http.Client
31+
client *http.Client
32+
clientOnce sync.Once
3133
}
3234

3335
func (pkim *PKIMetalConfig) execute(endpoint string, der []byte) (*lint.LintResult, error) {
@@ -36,11 +38,9 @@ func (pkim *PKIMetalConfig) execute(endpoint string, der []byte) (*lint.LintResu
3638
timeout = 100 * time.Millisecond
3739
}
3840

39-
// Initialize HTTP client once and reuse it for connection pooling
40-
if pkim.client == nil {
41-
pkim.client = &http.Client{
42-
Timeout: timeout,
43-
}
41+
// Initialize HTTP client once with thread-safe sync.Once for connection pooling
42+
pkim.clientOnce.Do(func() {
43+
pkim.client = &http.Client{}
4444
// If using Unix socket, set up custom transport
4545
if pkim.Socket != "" {
4646
pkim.client.Transport = &http.Transport{
@@ -49,7 +49,7 @@ func (pkim *PKIMetalConfig) execute(endpoint string, der []byte) (*lint.LintResu
4949
},
5050
}
5151
}
52-
}
52+
})
5353

5454
ctx, cancel := context.WithTimeout(context.Background(), timeout)
5555
defer cancel()

0 commit comments

Comments
 (0)