问题
My application has an API using SprayCan. In the application, any blocking code has a separate dispatcher for each specific resource.
Is it necessary to protect the API service from being blocked by the application by configuring it with it's own Dispatcher too?
Also is it common practice to use a Router for an API service to handle a larger capacity of requests?
class MyService extends Actor with HttpService {...}
val service = system.actorOf(MyService.props(...).withDispatcher(???))
回答1:
Is it necessary to protect the API service from being blocked by the application by configuring it with it's own Dispatcher too?
Usually not necessary. Check reference.conf
that comes as default config with Spray to see if that dispatcher will satisfy your needs. If not, provide a custom one.
Also is it common practice to use a Router for an API service to handle a larger capacity of requests?
Usually requests are handed off to another Actor to unblock the route or ran as Future
(possibly on a separate thread pool). See all available options here: How does spray.routing.HttpService dispatch requests?.
Finally, you should not block in the route handler because it will block your service. From your description it sounds like your blocking code runs in a Future
or similar. As long as it does not make route handler wait for result/block it's fine.
来源:https://stackoverflow.com/questions/34298233/use-a-dispatcher-with-spray-httpservice