Skip to content

Commit

Permalink
updated the golint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shanmydell committed Dec 22, 2023
1 parent 19e5184 commit cf1f00d
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions api/throttling.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,49 @@ func (e *TimeoutSemaphoreError) Error() string {
return e.msg
}

type timeoutSemaphore struct {
timeout time.Duration
semaphore chan struct{}
logger Logger
type TimeoutSemaphore struct {
Timeout time.Duration
Semaphore chan struct{}
Logger Logger
}

func NewTimeoutSemaphore(timeout, rateLimit int, logger Logger) *timeoutSemaphore {
func NewTimeoutSemaphore(timeout, rateLimit int, logger Logger) *TimeoutSemaphore {
log := logger

if log == nil {
log = &defaultLogger{}
}

return &timeoutSemaphore{
timeout: time.Duration(timeout) * time.Second,
semaphore: make(chan struct{}, rateLimit),
logger: log,
return &TimeoutSemaphore{
Timeout: time.Duration(timeout) * time.Second,
Semaphore: make(chan struct{}, rateLimit),
Logger: log,
}
}

func (ts *timeoutSemaphore) Acquire(ctx context.Context) error {
func (ts *TimeoutSemaphore) Acquire(ctx context.Context) error {
var cancelFunc func()
ctx, cancelFunc = context.WithTimeout(ctx, ts.timeout)
ctx, cancelFunc = context.WithTimeout(ctx, ts.Timeout)
defer cancelFunc()
for {
select {
case ts.semaphore <- struct{}{}:
ts.logger.Debug(ctx, "acquire a lock")
case ts.Semaphore <- struct{}{}:
ts.Logger.Debug(ctx, "acquire a lock")
return nil
case <-ctx.Done():
msg := "lock is acquire failed, timeout expired"
ts.logger.Error(ctx, msg)
ts.Logger.Error(ctx, msg)
return &TimeoutSemaphoreError{msg}
}
}
}

func (ts *timeoutSemaphore) Release(ctx context.Context) {
<-ts.semaphore
ts.logger.Debug(ctx, "release a lock")
func (ts *TimeoutSemaphore) Release(ctx context.Context) {
<-ts.Semaphore
ts.Logger.Debug(ctx, "release a lock")
}

func (ts *timeoutSemaphore) SetLogger(logger Logger) TimeoutSemaphoreInterface {
ts.logger = logger
func (ts *TimeoutSemaphore) SetLogger(logger Logger) TimeoutSemaphoreInterface {
ts.Logger = logger
return ts
}

0 comments on commit cf1f00d

Please sign in to comment.