DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Patterns To Make Synchronous Communication in Microservices Resilient
  • Exploring the Architecture of Amazon SQS
  • Coordinating Threads Using CountDownLatch
  • MuleSoft Parallel ForEach Processor vs. ForEach Processor

Trending

  • Adopting Agile Practices for Workforce Management: Benefits, Challenges, and Practices
  • Edge Data Platforms, Real-Time Services, and Modern Data Trends
  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  • Securing Your Applications With Spring Security

Infinity Is a Bad Timeout

I believe this is one of those issues that looks tiny but causes a lot of problems in the real world. It can (and should) be solved by the library/client designers.

Bozhidar Bozhanov user avatar by
Bozhidar Bozhanov
·
Mar. 23, 17 · Opinion
Like (4)
Save
Tweet
Share
4.00K Views

Join the DZone community and get the full member experience.

Join For Free

Many libraries wrap some external communication — be it a REST-like API, a message queue, a database, a mail server, or something else. Therefore, you have to have some timeout for connecting, for reading, writing, or idling. Sadly, many libraries have their default timeouts set to “0” or “-1” which means “infinity.”

And that is a useless and even harmful default. There isn’t a practical use case where you’d want to hang on forever waiting for a resource. And there are tons of situations where this can happen, i.e., the other end gets stuck. In the past three months, I had two libraries that have a default timeout of “infinity” and that eventually lead to production problems because we’ve forgotten to configure them properly. Sometimes, you even don’t see the problem until a thread pool gets exhausted.

So, I have a request to API/library designers (as I’ve done before – against property maps and encoding other than UTF-8). Never have “infinity” as a default timeout. Your library will thus cause lots of production issues. Also note that it’s sometimes an underlying HTTP client (or Socket) that doesn’t have a reasonable default — it’s still your job to fix that when wrapping it.

What default should you provide? Reasonable. Five seconds maybe? You may (rightly) say you don’t want to impose an arbitrary timeout on your users. In that case I have a better proposal:

Explicitly require a timeout for building your “client” (because these libraries are most often clients for some external system) — for example, Client.create(url, credentials, timeout) (and fail if no timeout is provided). That makes the users of the client actively consider what is a good timeout for their use case without imposing anything, and, most importantly, without risking stuck connections in production. Additionally, you can still present them with a “default” option, but still making them explicitly choose it. For example:

Client client = ClientBuilder.create(url)
                   .withCredentials(credentials)
                   .withTimeouts(Timeouts.connect(1000).read(1000))
                   .build();
// OR
Client client = ClientBuilder.create(url)
                   .withCredentials(credentials)
                   .withDefaultTimeouts()
                   .build();

The builder above should require “timeouts” to be set and should fail if neither of the two methods was invoked. Even if you don’t provide these options, at least have a good way of specifying timeouts. Some libraries require reflection to set the timeout of their underlying client.

I believe this is one of those issues that looks tiny but causes a lot of problems in the real world. It can (and should) be solved by the library/client designers. But since it isn’t always the case, we must make sure that timeouts are configured every time we use a third-party library.

Timeout (computing)

Published at DZone with permission of Bozhidar Bozhanov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Patterns To Make Synchronous Communication in Microservices Resilient
  • Exploring the Architecture of Amazon SQS
  • Coordinating Threads Using CountDownLatch
  • MuleSoft Parallel ForEach Processor vs. ForEach Processor

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: