Permalink
Browse files

memoize the relatively expensive remote IP code

  • Loading branch information...
1 parent 00a0a4d commit cda1a5d5fe002ca92aca01586e8a60439605f960 @indirect indirect committed Nov 14, 2011
Showing with 7 additions and 1 deletion.
  1. +7 −1 actionpack/lib/action_dispatch/middleware/remote_ip.rb
@@ -42,7 +42,7 @@ def initialize(env, middleware)
# HTTP_X_FORWARDED_FOR may be a comma-delimited list in the case of
# multiple chained proxies. The last address which is not a known proxy
# will be the originating IP.
- def to_s
+ def calculate_ip
client_ip = @env['HTTP_CLIENT_IP']
forwarded_ips = ips_from('HTTP_X_FORWARDED_FOR')
remote_addrs = ips_from('REMOTE_ADDR')
@@ -58,6 +58,12 @@ def to_s
client_ip || forwarded_ips.last || remote_addrs.first
end
+ def to_s
+ return @ip if @calculated_ip
+ @calculated_ip = true
+ @ip = calculate_ip
+ end
+
protected
def ips_from(header)

0 comments on commit cda1a5d

Please sign in to comment.