mirror of
https://github.com/go-i2p/go-i2cp.git
synced 2025-12-01 06:54:57 -05:00
Context Usage Example
This example demonstrates context-aware operations in the go-i2cp library.
Features Demonstrated
- Connection with Timeout: Using
context.WithTimeoutto prevent hanging connections - Session Creation with Cancellation: Manual cancellation using
context.WithCancel - Graceful Shutdown: Proper cleanup using the
Close()method - Background Processing: Running I/O loops with context support
What You'll Learn
- How to use
context.Contextfor timeouts and cancellation - Proper error handling for context-related errors
- Graceful shutdown patterns with resource cleanup
- Session lifecycle management
- Background task management with context
Running the Example
cd examples/context-usage
go run context_usage.go
Building the Example
cd examples/context-usage
go build
./context-usage
Example Output
The example will demonstrate four scenarios:
- Connection with Timeout - Shows how to connect with a 10-second timeout
- Session with Cancellation - Demonstrates manual cancellation after 5 seconds
- Graceful Shutdown - Shows proper cleanup of multiple sessions
- Background Processing - Demonstrates I/O processing with context
Note: Most scenarios will fail to fully execute without a running I2P router, but they demonstrate proper API usage and error handling.
Code Highlights
Connection with Timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := client.Connect(ctx)
// Context will automatically cancel after 10 seconds
Graceful Shutdown
// Close will:
// 1. Destroy all sessions
// 2. Wait for pending operations (max 5 seconds)
// 3. Close TCP connection
err := client.Close()
Context Cancellation
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(5 * time.Second)
cancel() // Cancel after 5 seconds
}()
err := client.CreateSession(ctx, session)
// Will fail with context cancelled error
Requirements
- Go 1.18 or later
- Optional: I2P router running on
127.0.0.1:7654for full functionality
Related Examples
- Modern Crypto Demo - Demonstrates cryptographic operations
- Examples Overview - All available examples