Class: Proxy::Ipam::IpCache
- Inherits:
-
Object
- Object
- Proxy::Ipam::IpCache
show all
- Includes:
- IpamHelper, Log, Singleton
- Defined in:
- lib/smart_proxy_ipam/ip_cache.rb
Overview
Class for managing temp in-memory cache to prevent same IP’s being suggested in race conditions
Constant Summary
collapse
- DEFAULT_CLEANUP_INTERVAL =
60
Constants included
from IpamHelper
Proxy::Ipam::IpamHelper::ERRORS, Proxy::Ipam::IpamHelper::MAX_IP_RETRIES
Instance Method Summary
collapse
Methods included from IpamHelper
#cache_next_ip, #find_new_ip, #get_request_group, #increment_ip, #provider, #usable_ip
Constructor Details
Returns a new instance of IpCache.
18
19
20
21
22
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 18
def initialize
@m = Monitor.new
init_cache
start_cleanup_task
end
|
Instance Method Details
#add(ip, mac, cidr, group_name) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 57
def add(ip, mac, cidr, group_name)
logger.debug("Adding IP '#{ip}' to cache for subnet '#{cidr}' in group '#{group_name}' for IPAM provider #{@provider.to_s}")
@m.synchronize do
mac_addr = mac.nil? || mac.empty? ? SecureRandom.uuid : mac
group_hash = @ip_cache[group_name.to_sym]
group_hash.each do |key, values|
if values.keys.include? mac_addr.to_sym
@ip_cache[group_name.to_sym][key].delete(mac_addr.to_sym)
end
@ip_cache[group_name.to_sym].delete(key) if @ip_cache[group_name.to_sym][key].nil? || @ip_cache[group_name.to_sym][key].empty?
end
if group_hash.key?(cidr.to_sym)
@ip_cache[group_name.to_sym][cidr.to_sym][mac_addr.to_sym] = {ip: ip.to_s, timestamp: Time.now.to_s}
else
@ip_cache = @ip_cache.merge({group_name.to_sym => {cidr.to_sym => {mac_addr.to_sym => {ip: ip.to_s, timestamp: Time.now.to_s}}}})
end
end
end
|
#get_cidr(group, cidr) ⇒ Object
40
41
42
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 40
def get_cidr(group, cidr)
@ip_cache[group.to_sym][cidr.to_sym]
end
|
#get_cleanup_interval ⇒ Object
#get_group(group) ⇒ Object
36
37
38
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 36
def get_group(group)
@ip_cache[group.to_sym]
end
|
#get_ip(group_name, cidr, mac) ⇒ Object
44
45
46
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 44
def get_ip(group_name, cidr, mac)
@ip_cache[group_name.to_sym][cidr.to_sym][mac.to_sym][:ip]
end
|
#get_provider ⇒ Object
28
29
30
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 28
def get_provider
@provider
end
|
#ip_exists(ip, cidr, group_name) ⇒ Object
52
53
54
55
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 52
def ip_exists(ip, cidr, group_name)
cidr_key = @ip_cache[group_name.to_sym][cidr.to_sym]&.to_s
cidr_key.include?(ip.to_s)
end
|
#set_group(group, value) ⇒ Object
32
33
34
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 32
def set_group(group, value)
@ip_cache[group.to_sym] = value
end
|
#set_provider_name(provider) ⇒ Object
24
25
26
|
# File 'lib/smart_proxy_ipam/ip_cache.rb', line 24
def set_provider_name(provider)
@provider = provider
end
|