Skip to content

Instantly share code, notes, and snippets.

@sumskyi
Created March 27, 2013 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumskyi/5255941 to your computer and use it in GitHub Desktop.
Save sumskyi/5255941 to your computer and use it in GitHub Desktop.
want proper index
def pick(provider, current_proxy=nil)
_accessible = {"unaccessible" => nil}
_not_banned = []
_not_banned << {"banned.#{provider}" => nil }
_not_banned << {"banned.#{provider}" => { '$lt' => BANNED_UNTIL[BANNED_SECONDS] }}
_different_subnet = {}
if current_proxy
_different_subnet = {
:subnet_int => {'$ne' => current_proxy[:subnet_int]}
}
end
_search_conditions = {
"$and" => [
_accessible,
_different_subnet,
{'$or' => _not_banned }
]
}
_sort_conditions = {"used_at.#{provider}" => 1}
proxy = find_one(_search_conditions, :sort => _sort_conditions)
raise NoProxyLeft, "sorry, all proxies are banned by #{provider}" unless proxy
proxy[:used_at] ||= {}
proxy[:used_at][provider] = Time.now
proxy.update
proxy
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment