In Ruby, don’t use timeout()

  • If all goes well, write the contents of a web page to the database.
  • If the HTTP request fails (e.g., server isn’t listening on port 80), throw an error.
  • If the time spent on the HTTP request plus the time spent writing to the database exceeds five seconds, throw an error and don’t write the row.
  • If the time spent on the HTTP request exceeds five seconds, throw an error and don’t write the row.
  • If the time spent on the database write exceeds five seconds, block until the write finishes (potentially forever), write the row and throw an error.

How Timeout::timeout() works

The consequences

A better way

  • If all goes well, write the contents of a web page to the database.
  • If the HTTP request fails (e.g., server isn’t listening on port 80), throw an error.
  • If we lose touch with the HTTP server for five seconds, throw an error.
  • If we lose touch with the database server for three seconds, throw an error.

Refer to the requirements

Conclusion

--

--

--

Journalist, ex software engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Hooper

Adam Hooper

Journalist, ex software engineer

More from Medium

Don’t Cry Over binding.pry

Intro to Rails Serializers

Seeding Data with Faker in Ruby

The logo for the Faker library

All the Code I Didn’t Write