If WriteHeader is called multiple times on a http.ResponseWriter, the
first status is the one that is used, not the last. Fix the wrapped
writer to reflect this fact.
For whatever reason, Go insisted on loading rm.sm[i] in several chunks,
even though it could be loaded in a single 64-bit block. Instead, let's
reorder our loads to minimize the amount of memory we're uselessly
moving around.
This gives us about a 15% perf boost in
github.com/julienschmidt/go-http-routing-benchmark's
BenchmarkGoji_StaticAll, and questionable benefits (i.e., not
distinguishable from noise but certainly no worse) on Goji's own
benchmarks.
This eliminates the race condition mentioned in a54c913a by forbidding
duplicate binds to the same socket (well, at least in the sense that
attempting to do so will *always* result in an error instead of
nondeterministically resulting in an error).
This fixes a race condition between package bind and the garbage
collector, where if the garbage collector ran between einhornInit and
einhornBind, bind would fatal with the error "dup: bad file descriptor"
The core of the bug is that Go's os.File uses runtime.SetFinalizer to
register a callback to close the underlying file descriptor an os.File
points at when the os.File itself is being garbage collected. However,
the Einhorn initialization code in bind, in the process of ensuring that
every Einhorn-passed socket had CloseOnExec set on it, allocated
os.File's pointing at each of these passed file descriptors, but did not
keep references to these os.File's, allowing them to be garbage
collected. Subsequently, if you attempted to bind one of these sockets,
you'd find that it was no longer open.
This is the simplest fix to the bug, which is to only allocate an
os.File when we actually attempt to bind the socket. Note that there's
still a race condition here if you attempt to bind the same file
descriptor twice, since a GC between the two binds will likely cause the
file to be collected. Fortunately, that one can be worked around by
simply not allowing such silly behavior :). Another patch that makes
this more clear will follow.
Closes#29.
Change the per-process nonce part of the request ID from 8 characters to
10, and wrap the entire thing in a retry loop so you can never get an
"unlucky" panic. I know this will "never" happen in practice, but it
doesn't hurt to make sure we never, ever have any collisions, and never,
ever have any runtime panics.
It's also worth documenting the math ("math") I used to calculate the
numbers here.
Previously, we would keep the URLParams / Env associated with a cStack
around until the next request flushed them. However, this might cause
either of these maps to stick around for much longer than they ought to,
potentially keeping references to many, many objects.
Instead, clear out the saved context on every release.
The "dryrun" parameter on Pattern.Match was kind of ugly and made for an
exceedingly mediocre public interface. Instead, split its functionality
in two: the previous "dryrun" behavior now lives in the Match method,
and Patterns now actually mutate state when Run is called.
The code on the backend is of course still the same (for now), but at
least the interface is a little nicer.
Previously, the middleware stack passed the router a C, but this was
both odd semantically (a pattern which mutated the environment might see
a *different* environment) and bad for perf: it cost us an allocation.
Now we only pass around *C's internally.
Importantly ("importantly"), this gets us down to 0 allocations for the
static routing case, and one allocation (the URLParams map) for the
normal routing case.
Let's just hope the GC does its job correctly and don't try to help it
out. This case is probably triggered very infrequently since most people
set up their middleware before they accept a single request, and it's
worth about 100ns of perf on the common case for us if we get rid of the
defer.
The fast routing diff introduced a regression with how method sets were
calculated for routes that did not match. This fixes that behavior, as
well as making routing considerably more memory-efficient (and therefore
CPU-efficient too) for the case in which many routes share a prefix.
Swap out the naive "try all the routes in order" router with a "compile
a trie down to bytecode" router. It's a ton faster, while providing all
the same semantics.
See the documentation at the top of web/fast_router.go for more.
Partially sort the routes on insertion. We're doing this so we can do
more efficient things to routes later.
The sorting rules are a bit subtle since we aren't allowed to rearrange
routes in a way that would cause the semantics to differ from the dumb
linear scan.
This middleware allows you to override a http.Request's RemoteAddr with
a value derived from either the X-Forwarded-For or X-Real-IP headers.
Fixes#12.
Provide a standard middleware to set c.Env. Don't include it in the
default stack, however, since the RequestID middleware will end up
allocating Env anyways.
Fixes#11
Instead of returning awkward untyped error strings, return real error
structs. This will allow users of the library to extract interesting
semantic meaning from our errors, and is just all around less awful than
what we had before.
Previously, we disallowed setting the empty string as a key in a map,
since at the time it seemed like doing so would allow all sorts of
unsavory bugs. In practice, I think this probably isn't actually true,
as I wasn't able to think of a scenario in which this bug would
materialize during the several moments I thought about it.
Plus, the code here to do sanity checking was wrong anyways.