- When querying third-party APIs, responses might be delayed or fail entirely.
- This pattern ensures your application doesn't hang indefinitely.
- Timeout select exits gracefully if the API takes too long.
Example: In an e-commerce app, if the payment gateway doesn’t respond in 2 seconds, abort the request and notify the user with a fallback option.
- Queries to the primary database may occasionally stall due to load or failure.
- Use timeout to switch to a replica or cached data after a delay.
Example: A content platform tries to load personalized recommendations. If the recommendation engine delays, it shows trending content instead.
- Wait for user input for a limited time.
- If the user is inactive, auto-submit or skip the turn.
Example: Online quiz game gives users 10 seconds per question. After 10 seconds of no response, it moves on automatically.
- Periodic checks on microservices, but with strict timeout.
- If a service doesn't respond in time, mark it as unhealthy.
Example: A load balancer checks if a server is healthy every 5 seconds, with a timeout of 1 second for response.
- If a goroutine is expected to complete quickly but doesn’t, timeout can detect potential deadlocks or blocking operations.
Example: In a distributed lock system, if acquiring a lock hangs due to a bug, the timeout allows fallback or alerts.
- Identify which of several channels is slow and react accordingly.
- Timeout helps isolate bottlenecks dynamically.
Example: A data pipeline reads from multiple sources. If one source slows down, the system skips it for now and continues processing others.
- Many protocols (e.g., HTTP, FTP, MQTT) have timeout expectations built-in.
- Timeout select helps implement those without additional timers.
Example: TCP keepalive logic that closes idle connections after a timeout period.
- When waiting on user action or external job completion, don’t wait forever.
- Timeout allows you to fail fast or transition to alternative logic.
Example: In an automation system, if a user doesn't approve within 60 minutes, the task escalates to a supervisor.
- When sending requests under a quota, timeouts allow graceful handling of delays.
- Instead of blocking, switch to cached data or retry logic.
Example: Twitter API has rate limits. Timeout helps avoid hanging calls and triggers a retry queue instead.
- If the user takes too long to respond, the assistant can exit or rephrase the question.
- Prevents infinite waiting.
Example: A banking chatbot asks “Would you like to see your balance?” If the user doesn't respond in 10 seconds, it says “Let me know when you're ready.”