lib/synapse/service_watcher/zookeeper.rb
def discover
      log.info "synapse: discovering backends for service #{@name}"

      new_backends = []
      @zk.children(@discovery['path'], :watch => true).each do |id|
        node = @zk.get("#{@discovery['path']}/#{id}")

        begin
          host, port, name, weight = deserialize_service_instance(node.first)
        rescue StandardError => e
          log.error "synapse: invalid data in ZK node #{id} at #{@discovery['path']}: #{e}"
        else
          server_port = @server_port_override ? @server_port_override : port

          # find the numberic id in the node name; used for leader elections if enabled
          numeric_id = id.split('_').last
          numeric_id = NUMBERS_RE =~ numeric_id ? numeric_id.to_i : nil

          log.debug "synapse: discovered backend #{name} at #{host}:#{server_port} for service #{@name}"
          new_backends << { 'name' => name, 'host' => host, 'port' => server_port, 'id' => numeric_id, 'weight' => weight }
        end
      end

      set_backends(new_backends)
    end
# find the current backends at the discovery path
find the current backends at the discovery path
find the current backends at the discovery path
def discover
      log.info "synapse: discovering backends for service #{@name}"
      new_backends = []
      @zk.children(@discovery['path'], :watch => true).each do |id|
        node = @zk.get("#{@discovery['path']}/#{id}")
        begin
          host, port, name, weight = deserialize_service_instance(node.first)
        rescue StandardError => e
          log.error "synapse: invalid data in ZK node #{id} at #{@discovery['path']}: #{e}"
        else
          server_port = @server_port_override ? @server_port_override : port
          numeric_id = id.split('_').last
          numeric_id = NUMBERS_RE =~ numeric_id ? numeric_id.to_i : nil
          log.debug "synapse: discovered backend #{name} at #{host}:#{server_port} for service #{@name}"
          new_backends << { 'name' => name, 'host' => host, 'port' => server_port, 'id' => numeric_id, 'weight' => weight }
        end
      end
      set_backends(new_backends)
    end
def discover log . info "synapse: discovering backends for service #{@name}" new_backends = [ ] @zk . children ( @discovery [ 'path' ] , :watch => true ) . each do | id | node = @zk . get ( "#{@discovery['path']}/#{id}" ) begin host , port , name , weight = deserialize_service_instance ( node . first ) rescue StandardError => e log . error "synapse: invalid data in ZK node #{id} at #{@discovery['path']}: #{e}" else server_port = @server_port_override ? @server_port_override : port numeric_id = id . split ( '_' ) . last numeric_id = NUMBERS_RE =~ numeric_id ? numeric_id . to_i : nil log . debug "synapse: discovered backend #{name} at #{host}:#{server_port} for service #{@name}" new_backends << { 'name' => name , 'host' => host , 'port' => server_port , 'id' => numeric_id , 'weight' => weight } end end set_backends ( new_backends ) end
find the current backends at the discovery path
find the current backends at the discovery path
# find the numberic id in the node name; used for leader elections if enabled