github.com/go-graphite/carbonapi@v0.17.0/limiter/simple.go (about)

     1  package limiter
     2  
     3  import "context"
     4  
     5  type SimpleLimiter chan struct{}
     6  
     7  func (l SimpleLimiter) Enter(ctx context.Context) error {
     8  	if l == nil {
     9  		return nil
    10  	}
    11  
    12  	select {
    13  	case l <- struct{}{}:
    14  		return nil
    15  	case <-ctx.Done():
    16  		return ErrTimeout
    17  	}
    18  }
    19  
    20  func (l SimpleLimiter) Leave() {
    21  	if l != nil {
    22  		<-l
    23  	}
    24  }
    25  
    26  func NewSimpleLimiter(l int) SimpleLimiter {
    27  	return make(chan struct{}, l)
    28  }