- Microservice Patterns and Best Practices
- Vinicius Feitosa Pacheco
- 332字
- 2021-06-30 19:02:54
Handlers
Creating routes to APIs in Go is very simple, but using native handlers' options can generate certain complexities, especially regarding the validations. The native muxer does not have a lot of flexibility, so the best option is to seek more productive tool handlers.
Go has a multitude of options for handlers, and perhaps the most explored library model because of the characteristic of writing the low-level language.
When it comes to performance for routers in Go, at the time of release of this book, there is nothing more performative than fasthttp: (https://github.com/valyala/fasthttp).This is a library written using Go that provides low-level language. Fasthttp metrics are outstanding.
Here are the numbers running tests locally to provision static files:
Running 10s test @ http://localhost:8080 4 threads and 16 connections Thread Stats Avg Stdev Max +/- Stdev Latency 447.99us 1.59ms 27.20ms 94.79% Req/Sec 37.13k 3.99k 47.86k 76.00% 1478457 requests in 10.02s, 1.03GB read Requests/sec: 147597.06 Transfer/sec: 105.15MB
As it can be seen, the number of requests per second exceeds 140,000. However, writing the routes using fasthttp can be as complex as with the native library. Due to this problem, there are some frameworks that do interface for fasthttp. One of these interfaces is the fasthttprouter (https://github.com/buaazp/fasthttprouter), which ultimately creates a number of development facilities without overly compromising the good performance of fasthttp.
Writing routes with extreme performance is very seductive, but we need a balance between performance and stability; here we have a point needing attention. Fasthttp, as well as all its aid interfaces, modifies the native standard of the handler Go to implement the context itself. If there really is a big performance problem, using fasthttp may be something to think about. I do not think this is our case. Therefore, it is recommended to use something that has more compatibility with the standard Go interface.
The most famous option is gorilla/mux (https://github.com/gorilla/mux). Without a doubt, it is one of the most mature and experienced libraries for Go.
- 微商之道
- SOA用戶(hù)指南
- 電子政務(wù)效益的經(jīng)濟(jì)分析與評(píng)價(jià)
- 農(nóng)產(chǎn)品物聯(lián)網(wǎng)研究與應(yīng)用
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見(jiàn):2015年中國(guó)互聯(lián)網(wǎng)安全大會(huì)文集
- 光纖通信系統(tǒng)與網(wǎng)絡(luò)(修訂版)
- SAE原理與網(wǎng)絡(luò)規(guī)劃
- 網(wǎng)管第一課:網(wǎng)絡(luò)操作系統(tǒng)與配置管理
- 高級(jí)網(wǎng)絡(luò)技術(shù)
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- 移動(dòng)物聯(lián)網(wǎng):商業(yè)模式+案例分析+應(yīng)用實(shí)戰(zhàn)
- 云計(jì)算技術(shù)與標(biāo)準(zhǔn)化
- 加密與解密實(shí)戰(zhàn)全攻略
- 視聽(tīng)變革:廣電的新媒體戰(zhàn)略
- 結(jié)網(wǎng)@改變世界的互聯(lián)網(wǎng)產(chǎn)品經(jīng)理(修訂版)