Skip to main content

41 posts tagged with "go"

View All Tags

Charged Twice: Safe Retries With Idempotency Keys in Fiber v3.7

ยท 33 min read
Fiber Team
Maintainers

A customer taps "Pay" on a train. The app sends POST /v1/orders, the server charges the card, writes the order and answers 201. The answer never arrives, because the train went into a tunnel. The app waits, times out and does what every good HTTP client does with a request that may not have reached the server: it sends it again. The server has no idea it has seen this request before. It charges the card a second time.

Retries are everywhere, and most of them are not in your code. Mobile SDKs retry on timeouts, service meshes retry on connection resets, job queues deliver a message again when a worker dies, and people press the button a second time when nothing seems to happen. For GET that is harmless. For a POST that moves money, sends an email or creates a ticket, it is a real bug. The usual fix is an idempotency key: the client picks a random value per operation and sends it with every attempt, and the server answers every repeat with the response of the first attempt instead of doing the work again.

Fiber has shipped an idempotency middleware since v2.42.0, and v3 carries it on. Fiber v3.7.0 made it fit for an endpoint like this one: keys can be scoped to the caller with KeyScope, cookies are no longer replayed to whoever sends a key (#4791), and headers that earlier middleware sets on every request are no longer copied into the stored response (#4834). This post builds the checkout endpoint from the story on top of it and then goes through the parts the middleware leaves to you. Everything was built and tested against Fiber v3.7.0 with Go 1.26.11, the Redis storage driver v3.6.4, go-redis v9.23.0 and Redis 7.0. KeyScope needs Fiber v3.7.0 or later, which needs Go 1.26.

Your Symlinks Return 404 on v3.7.0: Root Confinement in the Static Middleware

ยท 20 min read
Fiber Team
Maintainers

A shop runs on a single VM. Every deploy unpacks the build into a new directory under /srv/shop/releases, flips the current link to it, and reloads the service. Product photos and avatars live in /srv/shop/shared/uploads so they survive releases, and the deploy script links that directory into each release as public/uploads. The Fiber app serves /srv/shop/current/public with the static middleware and has done so without trouble for a year.

Then the app moves to Fiber v3.7.0. The tests pass, the deploy goes out, and every avatar on the site turns into a broken image. The CSS still loads, the home page still loads, and the logs show nothing but a stream of 404s for /uploads/.... A few minutes later someone notices that the Apple touch icon and the web fonts are gone too.

The app's code did not change. Fiber v3.7.0 ships fasthttp v1.78.0, and since fasthttp v1.77.0 the file server behind the static middleware resolves every path with Go's os.Root. A file root on disk is now confined: a symbolic link that leads out of it is treated like a missing file. The same release added an opt-out, AllowSymlinksOutsideRoot (#4803). The static documentation states the rule in one paragraph; this post is about what it does to a real deployment. It shows exactly which links are refused, a startup check that lists them before users find them, how to fix each kind without the opt-out, and the paths that the new rule does not cover at all. Everything was built and tested against Fiber v3.7.0 with Go 1.26.11. The option needs Fiber v3.7.0 or later, which needs Go 1.26.

Filters Nobody Planned For: Binding attr[color]=red Into Go Maps in Fiber v3.7

ยท 23 min read
Fiber Team
Maintainers

A shop sells jackets. The product list has filters for color and size, and the handler binds them into a struct with a Color field and a Size field. Then the merchandising team adds "material" to the product data, and a week later "hood" and "waterproof rating". Every new attribute is a new struct field, a new WHERE clause, a code review and a deploy, for something that is really just another row in the attributes table. The frontend team would rather send whatever the data has: ?attr[color]=red&attr[material]=down.

Plenty of APIs already speak this shape. PHP turns it into nested arrays and Rails into nested hashes, Stripe takes metadata[...] this way, JSON:API sets aside the filter parameter family for it, and Express parsed it into objects by default until version 5 switched its default query parser to a simpler one. In Fiber, a struct field of type map[string]string used to be skipped by the binder without a word. A request for exactly this has been open since 2023, and Fiber v3.7.0 closes it: map fields of a struct now bind from query strings and form bodies. This post builds a facet filter on top of it, backed by SQLite, and spends most of its time on what a map hands you: keys chosen by whoever sends the request. Everything was built and tested against Fiber v3.7.0 with modernc.org/sqlite v1.62.0. Map fields need Fiber v3.7.0 or later, which needs Go 1.26 like every release since v3.6.0.