18 lines
318 B
Go
18 lines
318 B
Go
package runtime
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func MaxAllowed(n int) gin.HandlerFunc {
|
|
sem := make(chan struct{}, n)
|
|
acquire := func() { sem <- struct{}{} }
|
|
release := func() { <-sem }
|
|
return func(c *gin.Context) {
|
|
acquire() // before request
|
|
defer release() // after request
|
|
c.Next()
|
|
|
|
}
|
|
}
|