diff --git a/.jules/bolt.md b/.jules/bolt.md index c214509..873df16 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -21,3 +21,7 @@ ## 2025-05-10 - fmt.Sscanf is lenient, strconv.Atoi is strict **Learning:** In Go, `fmt.Sscanf("%d")` parses digits until it encounters a non-digit character (e.g., `"2beta"` parses as `2`), while `strconv.Atoi` fails and returns `0` for the entire string. If you need the lenient parsing behavior of `fmt.Sscanf` for performance optimization, implement a custom byte-traversal loop to extract leading digits rather than relying on `strconv.Atoi` or regex, as it is over 10x faster and maintains exact functional parity. **Action:** When replacing `fmt.Sscanf` for performance, always evaluate whether the leniency of the parser is being implicitly relied upon by the surrounding code. + +## 2025-05-11 - Reuse http.Client to avoid TLS handshake overhead +**Learning:** Recreating an `http.Client` for every request is a performance anti-pattern that prevents TCP connection reuse (keep-alive) and forces new TLS handshakes, which is significantly slower for APIs like Anthropic's. +**Action:** Lift `http.Client` instances to package-level variables or struct fields for reuse to enable connection pooling, as they are explicitly designed to be thread-safe. diff --git a/internal/plugins/usage_common.go b/internal/plugins/usage_common.go index 6c54ca5..ca8a86f 100644 --- a/internal/plugins/usage_common.go +++ b/internal/plugins/usage_common.go @@ -37,6 +37,8 @@ const ( usageDiskCacheTTL = 5 * time.Minute ) +var usageClient = &http.Client{Timeout: usageAPITimeout} + // UsageResponse represents the API response from the usage endpoint type UsageResponse struct { FiveHour *UsageLimit `json:"five_hour"` @@ -181,8 +183,7 @@ func FetchUsage(ctx context.Context, token string) (*UsageResponse, error) { req.Header.Set("Content-Type", "application/json") req.Header.Set("anthropic-beta", "oauth-2025-04-20") - client := &http.Client{Timeout: usageAPITimeout} - resp, err := client.Do(req) + resp, err := usageClient.Do(req) if err != nil { return nil, fmt.Errorf("failed to fetch usage: %w", err) }