ConnectionPool.java
8.66 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.squareup.okhttp;
import com.squareup.okhttp.internal.Platform;
import com.squareup.okhttp.internal.Util;
import java.net.SocketException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public final class ConnectionPool
{
private static final long DEFAULT_KEEP_ALIVE_DURATION_MS = 300000L;
private static final ConnectionPool systemDefault;
private final Deque<Connection> connections = new ArrayDeque();
private final Runnable connectionsCleanupRunnable = new Runnable()
{
public void run()
{
ConnectionPool.this.runCleanupUntilPoolIsEmpty();
}
};
private Executor executor = new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue(), Util.threadFactory("OkHttp ConnectionPool", true));
private final long keepAliveDurationNs;
private final int maxIdleConnections;
static
{
String str1 = System.getProperty("http.keepAlive");
String str2 = System.getProperty("http.keepAliveDuration");
String str3 = System.getProperty("http.maxConnections");
if (str2 != null) {}
for (long l = Long.parseLong(str2); (str1 != null) && (!Boolean.parseBoolean(str1)); l = 300000L)
{
systemDefault = new ConnectionPool(0, l);
return;
}
if (str3 != null)
{
systemDefault = new ConnectionPool(Integer.parseInt(str3), l);
return;
}
systemDefault = new ConnectionPool(5, l);
}
public ConnectionPool(int paramInt, long paramLong)
{
this.maxIdleConnections = paramInt;
this.keepAliveDurationNs = (paramLong * 1000L * 1000L);
}
private void addConnection(Connection paramConnection)
{
boolean bool = this.connections.isEmpty();
this.connections.addFirst(paramConnection);
if (bool)
{
this.executor.execute(this.connectionsCleanupRunnable);
return;
}
notifyAll();
}
public static ConnectionPool getDefault()
{
return systemDefault;
}
private void runCleanupUntilPoolIsEmpty()
{
while (performCleanup()) {}
}
public final void evictAll()
{
try
{
ArrayList localArrayList = new ArrayList(this.connections);
this.connections.clear();
notifyAll();
int j = localArrayList.size();
int i = 0;
while (i < j)
{
Util.closeQuietly(((Connection)localArrayList.get(i)).getSocket());
i += 1;
}
return;
}
finally {}
}
public final Connection get(Address paramAddress)
{
for (;;)
{
try
{
Iterator localIterator = this.connections.descendingIterator();
if (localIterator.hasNext())
{
Connection localConnection2 = (Connection)localIterator.next();
if ((localConnection2.getRoute().getAddress().equals(paramAddress)) && (localConnection2.isAlive()) && (System.nanoTime() - localConnection2.getIdleStartTimeNs() < this.keepAliveDurationNs))
{
localIterator.remove();
boolean bool = localConnection2.isFramed();
Connection localConnection1 = localConnection2;
if (!bool) {}
try
{
Platform.get().tagSocket(localConnection2.getSocket());
localConnection1 = localConnection2;
if ((localConnection1 != null) && (localConnection1.isFramed())) {
this.connections.addFirst(localConnection1);
}
return localConnection1;
}
catch (SocketException localSocketException)
{
Util.closeQuietly(localConnection2.getSocket());
Platform.get().logW("Unable to tagSocket(): " + localSocketException);
}
}
}
else
{
Object localObject = null;
}
}
finally {}
}
}
public final int getConnectionCount()
{
try
{
int i = this.connections.size();
return i;
}
finally
{
localObject = finally;
throw ((Throwable)localObject);
}
}
final List<Connection> getConnections()
{
try
{
ArrayList localArrayList = new ArrayList(this.connections);
return localArrayList;
}
finally
{
localObject = finally;
throw ((Throwable)localObject);
}
}
public final int getHttpConnectionCount()
{
try
{
int i = this.connections.size();
int j = getMultiplexedConnectionCount();
return i - j;
}
finally
{
localObject = finally;
throw ((Throwable)localObject);
}
}
public final int getMultiplexedConnectionCount()
{
int i = 0;
for (;;)
{
try
{
Iterator localIterator = this.connections.iterator();
if (localIterator.hasNext())
{
boolean bool = ((Connection)localIterator.next()).isFramed();
if (bool) {
i += 1;
}
}
else
{
return i;
}
}
finally {}
}
}
@Deprecated
public final int getSpdyConnectionCount()
{
try
{
int i = getMultiplexedConnectionCount();
return i;
}
finally
{
localObject = finally;
throw ((Throwable)localObject);
}
}
final boolean performCleanup()
{
int i;
long l2;
Connection localConnection;
long l3;
try
{
if (this.connections.isEmpty()) {
return false;
}
ArrayList localArrayList = new ArrayList();
i = 0;
l2 = System.nanoTime();
l1 = this.keepAliveDurationNs;
localIterator = this.connections.descendingIterator();
for (;;)
{
if (!localIterator.hasNext()) {
break label151;
}
localConnection = (Connection)localIterator.next();
l3 = localConnection.getIdleStartTimeNs() + this.keepAliveDurationNs - l2;
if ((l3 > 0L) && (localConnection.isAlive())) {
break;
}
localIterator.remove();
localArrayList.add(localConnection);
}
if (!localConnection.isIdle()) {
break label310;
}
}
finally {}
long l1 = Math.min(l1, l3);
i += 1;
break label313;
label151:
Iterator localIterator = this.connections.descendingIterator();
label310:
label313:
label316:
for (;;)
{
if ((localIterator.hasNext()) && (i > this.maxIdleConnections))
{
localConnection = (Connection)localIterator.next();
if (localConnection.isIdle())
{
((List)localObject).add(localConnection);
localIterator.remove();
i -= 1;
break label316;
}
}
else
{
boolean bool = ((List)localObject).isEmpty();
if (!bool) {}
}
try
{
l2 = l1 / 1000000L;
wait(l2, (int)(l1 - 1000000L * l2));
return true;
}
catch (InterruptedException localInterruptedException)
{
int j;
for (;;) {}
}
j = ((List)localObject).size();
i = 0;
while (i < j)
{
Util.closeQuietly(((Connection)((List)localObject).get(i)).getSocket());
i += 1;
}
return true;
break label316;
break;
}
}
final void recycle(Connection paramConnection)
{
if (paramConnection.isFramed()) {}
while (!paramConnection.clearOwner()) {
return;
}
if (!paramConnection.isAlive())
{
Util.closeQuietly(paramConnection.getSocket());
return;
}
try
{
Platform.get().untagSocket(paramConnection.getSocket());
try
{
addConnection(paramConnection);
paramConnection.incrementRecycleCount();
paramConnection.resetIdleStartTime();
return;
}
finally {}
return;
}
catch (SocketException localSocketException)
{
Platform.get().logW("Unable to untagSocket(): " + localSocketException);
Util.closeQuietly(paramConnection.getSocket());
}
}
final void replaceCleanupExecutorForTests(Executor paramExecutor)
{
this.executor = paramExecutor;
}
final void share(Connection paramConnection)
{
if (!paramConnection.isFramed()) {
throw new IllegalArgumentException();
}
if (!paramConnection.isAlive()) {
return;
}
try
{
addConnection(paramConnection);
return;
}
finally {}
}
}
/* Location: /home/merong/decompile/hackery-dex2jar.jar!/com/squareup/okhttp/ConnectionPool.class
* Java compiler version: 6 (50.0)
* JD-Core Version: 0.7.1
*/