-
Notifications
You must be signed in to change notification settings - Fork 100
Service Filters
BJ Neilsen edited this page Sep 13, 2013
·
3 revisions
Service Filters provides ActionController-style filter support to service
instances, specifically adding before_filter
, after_filter
, and around_filter
.
require 'lib/foo/user.pb'
class Foo::UserService
before_filter :start_request_timer
after_filter :end_request_timer
around_filter :benchmark_request
# Provide a list of rpc methods to call (or exclude calling) for the given filter(s).
# The following two filters are essentially equivalent.
before_filter :verify_user_present, :only => [ :update, :delete ]
before_filter :verify_user_present, :except => [ :find, :create ]
# Using if/unless filters options to achieve the same goal, reporting a login after the login has been processed.
# Note that you can provide a method name or lambda, but you must return a boolean value.
after_filter :report_login, :only => :login, :if => :user_found?
after_filter :report_login, :only => :login, :if => lambda { |service| service.response.user_guid.present? }
after_filter :report_login, :only => :login, :unless => :user_missing?
after_filter :report_login, :only => :login, :unless => lambda { |service| service.response.user_guid.empty? }
#... rpc instance methods
private
def start_request_timer
@time_start = Time.now
end
def end_request_timer
@time_end = Time.now
log_info { ... }
end
def benchmark_request
Benchmark.benchmark do
yield
end
end
end