Posts

Showing posts from March, 2021

zio quick read: Extracting the executor for a service

zio quick read: Extracting the executor for a service Let’s say you have the need to obtain an Executor from the default zio runtime and use it to create a service. Let’s assume you have a simple function to make your service. This function has no zio knowledge. def mkService ( executor : java . util . concurrent . Executor ) : MyService = ? ? ? Let’s say that you want to use the zio default runtime executor. For example, your service needs to use a specific Executor to power HttpClient –the new async http client in java 11. Otherwise, HttpClient uses a common, shared thread pool which may or may not be what you want. The executor is in the ZIO environment and we want to create a layer so that MyService is in the environment R , the environment, for all effects in the program. We need to pull the executor out the environment and add the derived service as a layer. Here’s the code (this is the hard way): type MyEnv = Has [ MyService ] val resources