课程: Go Essentials: Concurrency, Connectivity, and High-Performance Apps

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Timeouts & Size Limit

Timeouts & Size Limit

- The fallacies of distributed computer states that the network is not reliable. The latency is not zero. The network is not secure. Wherever you walk with external services, you need to guard yourself and in this case, we're going to talk about two things you can help yourself, setting a timeout and limit the amount of data that you read. With use of the IO package, the net HTP package combined with the context package, you can achieve that. So to start, we'll create a context. This context is a time limit of 3000 millisecond or three seconds and then we create a new request with context. Given our context, a get method, the URL and nil for a bug and now we do HTP default client.do with the request. When we start reading the data, we can define how much data we want to read. In this case, I'm going to define a single megabyte and then I'm wrapping the response body with IO limit reader which is going to limit how…

内容