extend request deadline for applicatoin update

Signed-off-by: Ronni Skansing <rskansing@gmail.com>
This commit is contained in:
Ronni Skansing
2026-01-29 23:07:50 +01:00
parent 35b229908e
commit 3767adfad2
4 changed files with 32 additions and 2 deletions

View File

@@ -480,7 +480,7 @@ func setupRoutes(
GET(ROUTE_V1_VERSION, middleware.SessionHandler, controllers.Version.Get).
// update
GET(ROUTE_V1_UPDATE, middleware.SessionHandler, controllers.Update.GetUpdateDetails).
POST(ROUTE_V1_UPDATE, middleware.SessionHandler, controllers.Update.RunUpdate).
POST(ROUTE_V1_UPDATE, middleware.ExtendedTimeout(3*time.Minute), middleware.SessionHandler, controllers.Update.RunUpdate).
// backup
POST(ROUTE_V1_BACKUP_CREATE, middleware.SessionHandler, controllers.Backup.CreateBackup).
GET(ROUTE_V1_BACKUP_LIST, middleware.SessionHandler, controllers.Backup.ListBackups).

View File

@@ -1,6 +1,8 @@
package app
import (
"time"
"github.com/gin-gonic/gin"
"github.com/phishingclub/phishingclub/config"
"github.com/phishingclub/phishingclub/middleware"
@@ -41,3 +43,8 @@ func NewMiddlewares(
SessionHandler: sessionHandler,
}
}
// ExtendedTimeout returns a middleware that extends the write deadline for long-running operations.
func (m *Middlewares) ExtendedTimeout(timeout time.Duration) gin.HandlerFunc {
return middleware.ExtendedTimeout(timeout)
}

View File

@@ -0,0 +1,23 @@
package middleware
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
// ExtendedTimeout creates a middleware that extends the write deadline for long-running operations.
// this is necessary because the server's default WriteTimeout may be too short for operations
// like downloading updates.
func ExtendedTimeout(timeout time.Duration) gin.HandlerFunc {
return func(c *gin.Context) {
// get the underlying connection and extend the write deadline
rc := http.NewResponseController(c.Writer)
if err := rc.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Next()
}
}

View File

@@ -306,7 +306,7 @@ func (u *Update) RunUpdate(
req.Header.Set("User-Agent", "PhishingClub-Client")
client := &http.Client{
Timeout: 60 * time.Second, // Longer timeout for downloads
Timeout: 3 * time.Minute, // extended timeout for downloads
}
if !build.Flags.Production {