Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 3 additions & 2 deletions internal/plugins/usage_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
}
Expand Down
Loading