Posts

Showing posts from 2021

zio quick read: Extracting the executor for a service

zio quick read: Extracting the executor for a service I had the need to obtain an Executor from the default zio runtime and use it to create a service. Let’s assume you have a def mkMyService ( executor : java . util . concurrent . Executor ) : MyService = ? ? ? and you want to create the client using the zio default runtime thread pool. For example, to use a specific Executor with HttpClient which is a new async http client in java 11. Otherwise, HttpClient uses a common, shared thread pool which may or may not be what you want. Since the executor is in the ZIO environment and we want to create a layer so that the “client” is in the R (the input environment) for all effects, we can pull the executor out as an effect then add it to a layer. Here’s the code (this is the hard way): type MyEnv = Has [ MyService ] val resources : Layer [ Nothing , MyEnv ] = ZLayer . fromEffect ( ZIO . executor . map ( _ . asJava ) ) >>> ZLayer . req