mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
memoize the relatively expensive remote IP code
This commit is contained in:
parent
00a0a4ddeb
commit
cda1a5d5fe
1 changed files with 7 additions and 1 deletions
|
@ -42,7 +42,7 @@ module ActionDispatch
|
|||
# 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 @@ module ActionDispatch
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue