Skip to content

Commit

Permalink
Merge pull request #12 from KlassnayaAfrodita/patch-1
Browse files Browse the repository at this point in the history
Create timeoutMiddleware.go
  • Loading branch information
hokamsingh authored Oct 14, 2024
2 parents a08e065 + d43e30b commit c08f775
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions internal/core/middleware/timeoutMiddleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package middleware

import (
"context"
"net/http"
"time"
)

type TimeoutMiddleware struct {
Timeout time.Duration
}

// NewTimeoutMiddleware creates a new instance of timeout middleware
func NewTimeoutMiddleware(timeout time.Duration) *TimeoutMiddleware {
return &TimeoutMiddleware{Timeout: timeout}
}

// Handle adds a timeout to the request context
func (tm *TimeoutMiddleware) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

ctx, cancel := context.WithTimeout(r.Context(), tm.Timeout)
defer cancel()

// Replace the request context with a new context with a timeout
r = r.WithContext(ctx)

done := make(chan struct{})
go func() {
next.ServeHTTP(w, r)
close(done)
}()

// End the request when the timeout is reached or after the main handler completes
select {
case <-done:
// The handler completed its work before the timeout
case <-ctx.Done():
// Timeout: cancel the request and return 504 status
http.Error(w, http.StatusText(http.StatusGatewayTimeout), http.StatusGatewayTimeout)
}
})
}

0 comments on commit c08f775

Please sign in to comment.