8000 Switch to LIFO for object pool strategy · keevol/postgresql-async@577b6a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 577b6a8

Browse files
committed
Switch to LIFO for object pool strategy
Switch to use a stack datastructure to store connections. Current implementation uses FIFO strategy, which prevents unused connections from being closed because object pool goes through all connections. With LIFO strategy, unused connections will stay at the bottom of the stack and will be cleaned up during `testObjects` call. Additionally, declare `waitQueue` as `Queue` for clarity.
1 parent 8692fad commit 577b6a8

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

db-async-common/src/main/scala/com/github/mauricio/async/db/pool/SingleThreadedAsyncObjectPool.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.github.mauricio.async.db.pool
1919
import com.github.mauricio.async.db.util.{Log, Worker}
2020
import java.util.concurrent.atomic.AtomicLong
2121
import java.util.{TimerTask, Timer}
22-
import scala.collection.mutable.ArrayBuffer
22+
import scala.collection.mutable.{ArrayBuffer, Queue, Stack}
2323
import scala.concurrent.{Promise, Future}
2424
import scala.util.{Failure, Success}
2525

@@ -49,9 +49,9 @@ class SingleThreadedAsyncObjectPool[T](
4949
import SingleThreadedAsyncObjectPool.{Counter, log}
5050

5151
private val mainPool = Worker()
52-
private val poolables = new ArrayBuffer[PoolableHolder[T]](configuration.maxObjects)
52+
private var poolables = new Stack[PoolableHolder[T]]()
5353
private val checkouts = new ArrayBuffer[T](configuration.maxObjects)
54-
private val waitQueue = new ArrayBuffer[Promise[T]](configuration.maxQueueSize)
54+
private val waitQueue = new Queue[Promise[T]]()
5555
private val timer = new Timer("async-object-pool-timer-" + Counter.incrementAndGet(), true)
5656
timer.scheduleAtFixedRate(new TimerTask {
5757
def run() {
@@ -150,10 +150,10 @@ class SingleThreadedAsyncObjectPool[T](
150150
*/
151151

152152
private def addBack(item: T, promise: Promise[AsyncObjectPool[T]]) {
153-
this.poolables += new PoolableHolder[T](item)
153+
this.poolables.push(new PoolableHolder[T](item))
154154

155-
if (!this.waitQueue.isEmpty) {
156-
this.checkout(this.waitQueue.remove(0))
155+
if (this.waitQueue.nonEmpty) {
156+
this.checkout(this.waitQueue.dequeue())
157157
}
158158

159159
promise.success(this)
@@ -205,7 +205,7 @@ class SingleThreadedAsyncObjectPool[T](
205205
case e: Exception => promise.failure(e)
206206
}
207207
} else {
208-
val item = this.poolables.remove(0).item
208+
val item = this.poolables.pop().item
209209
this.checkouts += item
210210
promise.success(item)
211211
}
@@ -241,7 +241,7 @@ class SingleThreadedAsyncObjectPool[T](
241241
}
242242
}
243243
}
244-
this.poolables --= removals
244+
this.poolables = this.poolables.diff(removals)
245245
}
246246

247247
private class PoolableHolder[T](val item: T) {

0 commit comments

Comments
 (0)
0