scala/playframework

CustomExecutionContext 설정

wefree 2023. 3. 1. 22:13

Custom ExecutionContext 생성

import akka.actor.ActorSystem
import play.api.libs.concurrent.CustomExecutionContext

class PostExecutionContext @Inject()(actorSystem: ActorSystem)
    extends CustomExecutionContext(actorSystem, "repository.dispatcher")

 

application.conf 에 ExecutionContext 설정

repository.dispatcher {
  executor = "thread-pool-executor"
  throughput = 1
  thread-pool-executor {
    fixed-pool-size = 40
  }
}

## 참고: https://www.playframework.com/documentation/2.8.x/ThreadPools#Highly-synchronous
akka {
  actor {
    default-dispatcher {
      executor = "thread-pool-executor"
      throughput = 1
      thread-pool-executor {
        fixed-pool-size = 55 # db conn pool (50) + number of cores (4) + housekeeping (1)
      }
    }
  }
}

 

ExecutionContext 사용

import javax.inject.{Inject, Singleton}

import akka.actor.ActorSystem
import play.api.libs.concurrent.CustomExecutionContext
import play.api.{Logger, MarkerContext}

import scala.concurrent.Future


@Singleton
class PostRepositoryImpl @Inject()()(implicit ec: PostExecutionContext) {
  def create(data: PostData)(implicit mc: MarkerContext): Future[PostId] = {
    Future {
      logger.trace(s"create: data = $data")
      data.id
    }
  }
}

 

참고: https://www.playframework.com/documentation/2.8.x/ThreadPools