1

Given I have a pool of objects, how can I safely return an an pooled object back to the pool after usage if the usage is asynchronous? (using Future and Promise in this case)

Here's an example:

      pool
        .take
        .flatMap {
        connection =>
          connection
            .sendQuery("SELECT 0")
            .map {
            query =>
              pool.giveBack(connection)
              query.rows.get(0, 0)
          }
      }

The problem here is that if the sendQuery call fails, the object will never be returned to the pool. Is there some kind of pipeline sequence for futures what would allow me to safely return this object to the pool even if the code itself fails to do so or should I just ignore this?

The pool implementation is this one and the pooled object is this one.

My main objective here is to make the pool usage be as little error prone as possible and as it stands currently it's clearly not that, since the programmer could forget to return the object and the pool would quickly exhaust itself.

4

1 回答 1

1

您正在寻找 Future 的“andThen”方法。

于 2013-04-24T20:40:31.883 回答