Your Service as a Functor
Marius Eriksen’s Your Server as a Function proposes a useful model for the development and reasoning of distributed systems with a service-oriented architecture. Most of the discussion regarding this paper seems focused on futures, but I’ve been thinking a lot about services and filters. The following discussion is not dependent on an understanding of futures.
A service takes a request and returns a response:
type Service :: Request -> ResponseServices can be parameterized by other services:1
authorizedWwwService :: Service -> Service -> Service
authorizedWwwService authService wwwService request
| authService `authorizes` request = wwwService request
| otherwise = unauthorizedMessage authServiceFilters, as defined in the paper, take a request and a service, and return a response:
type Filter'' :: (Request, Service) -> ResponseHowever, if we curry the arguments, and swap the order of the Request and the Service, we get the following:
type Filter' :: Service -> Request -> ResponseNoting that the return type of Filter' is Request -> Response, which is just Service. We can thus simplify:
type Filter :: Service -> ServiceFilter is just a higher-order service!2
We can parameterize filters, too:
timeoutFilter :: TimeDuration -> Filter
cachingFilter :: Service -> Filter
loggingFilter :: Service -> Filter
statusFilter :: Service -> Filter
authenticationFilter :: Service -> FilterIf we add a Filter2, this would allow for things like load balancers:
type Filter2 :: Service -> Service -> Service
loadbalancerFilter2 :: LoadBalancerSettings -> Filter2Decomposing redundant work in this manner allows a distributed system to be developed declaratively and safely.
kms