goshippo / shippo-node-client

Shipping API Node.js library (USPS, FedEx, UPS and more)
https://goshippo.com/docs
MIT License
136 stars 54 forks source link

Set the client timeout to a lower value #10

Closed kevinburkeshyp closed 4 years ago

kevinburkeshyp commented 9 years ago

The default Shippo client timeout is set to 120 seconds. Heroku will terminate requests if they take longer than 30 seconds. No point in waiting around for 90 extra seconds for a response.

lrettig commented 7 years ago

For the record, you can address this in two ways. One appears to be with setTimeout:

var shippo = require('shippo')('<YOUR_PRIVATE_KEY>');
shippo.setTimeout(10000);

You can also do it manually:

var shippo = require('shippo')('<YOUR_PRIVATE_KEY>');
Promise.race([
  shippo.shipment.create(shipment),
  new Promise((resolve, reject) => {
    setTimeout(() => reject(new Error("Timed out")), 10000);
  })
]).then(
  result => // success
  err => // err, including timeout
);
kevinburkeshyp commented 7 years ago

Ideally timing out the request would cancel the request and close any resources associated with it (connections, response objects)

On Sat, May 6, 2017 at 08:38 Lane Rettig notifications@github.com wrote:

For the record, you can address this in two ways. One appears to be with setTimeout https://github.com/goshippo/shippo-node-client/blob/7fd73c41e649af097c1257b7d06ac4b6102f4069/lib/shippo.js#L72 :

var shippo = require('shippo')(''); shippo.setTimeout(10000);

You can also do it manually:

var shippo = require('shippo')(''); Promise.race([ shippo.shipment.create(shipment), new Promise((resolve, reject) => { setTimeout(() => reject(new Error("Timed out")), 10000); }) ]).then( result => // success err => // err, including timeout );

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/goshippo/shippo-node-client/issues/10#issuecomment-299647632, or mute the thread https://github.com/notifications/unsubscribe-auth/AKPN0XmXk2VGgCUOIE7K4qNGwRzo7F3qks5r3JPzgaJpZM4D0uTw .

--

-- Kevin Burke 925.271.7005 | kev.inburke.com

lrettig commented 7 years ago

This library appears to be using node's http under the hood, which I think handles timeouts, cf. https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback

On Sun, May 7, 2017 at 12:40 AM, Kevin Burke notifications@github.com wrote:

Ideally timing out the request would cancel the request and close any resources associated with it (connections, response objects)

On Sat, May 6, 2017 at 08:38 Lane Rettig notifications@github.com wrote:

For the record, you can address this in two ways. One appears to be with setTimeout https://github.com/goshippo/shippo-node-client/blob/ 7fd73c41e649af097c1257b7d06ac4b6102f4069/lib/shippo.js#L72 :

var shippo = require('shippo')(''); shippo.setTimeout(10000);

You can also do it manually:

var shippo = require('shippo')(''); Promise.race([ shippo.shipment.create(shipment), new Promise((resolve, reject) => { setTimeout(() => reject(new Error("Timed out")), 10000); }) ]).then( result => // success err => // err, including timeout );

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/goshippo/shippo-node-client/issues/10# issuecomment-299647632, or mute the thread https://github.com/notifications/unsubscribe-auth/ AKPN0XmXk2VGgCUOIE7K4qNGwRzo7F3qks5r3JPzgaJpZM4D0uTw .

--

-- Kevin Burke 925.271.7005 <(925)%20271-7005> | kev.inburke.com

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/goshippo/shippo-node-client/issues/10#issuecomment-299647755, or mute the thread https://github.com/notifications/unsubscribe-auth/ADKbNObzA4n5CV0ZntGAGrl2ZHcGFckAks5r3JRugaJpZM4D0uTw .

epistemancer commented 4 years ago

We're updating the timeout to 30s; 2m is overly long for most calls against our API. The timeout can be overridden with setTimeout as noted above.

For what it's worth, when the client timeout is longer than 30s but Heroku times the request out at 30s, I don't think the client can know the request timed out. I haven't used Heroku, but if I'm reading this correctly:

https://devcenter.heroku.com/articles/request-timeout

the requesting application can't tell the request has timed out inside Heroku's router, and the solution is to set the timeout within 30 seconds on the application side.