Navigation Menu

Skip to content

Commit

Permalink
Add ability to specify default timeout seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 20, 2015
1 parent b9f1aa4 commit 8aac6da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/droonga/client.rb
Expand Up @@ -30,6 +30,7 @@ class Client
DEFAULT_PORT = 10031
DEFAULT_TAG = "droonga"
DEFAULT_DATASET = "Default"
DEFAULT_TIMEOUT_SECONDS = 3

attr_writer :on_error

Expand Down Expand Up @@ -89,7 +90,7 @@ def initialize(options={})
@completion = options[:completion] != false
@validation = options[:validation] != false

@completer = MessageCompleter.new
@completer = MessageCompleter.new(:default_timeout => options[:default_timeout])
@validator = MessageValidator.new
end

Expand Down
10 changes: 10 additions & 0 deletions lib/droonga/client/message_completer.rb
Expand Up @@ -23,11 +23,15 @@ class MessageCompleter
def initialize(options={})
@options = options
@fixed_date = @options[:fixed_date]
@default_timeout = @options[:default_timeout]
end

def complete(message)
id = message["id"] || generate_id
date = message["date"] || @fixed_date || new_date
if not have_timeout?(message) and @default_timeout
message["timeout"] = @default_timeout
end
message.merge("id" => id, "date" => date)
end

Expand All @@ -41,6 +45,12 @@ def generate_id
def new_date
Time.now.utc.iso8601(MICRO_SECONDS_DECIMAL_PLACE)
end

def have_timeout?(message)
return true if message["timeout"]
return false unless message["body"].is_a?(Hash)
not message["body"]["timeout"].nil?
end
end
end
end
4 changes: 4 additions & 0 deletions lib/droonga/command/base.rb
Expand Up @@ -50,6 +50,9 @@ def parse_options(&block)
option.on("receiver-host=",
"Host name of this host.",
:default => Client::DEFAULT_HOST)
option.on("timeout=",
"Time to terminate unresponsive connections (in seconds).",
:default => Client::DEFAULT_TIMEOUT_SECONDS)
end
@options = options
rescue Slop::MissingOptionError => error
Expand All @@ -65,6 +68,7 @@ def client
:protocol => :droonga,
:receiver_host => @options["receiver-host"],
:receiver_port => 0,
:default_timeout => @options[:timeout],
}
@client ||= Droonga::Client.new(options)
end
Expand Down

0 comments on commit 8aac6da

Please sign in to comment.