콘텐츠로 이동

2025 03 07

2025-03-07

Scala Play에서 Future 처리방식

import scala.concurrent.ExecutionContext.Implicits.global

// controller
def detail(id: Long) = Action.async { implicit request =>
    service.detail(id, request.user).map { _ =>
        Ok("")
    }
} 

// service
def detail(id: Long, user: AdminUser) = Future[Unit] {
    if (user.hasFeature()) throw new Exception()
    val action = for {
        _ <- dao.findById(id)
    } yield ()

    db.execute(action)
}
  • 비동기 실행
    • ExecutionContext 내 idle 쓰레드에서 작업을 할당하여 처리
    • 컨트롤러 실행 메서드, 서비스 실행 메서드, 컨트롤러 콜백 모두 별도의 쓰레드 (같을 수 있음)에서 ExecutionContext에서 받아서 처리
    • 기본적으로 ExecutionContext는 ForkJoinPool
      • Runtime.availableProcessors() 호출해 CPU 코어수 기준 스레드 생성
  • 매개변수 전달/힙 메모리
    • JVM에서 모든 쓰레드가 힙 영역 공유.
    • 매개변수 객체들은 다 heap에 저장되어 참조할 수 있고, 값들은 복사되어 전달
    • 이렇다보니, 불변 객체를 쓰는게 함수형 패러다임에 좋겠죠?
      • 상태를 딱히 유지하기엔 여기저기 쓰레드에서 건들 수 있을테니

DB maxLifeTime

2025-03-06 14:03:46,969 WARN  [undefined] slick.dbs.center-db-master.db-18 [c.z.hikari.pool.PoolBase] 
slick.dbs.center-db-master.db - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@33a90c74 
(No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
  • 커넥션이 이미 종료된 시점에서 유효성 검증 시도
  • App connection pool maxLifeTime > DB connectionTimeout
    • DB
      • SHOW VARIABLES LIKE 'wait_timeout';
      • 'wait_timeout', '28800' (8시간)
  • 슬립 모드로 노트북 접을 때 hikariCP
    • db timezone 설정이 -9시간 전 으로 설정되어 있어서 영향을 받나... (왜 이렇게 했을꼬)