This commit is contained in:
Phước Trung
2023-08-24 15:51:02 +07:00
committed by Sergey Alexandrovich
parent 07528dbecf
commit 16571e1360
2 changed files with 8 additions and 8 deletions

View File

@@ -208,7 +208,7 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
if queueSem != nil { if queueSem != nil {
token, aquired := queueSem.TryAquire() token, aquired := queueSem.TryAcquire()
if !aquired { if !aquired {
panic(ierrors.New(429, "Too many requests", "Too many requests")) panic(ierrors.New(429, "Too many requests", "Too many requests"))
} }
@@ -282,17 +282,17 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
} }
} }
// The heavy part start here, so we need to restrict workers number // The heavy part start here, so we need to restrict worker number
var processingSemToken *semaphore.Token var processingSemToken *semaphore.Token
func() { func() {
defer metrics.StartQueueSegment(ctx)() defer metrics.StartQueueSegment(ctx)()
var aquired bool var acquired bool
processingSemToken, aquired = processingSem.Aquire(ctx) processingSemToken, acquired = processingSem.Acquire(ctx)
if !aquired { if !acquired {
// We don't actually need to check timeout here, // We don't actually need to check timeout here,
// but it's an easy way to check if this is an actual timeout // but it's an easy way to check if this is an actual timeout
// or the request was cancelled // or the request was canceled
checkErr(ctx, "queue", router.CheckTimeout(ctx)) checkErr(ctx, "queue", router.CheckTimeout(ctx))
} }
}() }()

View File

@@ -15,7 +15,7 @@ func New(n int) *Semaphore {
} }
} }
func (s *Semaphore) Aquire(ctx context.Context) (*Token, bool) { func (s *Semaphore) Acquire(ctx context.Context) (*Token, bool) {
select { select {
case s.sem <- struct{}{}: case s.sem <- struct{}{}:
return &Token{release: s.release}, true return &Token{release: s.release}, true
@@ -24,7 +24,7 @@ func (s *Semaphore) Aquire(ctx context.Context) (*Token, bool) {
} }
} }
func (s *Semaphore) TryAquire() (*Token, bool) { func (s *Semaphore) TryAcquire() (*Token, bool) {
select { select {
case s.sem <- struct{}{}: case s.sem <- struct{}{}:
return &Token{release: s.release}, true return &Token{release: s.release}, true