官术网_书友最值得收藏!

TimeoutHandler

The TimeoutHandler function returns a Handler interface that runs h with the given time limit. When we investigate common patterns in Chapter 6, Microservice Frameworks, we will see just how useful this can be for avoiding cascading failures in your service:

func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler 

The new handler calls h.ServeHTTP to handle each request, but if a call runs for longer than its time limit, the handler responds with a 503 Service Unavailable response with the given message (msg) in its body.

The last two handlers are especially interesting as they are, in effect, chaining handlers. This is a technique that we will go into more in-depth in a later chapter as it allows you to both practice clean code and also allows you to keep your code DRY.

I may have lifted most of the descriptions for these handlers straight from the Go documentation and you probably have already read these because you have read the documentation right? With Go, the documentation is excellent and writing documentation for your own packages is heavily encouraged, even enforced, if you use the golint command that comes with the standard package then this will report areas of your code which do not conform to the standards. I really recommend spending a little time browsing the standard docs when you are using one of the packages, not only will you learn the correct usage, you may learn that there is a better approach. You will certainly be exposed to good practice and style and you may even be able to keep working on the sad day that Stack Overflow stops working and the entire industry grinds to a halt.

 

主站蜘蛛池模板: 聂拉木县| 临安市| 疏附县| 汕尾市| 仪陇县| 渭南市| 甘孜县| 庐江县| 子洲县| 年辖:市辖区| 旺苍县| 芮城县| 洪湖市| 屏东市| 泉州市| 罗江县| 常熟市| 卫辉市| 奉节县| 永嘉县| 淳安县| 泽库县| 工布江达县| 和林格尔县| 霍州市| 砚山县| 铁力市| 景东| 莱芜市| 汶川县| 昔阳县| 义乌市| 商水县| 内黄县| 成都市| 吴忠市| 崇仁县| 习水县| 双流县| 德清县| 巫山县|