Comparison of net/http and httprouter
This post will mainly revolve around the comparison between different implementations of Routers in the HTTP based frameworks.
Let’s first go over what routers are in the context of a HTTP framework.
Most frameworks today implement the MVC pattern or at least something similar to it. Even lightweight frameworks which don’t actually implement any design pattern have multiple built in features which usually doesn’t require custom logic by the application developer.
When a request is received by the framework handler, the framework reads the URI path and dispatches the request to the user defined action along with the request context in the request object and expects and response object in return. It is the responsibility of the router to read the request URI and call the appropriate handler.
In this blog post, we are going to compare the routers in 2 packages:
- net/http (standard golang package)
- httprouter
Net/http
Golang’s standard library comes with a pretty powerful package to handle and build HTTP applications. The best part of the package is that the modules can be plugged in and changed as required.
Before starting the HTTP server, it is required to register the handlers with the appropriate URI pattern. When a URI pattern is added, the mapping is added to a map data structure which is equivalent to a hashmap.
When a request is received by the http module, it looks in the mux data structure which contains a map containing the pattern and the registered handler.
In order to find the required handler for the received pattern, it calls the match method defined on the ServeMux structure. In the below method, it first checks for a direct comparison in the mux.m map. If no items are found, it tries to find the longest valid match and calls the appropriate handler.
Httprouter
There are multiple popular frameworks built on top of the httprouter package like Gin, Ace, Neko, etc. The httprouter’s github page has an excellent description of the way the module handles the routing.
The main distinction from the net/http package is that httprouter uses compact prefix trees to find the appropriate handler to the URI pattern.
When a request is received by the httprouter module, it finds the appropriate tree based on the API method and calls the addRoute method defined on the node struct.
Taking a sample out of the github page, a tree is formed based on the registered handlers.
Comparison between net/http and httprouter Routers
Since it tries to find the longest valid match, the results from the match method may be confusing when there are many similar patterns defined. This has caused confusion for developers based on the issues raised for the same.
Since there are also multiple entries for each pattern defined, the memory usage will also be a little higher compared to httprouter when there are multiple similar patterns.
It also has to spend more compute compared to httprouter to reach the actual handler as the httprouter router can have a direct walk to the required handler making the correct and more predictable decision.
I hope you liked the article. Please let me know if you have any queries regarding the article. Happy reading!!
References