Permalink
Browse files

Reuse http connections.

  • Loading branch information...
1 parent 1b48f2d commit b61907ecea46f4b1c20424abadf6a090a925d799 @paulcarey committed May 6, 2010
Showing with 29 additions and 5 deletions.
  1. +20 −5 lib/relaxdb/net_http_server.rb
  2. +5 −0 lib/relaxdb/server.rb
  3. +4 −0 lib/relaxdb/taf2_curb_server.rb
@@ -6,7 +6,18 @@ def initialize(host, port)
@host = host
@port = port
end
-
+
+ def cx
+ unless @cx && @cx.active?
+ @cx = Net::HTTP.start(@host, @port)
+ end
+ @cx
+ end
+
+ def close_connection
+ @cx.finish if @cx
+ end
+
def delete(uri)
request(Net::HTTP::Delete.new(uri))
end
@@ -30,14 +41,18 @@ def post(uri, json)
end
def request(req)
- res = Net::HTTP.start(@host, @port) {|http|
- http.request(req)
- }
+ begin
+ res = cx.request(req)
+ rescue
+ @cx = nil
+ res = cx.request(req)
+ end
+
if (not res.kind_of?(Net::HTTPSuccess))
handle_error(req, res)
end
res
- end
+ end
def to_s
"http://#{@host}:#{@port}/"
View
@@ -29,6 +29,11 @@ def db_exists?(name)
# URL encode slashes e.g. RelaxDB.delete_db "foo%2Fbar"
def delete_db(name)
+ # Close the http connection as CouchDB will keep a file handle to the db open
+ # if the http connection remains open - this will result in CouchDB throwing
+ # emfile errors after a significant number of databases are deleted.
+ @server.close_connection
+
@logger.info("Deleting database #{name}")
@server.delete("/#{name}")
end
@@ -13,6 +13,10 @@ def initialize body
def initialize(host, port)
@host, @port = host, port
end
+
+ def close_connection
+ # Impl only in net_http as connections are reused
+ end
def delete(uri)
request(uri, 'delete'){ |c| c.http_delete}

0 comments on commit b61907e

Please sign in to comment.