feat: http router, command api

This commit is contained in:
Ilia Denisov
2026-01-07 13:52:20 +02:00
parent c9ed52b268
commit 1b0ab7a079
9 changed files with 404 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
package router
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
// LimitMiddleware limits number of concurrent connections using a buffered channel with limit spaces
func LimitMiddleware(limit int) gin.HandlerFunc {
if limit <= 0 {
panic("limit must be greater than 0")
}
semaphore := make(chan bool, limit)
t := time.NewTimer(time.Millisecond * 100)
return func(c *gin.Context) {
t.Reset(time.Millisecond * 100)
select {
case semaphore <- true:
c.Next()
<-semaphore
case <-t.C:
c.Status(http.StatusGatewayTimeout)
}
}
}