Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reuse http connections.

  • Loading branch information...
commit b61907ecea46f4b1c20424abadf6a090a925d799 1 parent 1b48f2d
@paulcarey authored
View
25 lib/relaxdb/net_http_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
5 lib/relaxdb/server.rb
@@ -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
View
4 lib/relaxdb/taf2_curb_server.rb
@@ -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}
Please sign in to comment.
Something went wrong with that request. Please try again.