Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.github.mauricio.async.db.pool
import com.github.mauricio.async.db.util.{Log, Worker}
import java.util.concurrent.atomic.AtomicLong
import java.util.{TimerTask, Timer}
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.{ArrayBuffer, Queue, Stack}
import scala.concurrent.{Promise, Future}
import scala.util.{Failure, Success}

Expand Down Expand Up @@ -49,9 +49,9 @@ class SingleThreadedAsyncObjectPool[T](
import SingleThreadedAsyncObjectPool.{Counter, log}

private val mainPool = Worker()
private val poolables = new ArrayBuffer[PoolableHolder[T]](configuration.maxObjects)
private var poolables = new Stack[PoolableHolder[T]]()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of line 244: this.poolables = this.poolables.diff(removals). Unfortunately mutable.Stack doesn't support removing elements that are not the top item, as far as I know.

private val checkouts = new ArrayBuffer[T](configuration.maxObjects)
private val waitQueue = new ArrayBuffer[Promise[T]](configuration.maxQueueSize)
private val waitQueue = new Queue[Promise[T]]()
private val timer = new Timer("async-object-pool-timer-" + Counter.incrementAndGet(), true)
timer.scheduleAtFixedRate(new TimerTask {
def run() {
Expand Down Expand Up @@ -150,10 +150,10 @@ class SingleThreadedAsyncObjectPool[T](
*/

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

if (!this.waitQueue.isEmpty) {
this.checkout(this.waitQueue.remove(0))
if (this.waitQueue.nonEmpty) {
this.checkout(this.waitQueue.dequeue())
}

promise.success(this)
Expand Down Expand Up @@ -205,7 +205,7 @@ class SingleThreadedAsyncObjectPool[T](
case e: Exception => promise.failure(e)
}
} else {
val item = this.poolables.remove(0).item
val item = this.poolables.pop().item
this.checkouts += item
promise.success(item)
}
Expand Down Expand Up @@ -241,7 +241,7 @@ class SingleThreadedAsyncObjectPool[T](
}
}
}
this.poolables --= removals
this.poolables = this.poolables.diff(removals)
}

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