Swapping different routers/request multiplexers into Caddy

Hi all,

I’ve seen quite a few Golang web frameworks out there – namely Iris, Gin, Echo, and Revel. I don’t fully understand what they do above and beyond something like Caddy, but many of them stress their performance (more than one claims to be the fastest Golang web framework). Key for some of them is the performance of their request router, or what the Golang docs call a “request multiplexer” (ServeMux).

I think at least a couple of them use Julien Schmidt’s HttpRouter, which looks to be much faster than the default ServeMux from the Go http package and Gorilla Mux too.

How hard or easy would it be swap HttpRouter into Caddy as a replacement for ServeMux? I’m just learning about these routers, but the performance difference looks enormous to me.

FYI, here’s an interesting article expressing frustration with all Go URL routers, and his follow-up introducing his solution.

This is a good question, and probably something I should speak about in a design document or talk in more detail. Caddy doesn’t use a router, not in the sense of these frameworks. It doesn’t use http.ServeMux either. The only “routing” it does is to match a request to a middleware chain – by hostname. These depend on the sites defined in the Caddyfile. Caddy uses a custom trie-like structure for that. Each middleware handler decides whether to handle a request based on its own configuration. It’s usually a base path, yes, but all requests go through all handlers for a host, even if it’s just a no-op.

1 Like