lib/shatter/service/discovery.rb in shatter-rb-0.0.2 vs lib/shatter/service/discovery.rb in shatter-rb-0.1.0
- old
+ new
@@ -1,49 +1,62 @@
+# frozen_string_literal: true
+
+require "zk"
module Shatter
module Service
class Discovery
class << self
+ def connection_pool
+ @connection_pool ||= ZK::Pool::Bounded.new(Shatter::Config.zookeeper_host)
+ end
def deregister_service(service_url)
- zk = ZK.new (Shatter::Config.zookeeper_host)
- if zk.exists?("/shater_service_instances/#{service_url}")
- zk.delete("/shater_service_instances/#{service_url}")
- end
- zk.close
+ zk = connection_pool.checkout
+ key = Shatter::Util.instances_key
+ zk.delete("#{key}/#{service_url}") if zk.exists?("#{key}/#{service_url}")
+ connection_pool.checkin(zk)
end
def register_service(service_url)
Shatter.logger.info "Registering #{service_url} to zookeeper"
- zk = ZK.new (Shatter::Config.zookeeper_host)
- unless zk.exists?("/shater_service_instances/#{service_url}")
- created = zk.create("/shater_service_instances/#{service_url}")
+ zk = connection_pool.checkout
+ key = Shatter::Util.instances_key
+ unless zk.exists?("#{key}/#{service_url}")
+ created = zk.create("#{key}/#{service_url}")
Shatter.logger.info "Registered #{created}"
end
- puts zk.children("/shater_service_instances")
- zk.close
+ connection_pool.checkin(zk)
end
def service_instance_url
service_instance_urls.sample
end
def service_instance_urls
- zk = ZK.new (Shatter::Config.zookeeper_host)
- urls = zk.children("/shater_service_instances")
- zk.close
+ zk = connection_pool.checkout
+ urls = zk.children(Shatter::Util.instances_key)
+ connection_pool.checkin(zk)
urls
end
+ def populate_result_location(uuid)
+ zk = connection_pool.checkout
+ zk.create(Util.zookeeper_response_key(uuid), my_host)
+ connection_pool.checkin(zk)
+ end
+
+ def my_host
+ my_ip = ENV["HOST_NAME"] || "localhost"
+ "#{my_ip}:#{Shatter::Config.service_port}"
+ end
+
def service_instance_url_for_uuid(uuid)
- druby_instance_url = nil
key = Util.zookeeper_response_key(uuid)
- zk = ZK.new(Shatter::Config.zookeeper_host)
+ zk = connection_pool.checkout
druby_instance_url = zk.get(key)[0] if zk.exists?(key)
- zk.close
- Shatter.logger.debug "Service instance url for #{uuid} - #{druby_instance_url}"
-
+ connection_pool.checkin(zk)
druby_instance_url
end
end
end
end
-end
\ No newline at end of file
+end