Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues/92: http.NewResponseController #213

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ func Run(h http.Handler, o Opts, l log.Logger) error {
// 2. https://blog.cloudflare.com/exposing-go-on-the-internet/
// 3. https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
// 4. https://github.com/golang/go/issues/27375
Handler: http.TimeoutHandler(
Handler: timeoutHandler(
h,
o.handlerTimeout,
fmt.Sprintf("ong: Handler timeout exceeded: %s", o.handlerTimeout),
// fmt.Sprintf("ong: Handler timeout exceeded: %s", o.handlerTimeout),
),
ReadHeaderTimeout: o.readHeaderTimeout,
ReadTimeout: o.readTimeout,
Expand Down Expand Up @@ -373,3 +373,22 @@ func listenerConfig() *net.ListenConfig {
},
}
}

func timeoutHandler(wrappedHandler http.Handler, handlerTimeout time.Duration) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rc := http.NewResponseController(w)

handlerTimeout := 2 * time.Second
now := time.Now()

if err := rc.SetReadDeadline(now.Add(handlerTimeout)); err != nil {
fmt.Printf("\n\n\t SetReadDeadline err= %v\n", err)
}

if err := rc.SetWriteDeadline(now.Add(handlerTimeout)); err != nil {
fmt.Printf("\n\n\t SetWriteDeadline err= %v\n", err)
}

wrappedHandler.ServeHTTP(w, r)
}
}