Skip to content

Instantly share code, notes, and snippets.

@robgee86
robgee86 / handler.go
Created September 18, 2024 09:42
Block consecutive WriteHeader
import (
"context"
"sync/atomic"
"github.com/felixge/httpsnoop"
)
// BlockConsecutiveWriteHeader is a middleware that prevents multiple WriteHeader calls on the http.ResponseWriter.
// This scenario leads to a server-side errors that indicate a wrong handler implementation. Despite this being a
// useful information, often in prod applications we still don't want our logs to be flooded, and it's in these cases