Skip to content

feat(client): add context.Context support for cancellation and timeout#118

Merged
xia0pin9 merged 3 commits intopinterest:masterfrom
xia0pin9:feat/add-context-support
Jan 14, 2026
Merged

feat(client): add context.Context support for cancellation and timeout#118
xia0pin9 merged 3 commits intopinterest:masterfrom
xia0pin9:feat/add-context-support

Conversation

@xia0pin9
Copy link
Contributor

@xia0pin9 xia0pin9 commented Jan 8, 2026

This commit adds context.Context support throughout the knox client to enable proper cancellation and timeout handling for long-running operations.

Changes:

  • Add context.Context parameter to cache and network key retrieval methods
  • New methods: CacheGetKeyWithContext(), NetworkGetKeyWithContext(), getHTTPDataWithContext()
  • Add context cancellation checks before expensive operations:
    • Before file I/O operations
    • Before JSON unmarshaling
    • Before each auth handler attempt
    • Before each retry attempt
    • Before backoff sleep
  • Existing methods now delegate to *WithContext() variants with context.Background() for backward compatibility
  • Fix bug in fileClient.setValues() where active keys were incorrectly appended instead of assigned to pre-allocated slice indices
  • Standardize error logging (Knox→knox, %s→%w, .Error()→err)

The context support allows callers to:

  • Set timeouts for key retrieval operations
  • Cancel in-flight requests when they're no longer needed
  • Properly handle deadline exceeded scenarios

All existing APIs maintain backward compatibility by using context.Background() internally.

This commit adds context.Context support throughout the knox client
to enable proper cancellation and timeout handling for long-running
operations.

Changes:
- Add context.Context parameter to cache and network key retrieval methods
- New methods: CacheGetKeyWithContext(), NetworkGetKeyWithContext(),
  getHTTPDataWithContext()
- Add context cancellation checks before expensive operations:
  * Before file I/O operations
  * Before JSON unmarshaling
  * Before each auth handler attempt
  * Before each retry attempt
  * Before backoff sleep
- Existing methods now delegate to *WithContext() variants with
  context.Background() for backward compatibility
- Fix bug in fileClient.setValues() where active keys were incorrectly
  appended instead of assigned to pre-allocated slice indices
- Standardize error logging (Knox→knox, %s→%w, .Error()→err)

The context support allows callers to:
- Set timeouts for key retrieval operations
- Cancel in-flight requests when they're no longer needed
- Properly handle deadline exceeded scenarios

All existing APIs maintain backward compatibility by using
context.Background() internally.
@xia0pin9 xia0pin9 force-pushed the feat/add-context-support branch from 6c8c291 to b88a91c Compare January 14, 2026 22:45
@xia0pin9 xia0pin9 merged commit f3b0cb5 into pinterest:master Jan 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants