2024-09-24 23:07:42 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-09-24 23:21:48 +02:00
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-09-24 23:07:42 +02:00
|
|
|
"time"
|
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-09-24 23:07:42 +02:00
|
|
|
)
|
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
func Timeout(d time.Duration) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-09-24 23:21:48 +02:00
|
|
|
finish := make(chan struct{}, 1)
|
|
|
|
panicChan := make(chan interface{}, 1)
|
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
durationCtx, cancel := context.WithTimeout(c.Request.Context(), d)
|
2024-09-24 23:21:48 +02:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Handle panic
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
panicChan <- p
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Run the next middleware or the business logic
|
|
|
|
c.Next()
|
|
|
|
|
|
|
|
finish <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case p := <-panicChan:
|
|
|
|
// panic
|
|
|
|
log.Println(p)
|
2024-09-28 11:13:18 +02:00
|
|
|
c.Status(http.StatusInternalServerError)
|
2024-09-24 23:21:48 +02:00
|
|
|
case <-finish:
|
|
|
|
// finish normally
|
|
|
|
log.Println("finish")
|
|
|
|
case <-durationCtx.Done():
|
2024-09-28 11:13:18 +02:00
|
|
|
c.JSON(http.StatusRequestTimeout, "time out")
|
2024-09-24 23:21:48 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-24 23:07:42 +02:00
|
|
|
}
|