RateLimiter.java
4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.google.common.util.concurrent;
import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Stopwatch;
import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.ThreadSafe;
import rq.a;
import rq.b;
@Beta
@ThreadSafe
public abstract class RateLimiter
{
private final a a;
private volatile Object b;
protected RateLimiter(a parama)
{
this.a = ((a)Preconditions.checkNotNull(parama));
}
private long a(int paramInt)
{
b(paramInt);
synchronized (c())
{
long l = b(paramInt, this.a.a());
return l;
}
}
private static int b(int paramInt)
{
if (paramInt > 0) {}
for (boolean bool = true;; bool = false)
{
Preconditions.checkArgument(bool, "Requested permits (%s) must be positive", new Object[] { Integer.valueOf(paramInt) });
return paramInt;
}
}
private long b(int paramInt, long paramLong)
{
return Math.max(a(paramInt, paramLong) - paramLong, 0L);
}
private Object c()
{
Object localObject1 = this.b;
if (localObject1 == null) {
try
{
Object localObject3 = this.b;
localObject1 = localObject3;
if (localObject3 == null)
{
localObject1 = new Object();
this.b = localObject1;
}
return localObject1;
}
finally {}
}
return localObject2;
}
public static RateLimiter create(double paramDouble)
{
rq.a locala = new rq.a(new RateLimiter.a.1());
locala.setRate(paramDouble);
return locala;
}
public static RateLimiter create(double paramDouble, long paramLong, TimeUnit paramTimeUnit)
{
if (paramLong >= 0L) {}
for (boolean bool = true;; bool = false)
{
Preconditions.checkArgument(bool, "warmupPeriod must not be negative: %s", new Object[] { Long.valueOf(paramLong) });
paramTimeUnit = new rq.b(new RateLimiter.a.1(), paramLong, paramTimeUnit);
paramTimeUnit.setRate(paramDouble);
return paramTimeUnit;
}
}
protected abstract double a();
protected abstract long a(int paramInt, long paramLong);
protected abstract void a(double paramDouble, long paramLong);
public double acquire()
{
return acquire(1);
}
public double acquire(int paramInt)
{
long l = a(paramInt);
this.a.a(l);
return l * 1.0D / TimeUnit.SECONDS.toMicros(1L);
}
protected abstract long b();
public final double getRate()
{
synchronized (c())
{
double d = a();
return d;
}
}
public final void setRate(double paramDouble)
{
if ((paramDouble > 0.0D) && (!Double.isNaN(paramDouble))) {}
for (boolean bool = true;; bool = false)
{
Preconditions.checkArgument(bool, "rate must be positive");
synchronized (c())
{
a(paramDouble, this.a.a());
return;
}
}
}
public String toString()
{
return String.format("RateLimiter[stableRate=%3.1fqps]", new Object[] { Double.valueOf(getRate()) });
}
public boolean tryAcquire()
{
return tryAcquire(1, 0L, TimeUnit.MICROSECONDS);
}
public boolean tryAcquire(int paramInt)
{
return tryAcquire(paramInt, 0L, TimeUnit.MICROSECONDS);
}
public boolean tryAcquire(int paramInt, long paramLong, TimeUnit arg4)
{
paramLong = Math.max(???.toMicros(paramLong), 0L);
b(paramInt);
for (;;)
{
synchronized (c())
{
long l = this.a.a();
if (b() - paramLong <= l)
{
i = 1;
if (i == 0) {
return false;
}
paramLong = b(paramInt, l);
this.a.a(paramLong);
return true;
}
}
int i = 0;
}
}
public boolean tryAcquire(long paramLong, TimeUnit paramTimeUnit)
{
return tryAcquire(1, paramLong, paramTimeUnit);
}
@VisibleForTesting
public static abstract class a
{
abstract long a();
abstract void a(long paramLong);
}
}
/* Location: /home/merong/decompile/hackery-dex2jar.jar!/com/google/common/util/concurrent/RateLimiter.class
* Java compiler version: 6 (50.0)
* JD-Core Version: 0.7.1
*/