julienschmidt / httprouter
A high performance HTTP request router that scales well
AI Architecture Analysis
This repository is indexed by RepoMind. By analyzing julienschmidt/httprouter in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.
Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.
Repository Overview (README excerpt)
Crawler viewHttpRouter HttpRouter is a lightweight high performance HTTP request router (also called *multiplexer* or just *mux* for short) for Go. In contrast to the default mux of Go's package, this router supports variables in the routing pattern and matches against the request method. It also scales better. The router is optimized for high performance and a small memory footprint. It scales well even with very long paths and a large number of routes. A compressing dynamic trie (radix tree) structure is used for efficient matching. Features **Only explicit matches:** With other routers, like , a requested URL path could match multiple patterns. Therefore they have some awkward pattern priority rules, like *longest match* or *first registered, first matched*. By design of this router, a request can only match exactly one or no route. As a result, there are also no unintended matches, which makes it great for SEO and improves the user experience. **Stop caring about trailing slashes:** Choose the URL style you like, the router automatically redirects the client if a trailing slash is missing or if there is one extra. Of course it only does so, if the new path has a handler. If you don't like it, you can turn off this behavior. **Path auto-correction:** Besides detecting the missing or additional trailing slash at no extra cost, the router can also fix wrong cases and remove superfluous path elements (like or ). Is CAPTAIN CAPS LOCK one of your users? HttpRouter can help him by making a case-insensitive look-up and redirecting him to the correct URL. **Parameters in your routing pattern:** Stop parsing the requested URL path, just give the path segment a name and the router delivers the dynamic value to you. Because of the design of the router, path parameters are very cheap. **Zero Garbage:** The matching and dispatching process generates zero bytes of garbage. The only heap allocations that are made are building the slice of the key-value pairs for path parameters, and building new context and request objects (the latter only in the standard / API). In the 3-argument API, if the request path contains no parameters not a single heap allocation is necessary. **Best Performance:** Benchmarks speak for themselves. See below for technical details of the implementation. **No more server crashes:** You can set a Panic handler to deal with panics occurring during handling a HTTP request. The router then recovers and lets the log what happened and deliver a nice error page. **Perfect for APIs:** The router design encourages to build sensible, hierarchical RESTful APIs. Moreover it has built-in native support for OPTIONS requests and replies. Of course you can also set **custom and handlers** and **serve static files**. Usage This is just a quick introduction, view the Docs for details. $ go get github.com/julienschmidt/httprouter and use it, like in this trivial example: Named parameters As you can see, is a *named parameter*. The values are accessible via , which is just a slice of s. You can get the value of a parameter either by its index in the slice, or by using the method: can be retrieved by . When using a (using or ) instead of HttpRouter's handle API using a 3rd function parameter, the named parameters are stored in the . See more below under Why doesn't this work with http.Handler?. Named parameters only match a single path segment: **Note:** Since this router has only explicit matches, you can not register static routes and parameters for the same path segment. For example you can not register the patterns and for the same request method at the same time. The routing of different request methods is independent from each other. Catch-All parameters The second type are *catch-all* parameters and have the form . Like the name suggests, they match everything. Therefore they must always be at the **end** of the pattern: How does it work? The router relies on a tree structure which makes heavy use of *common prefixes*, it is basically a *compact* *prefix tree* (or just *Radix tree*). Nodes with a common prefix also share a common parent. Here is a short example what the routing tree for the request method could look like: Every represents the memory address of a handler function (a pointer). If you follow a path trough the tree from the root to the leaf, you get the complete route path, e.g , where is just a placeholder (*parameter*) for an actual post name. Unlike hash-maps, a tree structure also allows us to use dynamic parts like the parameter, since we actually match against the routing patterns instead of just comparing hashes. As benchmarks show, this works very well and efficient. Since URL paths have a hierarchical structure and make use only of a limited set of characters (byte values), it is very likely that there are a lot of common prefixes. This allows us to easily reduce the routing into ever smaller problems. Moreover the router manages a separate tree for every request method. For one thing it is more space efficient than holding a method->handle map in every single node, it also allows us to greatly reduce the routing problem before even starting the look-up in the prefix-tree. For even better scalability, the child nodes on each tree level are ordered by priority, where the priority is just the number of handles registered in sub nodes (children, grandchildren, and so on..). This helps in two ways: • Nodes which are part of the most routing paths are evaluated first. This helps to make as much routes as possible to be reachable as fast as possible. • It is some sort of cost compensation. The longest reachable path (highest cost) can always be evaluated first. The following scheme visualizes the tree structure. Nodes are evaluated from top to bottom and from left to right. Why doesn't this work with ? **It does!** The router itself implements the interface. Moreover the router provides convenient adapters for s and s which allows them to be used as a wh…