Skip to content

Commit 7fbe427

Browse files
allow configuration curl multi MAX_HOST_CONNECTIONS
https://curl.se/libcurl/c/CURLMOPT_MAX_HOST_CONNECTIONS.html
1 parent 02ed36d commit 7fbe427

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

ext/curb_multi.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,25 @@ static VALUE ruby_curl_multi_max_connects(VALUE self, VALUE count) {
187187
return count;
188188
}
189189

190+
/*
191+
* call-seq:
192+
* multi = Curl::Multi.new
193+
* multi.max_host_connections = 1
194+
*
195+
* Set the max number of connections per host
196+
*/
197+
static VALUE ruby_curl_multi_max_host_connections(VALUE self, VALUE count) {
198+
#ifdef HAVE_CURLMOPT_MAX_HOST_CONNECTIONS
199+
ruby_curl_multi *rbcm;
200+
201+
Data_Get_Struct(self, ruby_curl_multi, rbcm);
202+
203+
curl_multi_setopt(rbcm->handle, CURLMOPT_MAX_HOST_CONNECTIONS, NUM2LONG(count));
204+
#endif
205+
206+
return count;
207+
}
208+
190209
/*
191210
* call-seq:
192211
* multi = Curl::Multi.new
@@ -704,6 +723,7 @@ void init_curb_multi() {
704723
rb_define_singleton_method(cCurlMulti, "autoclose", ruby_curl_multi_get_autoclose, 0);
705724
/* Instance methods */
706725
rb_define_method(cCurlMulti, "max_connects=", ruby_curl_multi_max_connects, 1);
726+
rb_define_method(cCurlMulti, "max_host_connections=", ruby_curl_multi_max_host_connections, 1);
707727
rb_define_method(cCurlMulti, "pipeline=", ruby_curl_multi_pipeline, 1);
708728
rb_define_method(cCurlMulti, "_add", ruby_curl_multi_add, 1);
709729
rb_define_method(cCurlMulti, "_remove", ruby_curl_multi_remove, 1);

ext/extconf.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ def have_constant(name)
338338
have_constant :CURL_SSLVERSION_SSLv2
339339
have_constant :CURL_SSLVERSION_SSLv3
340340

341+
# Added in 7.30.0
342+
have_constant "curlmopt_max_host_connections"
343+
341344
# Added in 7.34.0
342345
have_constant :CURL_SSLVERSION_TLSv1_0
343346
have_constant :CURL_SSLVERSION_TLSv1_1

tests/tc_curl_multi.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,25 @@ def test_multi_easy_http_with_max_connects
552552
end
553553
end
554554

555+
def test_multi_easy_http_with_max_host_connections
556+
urls = [
557+
{ :url => TestServlet.url + '?q=1', :method => :get },
558+
{ :url => TestServlet.url + '?q=2', :method => :get },
559+
{ :url => TestServlet.url + '?q=3', :method => :get }
560+
]
561+
Curl::Multi.http(urls, {:pipeline => true, :max_host_connections => 1}) do|easy, code, method|
562+
assert_equal 200, code
563+
case method
564+
when :post
565+
assert_match(/POST/, easy.body)
566+
when :get
567+
assert_match(/GET/, easy.body)
568+
when :put
569+
assert_match(/PUT/, easy.body)
570+
end
571+
end
572+
end
573+
555574
def test_multi_recieves_500
556575
m = Curl::Multi.new
557576
e = Curl::Easy.new("http://127.0.0.1:9129/methods")

0 commit comments

Comments
 (0)