An unresponsive service can be worse than a down one. It can tie up your entire system if not handled properly. All network requests should have a timeout.
Here’s how to add timeouts for popular Node packages. All have been tested. The default is no timeout, unless otherwise specified. Enjoy!
Also available for Ruby, Python, Go, PHP, and Rust
Standard library
NPM
exec(cmd, {timeout: 1000});
new dns.Resolver({timeout: 1000});
http.request(url, {timeout: 1000});
const socket = new net.Socket(); socket.setTimeout(1000);
axios.get(url, {timeout: 1000}); // or const instance = axios.create(); instance.defaults.timeout = 1000;
const client = new cassandra.Client({ socketOptions: { connectTimeout: 1000, readTimeout: 1000 } });
await got(url, {timeout: 1000});
const client = new MongoClient(uri, { connectTimeoutMS: 1000, socketTimeoutMS: 1000, serverSelectionTimeoutMS: 1000 });
const connection = mysql.createConnection({ connectTimeout: 1000 });
Default: 10s connect timeout
new Client({connectionTimeoutMillis: 1000});
redis.createClient({connect_timeout: 1000});
new Client({requestTimeout: 1000});
Default: 30s
@opensearch-project/opensearchnew Client({requestTimeout: 1000});
Default: 30s
Don’t see a library you use?Let us know. Even better, create a pull request for it.
git clone https://github.com/ankane/node-timeouts.git cd node-timeouts npm install
To run all tests, use:
To run individual tests, use:
npm test test/http.test.js
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4