Navigation Menu

Skip to content

Commit

Permalink
Dispose clients automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 29, 2015
1 parent 2b86bdf commit e068c7e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bin/droonga-groonga
Expand Up @@ -48,7 +48,7 @@ module Droonga
"body" => build_params(ARGV),
}

response = client.request(groonga_message)
response = request(groonga_message)
raise NoResponse.new unless response
body = response["body"]

Expand Down
4 changes: 2 additions & 2 deletions bin/droonga-system-status
Expand Up @@ -29,8 +29,8 @@ module Droonga
:default => false)
end

response = client.request("dataset" => @options[:dataset],
"type" => "system.status")
response = request("dataset" => @options[:dataset],
"type" => "system.status")
raise NoResponse.new unless response
body = response["body"]

Expand Down
21 changes: 19 additions & 2 deletions lib/droonga/command/base.rb
Expand Up @@ -63,7 +63,21 @@ def parse_options(&block)
raise MissingRequiredParameter.new
end

def client
def request(message)
resposne = nil
open do |client|
response = client.request(message)
end
response
end

def send(message)
open do |client|
client.send(message)
end
end

def open(&block)
options = {
:host => @options[:host],
:port => @options[:port],
Expand All @@ -72,8 +86,11 @@ def client
:receiver_host => @options["receiver-host"],
:receiver_port => 0,
:default_timeout => @options[:timeout],
:default_target_role => @options[:target_role],
}
@client ||= Droonga::Client.new(options)
Droonga::Client.open(options) do |client|
yield(client)
end
end
end
end
Expand Down

0 comments on commit e068c7e

Please sign in to comment.