2020-07-22 13:02:32 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
2021-08-09 08:53:58 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2020-07-22 13:02:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-04-13 16:56:58 +00:00
|
|
|
// MaxClients provides HTTP handler wrapper with the client limit.
|
2020-07-22 13:02:32 +00:00
|
|
|
MaxClients interface {
|
|
|
|
Handle(http.HandlerFunc) http.HandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
maxClients struct {
|
|
|
|
pool chan struct{}
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultRequestDeadline = time.Second * 30
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// NewMaxClientsMiddleware returns MaxClients interface with handler wrapper based on
|
2022-04-13 16:56:58 +00:00
|
|
|
// the provided count and the timeout limits.
|
2020-07-22 13:02:32 +00:00
|
|
|
func NewMaxClientsMiddleware(count int, timeout time.Duration) MaxClients {
|
|
|
|
if timeout <= 0 {
|
|
|
|
timeout = defaultRequestDeadline
|
|
|
|
}
|
|
|
|
|
|
|
|
return &maxClients{
|
|
|
|
pool: make(chan struct{}, count),
|
|
|
|
timeout: timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// Handler wraps HTTP handler function with logic limiting access to it.
|
2020-07-22 13:02:32 +00:00
|
|
|
func (m *maxClients) Handle(f http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if m.pool == nil {
|
|
|
|
f.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
deadline := time.NewTimer(m.timeout)
|
|
|
|
defer deadline.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case m.pool <- struct{}{}:
|
|
|
|
defer func() { <-m.pool }()
|
|
|
|
f.ServeHTTP(w, r)
|
|
|
|
case <-deadline.C:
|
|
|
|
// Send a http timeout message
|
2021-08-09 08:53:58 +00:00
|
|
|
WriteErrorResponse(w, GetReqInfo(r.Context()), errors.GetAPIError(errors.ErrOperationTimedOut))
|
2020-07-22 13:02:32 +00:00
|
|
|
return
|
|
|
|
case <-r.Context().Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|