Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.services.simpleworkflow.flow.worker;
 
 
 public class Throttler {
 
     private static final Log log = LogFactory.getLog(Throttler.class);
    
    
Human readable name of the resource being throttled. Used for logging only.
 
     private final String name_;

    
Used as a circular buffer
 
     private CircularLongBuffer checkPointTimes_;

    
Used as an index to a circular buffer
 
     private long index_;

    
Interval used to measure the rate. Shorter interval allows less spikey rates.
 
     private long rateInterval_;
 
     private long rateIntervalMilliseconds_;
 
     private long overslept_;

    
Construct throttler.

Parameters:
name Human readable name of the resource being throttled. Used for logging only.
maxRatePerSecond maximum rate allowed
rateIntervalMilliseconds rate measurement interval. Interval should be at least 1000 / maxRatePerSecond.
 
     public Throttler(String namedouble maxRatePerSecondlong rateIntervalMilliseconds) {
         if (null == name) {
             throw new IllegalArgumentException("null name");
         }
          = name;
         if (maxRatePerSecond <= 0) {
             throw new IllegalArgumentException("0 or negative maxRatePerSecond");
         }
         if (rateIntervalMilliseconds <= 0) {
             throw new IllegalArgumentException("0 or negative rateIntervalMilliseconds");
         }
         synchronized(this) {
              = rateIntervalMilliseconds;
             setMaxRatePerSecond(maxRatePerSecond);
         }
     }
 
     public synchronized void setMaxRatePerSecond(double maxRatePerSecond) {
         int maxMessagesPerRateInterval = (int) (maxRatePerSecond *  / 1000);
         if (maxMessagesPerRateInterval == 0) {
             maxMessagesPerRateInterval = 1;
              =  (long) (1.0 / maxRatePerSecond * 1000.0);
         } else {
              = ;
         }
         if ( != null) {
             int oldSize = .size();
              = .copy( - maxMessagesPerRateIntervalmaxMessagesPerRateInterval);
              = Math.min(.size(), oldSize);
         } else {
              = new CircularLongBuffer(maxMessagesPerRateInterval);
              = 0;
         }
         .debug("new rate=" + maxRatePerSecond + " (msg/sec)");
     }
 
     public synchronized void throttle(int countthrows InterruptedException {
         for(int i=0; i<count; ++i) {
             throttle();
         }
     }
    
    
When called on each request sleeps if called faster then configured average rate.

Throws:
java.lang.InterruptedException when interrupted
    public synchronized void throttle() throws InterruptedException {
        long now = System.currentTimeMillis();
        long checkPoint = .get();
        if (checkPoint > 0) {
            long elapsed = now - checkPoint;
            
            // if the time for this window is less than the minimum per window
            if (elapsed >= 0 && elapsed < ) {
                long sleepInterval =  - elapsed - ;
                 = 0;
                if (sleepInterval > 0) {
                    if (.isTraceEnabled()) {
                        .debug("Throttling "
                                + 
                                + ": called "
                                + .size()
                                + " times in last "
                                + elapsed
                                + " milliseconds. Going to sleep for "
                                + sleepInterval
                                + " milliseconds.");
                    }
                    long t = System.currentTimeMillis();
                    Thread.sleep(sleepInterval);
                     = System.currentTimeMillis() - t - sleepInterval;
                }
            }
        }
        .set(++, System.currentTimeMillis());
    }
    
New to GrepCode? Check out our FAQ X