github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/flowcontrol/throttle.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flowcontrol
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/juju/ratelimit"
    23  )
    24  
    25  type RateLimiter interface {
    26  	// TryAccept returns true if a token is taken immediately. Otherwise,
    27  	// it returns false.
    28  	TryAccept() bool
    29  	// Accept returns once a token becomes available.
    30  	Accept()
    31  	// Stop stops the rate limiter, subsequent calls to CanAccept will return false
    32  	Stop()
    33  	// Saturation returns a percentage number which describes how saturated
    34  	// this rate limiter is.
    35  	// Usually we use token bucket rate limiter. In that case,
    36  	// 1.0 means no tokens are available; 0.0 means we have a full bucket of tokens to use.
    37  	Saturation() float64
    38  }
    39  
    40  type tokenBucketRateLimiter struct {
    41  	limiter *ratelimit.Bucket
    42  }
    43  
    44  // NewTokenBucketRateLimiter creates a rate limiter which implements a token bucket approach.
    45  // The rate limiter allows bursts of up to 'burst' to exceed the QPS, while still maintaining a
    46  // smoothed qps rate of 'qps'.
    47  // The bucket is initially filled with 'burst' tokens, and refills at a rate of 'qps'.
    48  // The maximum number of tokens in the bucket is capped at 'burst'.
    49  func NewTokenBucketRateLimiter(qps float32, burst int) RateLimiter {
    50  	limiter := ratelimit.NewBucketWithRate(float64(qps), int64(burst))
    51  	return &tokenBucketRateLimiter{limiter}
    52  }
    53  
    54  func (t *tokenBucketRateLimiter) TryAccept() bool {
    55  	return t.limiter.TakeAvailable(1) == 1
    56  }
    57  
    58  func (t *tokenBucketRateLimiter) Saturation() float64 {
    59  	capacity := t.limiter.Capacity()
    60  	avail := t.limiter.Available()
    61  	return float64(capacity-avail) / float64(capacity)
    62  }
    63  
    64  // Accept will block until a token becomes available
    65  func (t *tokenBucketRateLimiter) Accept() {
    66  	t.limiter.Wait(1)
    67  }
    68  
    69  func (t *tokenBucketRateLimiter) Stop() {
    70  }
    71  
    72  type fakeAlwaysRateLimiter struct{}
    73  
    74  func NewFakeAlwaysRateLimiter() RateLimiter {
    75  	return &fakeAlwaysRateLimiter{}
    76  }
    77  
    78  func (t *fakeAlwaysRateLimiter) TryAccept() bool {
    79  	return true
    80  }
    81  
    82  func (t *fakeAlwaysRateLimiter) Saturation() float64 {
    83  	return 0
    84  }
    85  
    86  func (t *fakeAlwaysRateLimiter) Stop() {}
    87  
    88  func (t *fakeAlwaysRateLimiter) Accept() {}
    89  
    90  type fakeNeverRateLimiter struct {
    91  	wg sync.WaitGroup
    92  }
    93  
    94  func NewFakeNeverRateLimiter() RateLimiter {
    95  	wg := sync.WaitGroup{}
    96  	wg.Add(1)
    97  	return &fakeNeverRateLimiter{
    98  		wg: wg,
    99  	}
   100  }
   101  
   102  func (t *fakeNeverRateLimiter) TryAccept() bool {
   103  	return false
   104  }
   105  
   106  func (t *fakeNeverRateLimiter) Saturation() float64 {
   107  	return 1
   108  }
   109  
   110  func (t *fakeNeverRateLimiter) Stop() {
   111  	t.wg.Done()
   112  }
   113  
   114  func (t *fakeNeverRateLimiter) Accept() {
   115  	t.wg.Wait()
   116  }