Don't trust default timeouts

栏目: IT技术 · 发布时间: 4年前

内容简介:Modern applications don’t crash; they hang. One of the main reasons for it is the assumption that the network is reliable. It isn’t.When you make a network call without setting a timeout, you are telling your code that you are 100% confident that the call

Modern applications don’t crash; they hang. One of the main reasons for it is the assumption that the network is reliable. It isn’t.

When you make a network call without setting a timeout, you are telling your code that you are 100% confident that the call is going to succeed. Would you really take that bet?

If you are a making synchronous network call that never returns, then to very least your thread hogs forever. Whoops. Asynchronous network calls that don’t return are not free either. Sure, you are not hogging threads, but you are leaking sockets. Any HTTP client library worth its salt uses socket pools to avoid recreating connections . And those pools have a limited capacity. Like any other resource leak, it’s only a matter of time until there are no sockets left. When that happens, your application is going to get stuck waiting for a connection to free up.

If the network is not reliable, why do we keep creating APIs that have infinity as the default timeout? Some APIs don’t even have a way to set a timeout in the first place! A good API should be easy to use the right way and hard to use the wrong way. When the default timeout is infinity, it’s all too easy for a client to shoot itself in the foot.

If you remember one thing from this post, then let it be this: never use “infinity” as a default timeout .

Let’s take a look at some concrete examples.

Javascript’s XMLHttpRequest is THE web API to retrieve data from a server asynchronously. Its default timeout is zero , which means there is no timeout!

var xhr = new XMLHttpRequest();
xhr.open('GET', '/api', true);

// No timeout by default!
xhr.timeout = 10000; 

xhr.onload = function () {
 // Request finished
};

xhr.ontimeout = function (e) {
 // Request timed out
};

xhr.send(null);

Client-side timeouts are as crucial as server-side ones. There is a maximum number of sockets your browser can open for a particular host. If you make network requests that never returns, you are going to exhaust the socket pool. When the pool is exhausted, you are no longer able to connect to the host.

The fetch web API is a modern replacement for the XMLHttpRequest API, which uses Promises. When the API was initially introduced, there was no way to set a timeout at all ! Browsers have recently added experimental support for the Abort API to support timeouts, though.

const controller = new AbortController();

const signal = controller.signal;

const fetchPromise = fetch(url, {signal});  

// No timeout by default!
setTimeout(() => controller.abort(), 10000); 

fetchPromise.then(response => {
 // Request finished
})

Things aren’t much rosier in Python-land. The requests library uses a default timeout of infinity .

# No timeout by default!
response = requests.get('https://github.com/', timeout=10)

What about Go? Go’s HTTP package doesn’t use timeouts by default either.

var client = &http.Client{
  // No timeout by default!
  Timeout: time.Second * 10, 
}

response, _ := client .Get(url)

Modern HTTP clients for Java and .NET do a much better job and usually, come with default timeouts. For example, .Net Core’s HttpClient has a default timeout of 100 seconds . It’s lax but much better than no timeout at all. That comes as no surprise since those languages are used to build large scale distributed systems that need to be robust against network failures. Network requests without timeouts are the top silent killer of distributed systems.

Remember this

As a rule of thumb, always set timeouts when making network calls. And if you build libraries, always set reasonable default timeouts and make them configurable for your clients.

Do you want to learn more about stability patterns and anti-patterns of distributed systems? Check out my upcoming video class .


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Unreal Engine 4 Scripting with C++ Cookbook

Unreal Engine 4 Scripting with C++ Cookbook

Sherif, William、Stephen Whittle / 2016-10-24

Unreal Engine 4 (UE4) is a complete suite of game development tools made by game developers, for game developers. With more than 100 practical recipes, this book is a guide showcasing techniques to us......一起来看看 《Unreal Engine 4 Scripting with C++ Cookbook》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器