2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.device;
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.atomic.AtomicInteger;
12 import javax.annotation.concurrent.GuardedBy;
14 abstract class SimpleRatelimiter {
15 private final AtomicInteger counter = new AtomicInteger();
16 private final int lowWatermark;
17 private int lowWatermarkEffective;
18 private final int highWatermark;
20 private volatile boolean limited;
22 SimpleRatelimiter(final int lowWatermark, final int highWatermark) {
23 Preconditions.checkArgument(lowWatermark >= 0);
24 Preconditions.checkArgument(highWatermark >= 0);
25 Preconditions.checkArgument(lowWatermark <= highWatermark);
27 this.lowWatermark = lowWatermark;
28 this.highWatermark = highWatermark;
29 lowWatermarkEffective = lowWatermark;
32 protected final boolean isLimited() {
36 protected abstract void disableFlow();
37 protected abstract void enableFlow();
39 boolean acquirePermit() {
40 final int cnt = counter.incrementAndGet();
41 if (cnt > highWatermark) {
42 synchronized (counter) {
43 final int recheck = counter.decrementAndGet();
44 if (recheck >= highWatermark && !limited) {
55 void releasePermit() {
56 final int cnt = counter.decrementAndGet();
57 if (cnt <= lowWatermarkEffective) {
58 synchronized (counter) {
59 final int recheck = counter.get();
60 if (recheck <= lowWatermarkEffective && limited) {
69 void resetLowWaterMark() {
70 synchronized (counter) {
71 lowWatermarkEffective = lowWatermark;
75 void adaptLowWaterMarkAndDisableFlow(int temporaryLowWaterMark) {
76 if (temporaryLowWaterMark < highWatermark) {
77 synchronized (counter) {
78 lowWatermarkEffective = temporaryLowWaterMark;
87 int getOccupiedPermits() {