ConnectionPool.smali 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. .class public final Lokhttp3/ConnectionPool;
  2. .super Ljava/lang/Object;
  3. .source ""
  4. # static fields
  5. .field static final synthetic $assertionsDisabled:Z
  6. .field private static final executor:Ljava/util/concurrent/Executor;
  7. # instance fields
  8. .field private final cleanupRunnable:Ljava/lang/Runnable;
  9. .field cleanupRunning:Z
  10. .field private final connections:Ljava/util/Deque;
  11. .annotation system Ldalvik/annotation/Signature;
  12. value = {
  13. "Ljava/util/Deque<",
  14. "Lokhttp3/internal/connection/RealConnection;",
  15. ">;"
  16. }
  17. .end annotation
  18. .end field
  19. .field private final keepAliveDurationNs:J
  20. .field private final maxIdleConnections:I
  21. .field final routeDatabase:Lokhttp3/internal/connection/RouteDatabase;
  22. # direct methods
  23. .method static constructor <clinit>()V
  24. .locals 9
  25. new-instance v8, Ljava/util/concurrent/ThreadPoolExecutor;
  26. const/4 v1, 0x0
  27. const v2, 0x7fffffff
  28. const-wide/16 v3, 0x3c
  29. sget-object v5, Ljava/util/concurrent/TimeUnit;->SECONDS:Ljava/util/concurrent/TimeUnit;
  30. new-instance v6, Ljava/util/concurrent/SynchronousQueue;
  31. invoke-direct {v6}, Ljava/util/concurrent/SynchronousQueue;-><init>()V
  32. const/4 v0, 0x1
  33. const-string v7, "OkHttp ConnectionPool"
  34. invoke-static {v7, v0}, Lokhttp3/internal/Util;->threadFactory(Ljava/lang/String;Z)Ljava/util/concurrent/ThreadFactory;
  35. move-result-object v7
  36. move-object v0, v8
  37. invoke-direct/range {v0 .. v7}, Ljava/util/concurrent/ThreadPoolExecutor;-><init>(IIJLjava/util/concurrent/TimeUnit;Ljava/util/concurrent/BlockingQueue;Ljava/util/concurrent/ThreadFactory;)V
  38. sput-object v8, Lokhttp3/ConnectionPool;->executor:Ljava/util/concurrent/Executor;
  39. return-void
  40. .end method
  41. .method public constructor <init>()V
  42. .locals 4
  43. sget-object v0, Ljava/util/concurrent/TimeUnit;->MINUTES:Ljava/util/concurrent/TimeUnit;
  44. const/4 v1, 0x5
  45. const-wide/16 v2, 0x5
  46. invoke-direct {p0, v1, v2, v3, v0}, Lokhttp3/ConnectionPool;-><init>(IJLjava/util/concurrent/TimeUnit;)V
  47. return-void
  48. .end method
  49. .method public constructor <init>(IJLjava/util/concurrent/TimeUnit;)V
  50. .locals 2
  51. invoke-direct {p0}, Ljava/lang/Object;-><init>()V
  52. new-instance v0, Lokhttp3/ConnectionPool$1;
  53. invoke-direct {v0, p0}, Lokhttp3/ConnectionPool$1;-><init>(Lokhttp3/ConnectionPool;)V
  54. iput-object v0, p0, Lokhttp3/ConnectionPool;->cleanupRunnable:Ljava/lang/Runnable;
  55. new-instance v0, Ljava/util/ArrayDeque;
  56. invoke-direct {v0}, Ljava/util/ArrayDeque;-><init>()V
  57. iput-object v0, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  58. new-instance v0, Lokhttp3/internal/connection/RouteDatabase;
  59. invoke-direct {v0}, Lokhttp3/internal/connection/RouteDatabase;-><init>()V
  60. iput-object v0, p0, Lokhttp3/ConnectionPool;->routeDatabase:Lokhttp3/internal/connection/RouteDatabase;
  61. iput p1, p0, Lokhttp3/ConnectionPool;->maxIdleConnections:I
  62. invoke-virtual {p4, p2, p3}, Ljava/util/concurrent/TimeUnit;->toNanos(J)J
  63. move-result-wide v0
  64. iput-wide v0, p0, Lokhttp3/ConnectionPool;->keepAliveDurationNs:J
  65. const-wide/16 v0, 0x0
  66. cmp-long p1, p2, v0
  67. if-lez p1, :cond_0
  68. return-void
  69. :cond_0
  70. new-instance p1, Ljava/lang/IllegalArgumentException;
  71. new-instance p4, Ljava/lang/StringBuilder;
  72. invoke-direct {p4}, Ljava/lang/StringBuilder;-><init>()V
  73. const-string v0, "keepAliveDuration <= 0: "
  74. invoke-virtual {p4, v0}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
  75. invoke-virtual {p4, p2, p3}, Ljava/lang/StringBuilder;->append(J)Ljava/lang/StringBuilder;
  76. invoke-virtual {p4}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
  77. move-result-object p2
  78. invoke-direct {p1, p2}, Ljava/lang/IllegalArgumentException;-><init>(Ljava/lang/String;)V
  79. throw p1
  80. .end method
  81. .method private pruneAndGetAllocationCount(Lokhttp3/internal/connection/RealConnection;J)I
  82. .locals 6
  83. iget-object v0, p1, Lokhttp3/internal/connection/RealConnection;->allocations:Ljava/util/List;
  84. const/4 v1, 0x0
  85. const/4 v2, 0x0
  86. :cond_0
  87. :goto_0
  88. invoke-interface {v0}, Ljava/util/List;->size()I
  89. move-result v3
  90. if-ge v2, v3, :cond_2
  91. invoke-interface {v0, v2}, Ljava/util/List;->get(I)Ljava/lang/Object;
  92. move-result-object v3
  93. check-cast v3, Ljava/lang/ref/Reference;
  94. invoke-virtual {v3}, Ljava/lang/ref/Reference;->get()Ljava/lang/Object;
  95. move-result-object v4
  96. if-eqz v4, :cond_1
  97. add-int/lit8 v2, v2, 0x1
  98. goto :goto_0
  99. :cond_1
  100. check-cast v3, Lokhttp3/internal/connection/StreamAllocation$StreamAllocationReference;
  101. new-instance v4, Ljava/lang/StringBuilder;
  102. invoke-direct {v4}, Ljava/lang/StringBuilder;-><init>()V
  103. const-string v5, "A connection to "
  104. invoke-virtual {v4, v5}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
  105. invoke-virtual {p1}, Lokhttp3/internal/connection/RealConnection;->route()Lokhttp3/Route;
  106. move-result-object v5
  107. invoke-virtual {v5}, Lokhttp3/Route;->address()Lokhttp3/Address;
  108. move-result-object v5
  109. invoke-virtual {v5}, Lokhttp3/Address;->url()Lokhttp3/HttpUrl;
  110. move-result-object v5
  111. invoke-virtual {v4, v5}, Ljava/lang/StringBuilder;->append(Ljava/lang/Object;)Ljava/lang/StringBuilder;
  112. const-string v5, " was leaked. Did you forget to close a response body?"
  113. invoke-virtual {v4, v5}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
  114. invoke-virtual {v4}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
  115. move-result-object v4
  116. invoke-static {}, Lokhttp3/internal/platform/Platform;->get()Lokhttp3/internal/platform/Platform;
  117. move-result-object v5
  118. iget-object v3, v3, Lokhttp3/internal/connection/StreamAllocation$StreamAllocationReference;->callStackTrace:Ljava/lang/Object;
  119. invoke-virtual {v5, v4, v3}, Lokhttp3/internal/platform/Platform;->logCloseableLeak(Ljava/lang/String;Ljava/lang/Object;)V
  120. invoke-interface {v0, v2}, Ljava/util/List;->remove(I)Ljava/lang/Object;
  121. const/4 v3, 0x1
  122. iput-boolean v3, p1, Lokhttp3/internal/connection/RealConnection;->noNewStreams:Z
  123. invoke-interface {v0}, Ljava/util/List;->isEmpty()Z
  124. move-result v3
  125. if-eqz v3, :cond_0
  126. iget-wide v2, p0, Lokhttp3/ConnectionPool;->keepAliveDurationNs:J
  127. sub-long/2addr p2, v2
  128. iput-wide p2, p1, Lokhttp3/internal/connection/RealConnection;->idleAtNanos:J
  129. return v1
  130. :cond_2
  131. invoke-interface {v0}, Ljava/util/List;->size()I
  132. move-result p1
  133. return p1
  134. .end method
  135. # virtual methods
  136. .method cleanup(J)J
  137. .locals 11
  138. monitor-enter p0
  139. :try_start_0
  140. iget-object v0, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  141. invoke-interface {v0}, Ljava/util/Deque;->iterator()Ljava/util/Iterator;
  142. move-result-object v0
  143. const/4 v1, 0x0
  144. const/4 v2, 0x0
  145. const-wide/high16 v3, -0x8000000000000000L
  146. move-object v5, v2
  147. const/4 v2, 0x0
  148. const/4 v6, 0x0
  149. :cond_0
  150. :goto_0
  151. invoke-interface {v0}, Ljava/util/Iterator;->hasNext()Z
  152. move-result v7
  153. if-eqz v7, :cond_2
  154. invoke-interface {v0}, Ljava/util/Iterator;->next()Ljava/lang/Object;
  155. move-result-object v7
  156. check-cast v7, Lokhttp3/internal/connection/RealConnection;
  157. invoke-direct {p0, v7, p1, p2}, Lokhttp3/ConnectionPool;->pruneAndGetAllocationCount(Lokhttp3/internal/connection/RealConnection;J)I
  158. move-result v8
  159. if-lez v8, :cond_1
  160. add-int/lit8 v6, v6, 0x1
  161. goto :goto_0
  162. :cond_1
  163. add-int/lit8 v2, v2, 0x1
  164. iget-wide v8, v7, Lokhttp3/internal/connection/RealConnection;->idleAtNanos:J
  165. sub-long v8, p1, v8
  166. cmp-long v10, v8, v3
  167. if-lez v10, :cond_0
  168. move-object v5, v7
  169. move-wide v3, v8
  170. goto :goto_0
  171. :cond_2
  172. iget-wide p1, p0, Lokhttp3/ConnectionPool;->keepAliveDurationNs:J
  173. cmp-long v0, v3, p1
  174. if-gez v0, :cond_6
  175. iget p1, p0, Lokhttp3/ConnectionPool;->maxIdleConnections:I
  176. if-le v2, p1, :cond_3
  177. goto :goto_1
  178. :cond_3
  179. if-lez v2, :cond_4
  180. iget-wide p1, p0, Lokhttp3/ConnectionPool;->keepAliveDurationNs:J
  181. sub-long/2addr p1, v3
  182. monitor-exit p0
  183. return-wide p1
  184. :cond_4
  185. if-lez v6, :cond_5
  186. iget-wide p1, p0, Lokhttp3/ConnectionPool;->keepAliveDurationNs:J
  187. monitor-exit p0
  188. return-wide p1
  189. :cond_5
  190. iput-boolean v1, p0, Lokhttp3/ConnectionPool;->cleanupRunning:Z
  191. const-wide/16 p1, -0x1
  192. monitor-exit p0
  193. return-wide p1
  194. :cond_6
  195. :goto_1
  196. iget-object p1, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  197. invoke-interface {p1, v5}, Ljava/util/Deque;->remove(Ljava/lang/Object;)Z
  198. monitor-exit p0
  199. :try_end_0
  200. .catchall {:try_start_0 .. :try_end_0} :catchall_0
  201. invoke-virtual {v5}, Lokhttp3/internal/connection/RealConnection;->socket()Ljava/net/Socket;
  202. move-result-object p1
  203. invoke-static {p1}, Lokhttp3/internal/Util;->closeQuietly(Ljava/net/Socket;)V
  204. const-wide/16 p1, 0x0
  205. return-wide p1
  206. :catchall_0
  207. move-exception p1
  208. :try_start_1
  209. monitor-exit p0
  210. :try_end_1
  211. .catchall {:try_start_1 .. :try_end_1} :catchall_0
  212. goto :goto_3
  213. :goto_2
  214. throw p1
  215. :goto_3
  216. goto :goto_2
  217. .end method
  218. .method connectionBecameIdle(Lokhttp3/internal/connection/RealConnection;)Z
  219. .locals 1
  220. iget-boolean v0, p1, Lokhttp3/internal/connection/RealConnection;->noNewStreams:Z
  221. if-nez v0, :cond_1
  222. iget v0, p0, Lokhttp3/ConnectionPool;->maxIdleConnections:I
  223. if-nez v0, :cond_0
  224. goto :goto_0
  225. :cond_0
  226. invoke-virtual {p0}, Ljava/lang/Object;->notifyAll()V
  227. const/4 p1, 0x0
  228. return p1
  229. :cond_1
  230. :goto_0
  231. iget-object v0, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  232. invoke-interface {v0, p1}, Ljava/util/Deque;->remove(Ljava/lang/Object;)Z
  233. const/4 p1, 0x1
  234. return p1
  235. .end method
  236. .method public declared-synchronized connectionCount()I
  237. .locals 1
  238. monitor-enter p0
  239. :try_start_0
  240. iget-object v0, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  241. invoke-interface {v0}, Ljava/util/Deque;->size()I
  242. move-result v0
  243. :try_end_0
  244. .catchall {:try_start_0 .. :try_end_0} :catchall_0
  245. monitor-exit p0
  246. return v0
  247. :catchall_0
  248. move-exception v0
  249. monitor-exit p0
  250. throw v0
  251. .end method
  252. .method deduplicate(Lokhttp3/Address;Lokhttp3/internal/connection/StreamAllocation;)Ljava/net/Socket;
  253. .locals 3
  254. iget-object v0, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  255. invoke-interface {v0}, Ljava/util/Deque;->iterator()Ljava/util/Iterator;
  256. move-result-object v0
  257. :cond_0
  258. invoke-interface {v0}, Ljava/util/Iterator;->hasNext()Z
  259. move-result v1
  260. const/4 v2, 0x0
  261. if-eqz v1, :cond_1
  262. invoke-interface {v0}, Ljava/util/Iterator;->next()Ljava/lang/Object;
  263. move-result-object v1
  264. check-cast v1, Lokhttp3/internal/connection/RealConnection;
  265. invoke-virtual {v1, p1, v2}, Lokhttp3/internal/connection/RealConnection;->isEligible(Lokhttp3/Address;Lokhttp3/Route;)Z
  266. move-result v2
  267. if-eqz v2, :cond_0
  268. invoke-virtual {v1}, Lokhttp3/internal/connection/RealConnection;->isMultiplexed()Z
  269. move-result v2
  270. if-eqz v2, :cond_0
  271. invoke-virtual {p2}, Lokhttp3/internal/connection/StreamAllocation;->connection()Lokhttp3/internal/connection/RealConnection;
  272. move-result-object v2
  273. if-eq v1, v2, :cond_0
  274. invoke-virtual {p2, v1}, Lokhttp3/internal/connection/StreamAllocation;->releaseAndAcquire(Lokhttp3/internal/connection/RealConnection;)Ljava/net/Socket;
  275. move-result-object p1
  276. return-object p1
  277. :cond_1
  278. return-object v2
  279. .end method
  280. .method public evictAll()V
  281. .locals 4
  282. new-instance v0, Ljava/util/ArrayList;
  283. invoke-direct {v0}, Ljava/util/ArrayList;-><init>()V
  284. monitor-enter p0
  285. :try_start_0
  286. iget-object v1, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  287. invoke-interface {v1}, Ljava/util/Deque;->iterator()Ljava/util/Iterator;
  288. move-result-object v1
  289. :cond_0
  290. :goto_0
  291. invoke-interface {v1}, Ljava/util/Iterator;->hasNext()Z
  292. move-result v2
  293. if-eqz v2, :cond_1
  294. invoke-interface {v1}, Ljava/util/Iterator;->next()Ljava/lang/Object;
  295. move-result-object v2
  296. check-cast v2, Lokhttp3/internal/connection/RealConnection;
  297. iget-object v3, v2, Lokhttp3/internal/connection/RealConnection;->allocations:Ljava/util/List;
  298. invoke-interface {v3}, Ljava/util/List;->isEmpty()Z
  299. move-result v3
  300. if-eqz v3, :cond_0
  301. const/4 v3, 0x1
  302. iput-boolean v3, v2, Lokhttp3/internal/connection/RealConnection;->noNewStreams:Z
  303. invoke-interface {v0, v2}, Ljava/util/List;->add(Ljava/lang/Object;)Z
  304. invoke-interface {v1}, Ljava/util/Iterator;->remove()V
  305. goto :goto_0
  306. :cond_1
  307. monitor-exit p0
  308. :try_end_0
  309. .catchall {:try_start_0 .. :try_end_0} :catchall_0
  310. invoke-interface {v0}, Ljava/util/List;->iterator()Ljava/util/Iterator;
  311. move-result-object v0
  312. :goto_1
  313. invoke-interface {v0}, Ljava/util/Iterator;->hasNext()Z
  314. move-result v1
  315. if-eqz v1, :cond_2
  316. invoke-interface {v0}, Ljava/util/Iterator;->next()Ljava/lang/Object;
  317. move-result-object v1
  318. check-cast v1, Lokhttp3/internal/connection/RealConnection;
  319. invoke-virtual {v1}, Lokhttp3/internal/connection/RealConnection;->socket()Ljava/net/Socket;
  320. move-result-object v1
  321. invoke-static {v1}, Lokhttp3/internal/Util;->closeQuietly(Ljava/net/Socket;)V
  322. goto :goto_1
  323. :cond_2
  324. return-void
  325. :catchall_0
  326. move-exception v0
  327. :try_start_1
  328. monitor-exit p0
  329. :try_end_1
  330. .catchall {:try_start_1 .. :try_end_1} :catchall_0
  331. goto :goto_3
  332. :goto_2
  333. throw v0
  334. :goto_3
  335. goto :goto_2
  336. .end method
  337. .method get(Lokhttp3/Address;Lokhttp3/internal/connection/StreamAllocation;Lokhttp3/Route;)Lokhttp3/internal/connection/RealConnection;
  338. .locals 3
  339. iget-object v0, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  340. invoke-interface {v0}, Ljava/util/Deque;->iterator()Ljava/util/Iterator;
  341. move-result-object v0
  342. :cond_0
  343. invoke-interface {v0}, Ljava/util/Iterator;->hasNext()Z
  344. move-result v1
  345. if-eqz v1, :cond_1
  346. invoke-interface {v0}, Ljava/util/Iterator;->next()Ljava/lang/Object;
  347. move-result-object v1
  348. check-cast v1, Lokhttp3/internal/connection/RealConnection;
  349. invoke-virtual {v1, p1, p3}, Lokhttp3/internal/connection/RealConnection;->isEligible(Lokhttp3/Address;Lokhttp3/Route;)Z
  350. move-result v2
  351. if-eqz v2, :cond_0
  352. const/4 p1, 0x1
  353. invoke-virtual {p2, v1, p1}, Lokhttp3/internal/connection/StreamAllocation;->acquire(Lokhttp3/internal/connection/RealConnection;Z)V
  354. return-object v1
  355. :cond_1
  356. const/4 p1, 0x0
  357. return-object p1
  358. .end method
  359. .method public declared-synchronized idleConnectionCount()I
  360. .locals 3
  361. monitor-enter p0
  362. const/4 v0, 0x0
  363. :try_start_0
  364. iget-object v1, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  365. invoke-interface {v1}, Ljava/util/Deque;->iterator()Ljava/util/Iterator;
  366. move-result-object v1
  367. :cond_0
  368. :goto_0
  369. invoke-interface {v1}, Ljava/util/Iterator;->hasNext()Z
  370. move-result v2
  371. if-eqz v2, :cond_1
  372. invoke-interface {v1}, Ljava/util/Iterator;->next()Ljava/lang/Object;
  373. move-result-object v2
  374. check-cast v2, Lokhttp3/internal/connection/RealConnection;
  375. iget-object v2, v2, Lokhttp3/internal/connection/RealConnection;->allocations:Ljava/util/List;
  376. invoke-interface {v2}, Ljava/util/List;->isEmpty()Z
  377. move-result v2
  378. :try_end_0
  379. .catchall {:try_start_0 .. :try_end_0} :catchall_0
  380. if-eqz v2, :cond_0
  381. add-int/lit8 v0, v0, 0x1
  382. goto :goto_0
  383. :cond_1
  384. monitor-exit p0
  385. return v0
  386. :catchall_0
  387. move-exception v0
  388. monitor-exit p0
  389. goto :goto_2
  390. :goto_1
  391. throw v0
  392. :goto_2
  393. goto :goto_1
  394. .end method
  395. .method put(Lokhttp3/internal/connection/RealConnection;)V
  396. .locals 2
  397. iget-boolean v0, p0, Lokhttp3/ConnectionPool;->cleanupRunning:Z
  398. if-nez v0, :cond_0
  399. const/4 v0, 0x1
  400. iput-boolean v0, p0, Lokhttp3/ConnectionPool;->cleanupRunning:Z
  401. sget-object v0, Lokhttp3/ConnectionPool;->executor:Ljava/util/concurrent/Executor;
  402. iget-object v1, p0, Lokhttp3/ConnectionPool;->cleanupRunnable:Ljava/lang/Runnable;
  403. invoke-interface {v0, v1}, Ljava/util/concurrent/Executor;->execute(Ljava/lang/Runnable;)V
  404. :cond_0
  405. iget-object v0, p0, Lokhttp3/ConnectionPool;->connections:Ljava/util/Deque;
  406. invoke-interface {v0, p1}, Ljava/util/Deque;->add(Ljava/lang/Object;)Z
  407. return-void
  408. .end method