0

question about filter : https://twitter.github.io/scala_school/finagle.html#Filter

For typical stack of filters + service layout, how can I add a default behavior for each of the filters? Which basically look at the request/response and do some side effect actions(counting, logging etc). I don't want to write code for each filter, but rather want to have this default behavior triggered on end of each filter.

user776635
  • 861
  • 3
  • 9
  • 13

1 Answers1

1

What you want is filter composition, i.e. you create a filter that's really just a pass-through and causes some side-effects and then you can mix that in with any other filter via composition. e.g.:

val authFilter: Filter[HttpReq, HttpRep, AuthHttpReq, HttpRep]
val loggingFilter[Req, Rep]: Filter[Req, Rep, Req, Rep]

val authWithLogging: Filter[HttpReq, HttpRep, AuthHttpReq, HttpRep] =
  authFilter andThen loggingFilter
Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
  • that can work, but it will be strange, say I have: FilterA andThen FilterB andThen FilterC with this approach I will be doing : FilterA andThen LoggingFilter andThen FilterB andThen LoggingFilter andThen FilterC – user776635 Apr 30 '15 at 06:49
  • Correct. That's why LoggingFilter should be composed in rather than say, be a common filter base class. Ideally you would compose the filters you need for the service you need rather than compose previously composed filters – Arne Claassen Apr 30 '15 at 16:05