-
Notifications
You must be signed in to change notification settings - Fork 348
/
ExecutionContexts.scala
31 lines (25 loc) · 1.1 KB
/
ExecutionContexts.scala
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
// Copyright (c) 2013-2018 Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.util
import cats.effect.{ Resource, Sync }
import java.util.concurrent.{ Executors, ExecutorService }
import scala.concurrent.ExecutionContext
object ExecutionContexts {
/** Resource yielding an `ExecutionContext` backed by a fixed-size pool. */
def fixedThreadPool[F[_]](size: Int)(
implicit sf: Sync[F]
): Resource[F, ExecutionContext] = {
val alloc = sf.delay(Executors.newFixedThreadPool(size))
val free = (es: ExecutorService) => sf.delay(es.shutdown())
Resource.make(alloc)(free).map(ExecutionContext.fromExecutor)
}
/** Resource yielding an `ExecutionContext` backed by an unbounded thread pool. */
def cachedThreadPool[F[_]](
implicit sf: Sync[F]
): Resource[F, ExecutionContext] = {
val alloc = sf.delay(Executors.newCachedThreadPool)
val free = (es: ExecutorService) => sf.delay(es.shutdown())
Resource.make(alloc)(free).map(ExecutionContext.fromExecutor)
}
}