Class: Curl::Multi

Inherits:
Object
  • Object
show all
Defined in:
lib/rhack/curl/multi.rb,
ext/curb/curb_multi.c,
ext/curb-original/curb_multi.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.Curl::Multi.default_timeout( = 4) ⇒ 4

Get the global default time out for all Curl::Multi Handles.

Returns:

  • (4)


132
133
134
# File 'ext/curb/curb_multi.c', line 132

VALUE ruby_curl_multi_get_default_timeout(VALUE klass) {
  return INT2FIX(cCurlMutiDefaulttimeout);
}

.Curl::Multi.default_timeout( = 4) ⇒ 4

Set the global default time out for all Curl::Multi Handles. This value is used when libcurl cannot determine a timeout value when calling curl_multi_timeout.

Returns:

  • (4)


120
121
122
123
# File 'ext/curb/curb_multi.c', line 120

VALUE ruby_curl_multi_set_default_timeout(VALUE klass, VALUE timeout) {
  cCurlMutiDefaulttimeout = FIX2LONG(timeout);
  return timeout;
}

.Curl::Multi.new#<Curl::Easy...>

Create a new Curl::Multi instance

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/curb/curb_multi.c', line 92

VALUE ruby_curl_multi_new(VALUE klass) {
  VALUE new_curlm;

  ruby_curl_multi *rbcm = ALLOC(ruby_curl_multi);

  rbcm->handle = curl_multi_init();
  if (!rbcm->handle) {
    rb_raise(mCurlErrFailedInit, "Failed to initialize multi handle");
  }

  rbcm->requests = rb_hash_new();

  rbcm->active = 0;
  rbcm->running = 0;

  new_curlm = Data_Wrap_Struct(klass, curl_multi_mark, curl_multi_free, rbcm);

  return new_curlm;
}

Instance Method Details

#add(easy) ⇒ Object

multi = Curl::Multi.new easy = Curl::Easy.new(‘url’)

multi.add(easy)

Add an easy handle to the multi stack



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'ext/curb/curb_multi.c', line 237

VALUE ruby_curl_multi_add(VALUE self, VALUE easy) {
  CURLMcode mcode;
  ruby_curl_easy *rbce;
  ruby_curl_multi *rbcm;

  Data_Get_Struct(self, ruby_curl_multi, rbcm);
  Data_Get_Struct(easy, ruby_curl_easy, rbce);

  /* setup the easy handle */
  ruby_curl_easy_setup( rbce );

  mcode = curl_multi_add_handle(rbcm->handle, rbce->curl);
  if (mcode != CURLM_CALL_MULTI_PERFORM && mcode != CURLM_OK) {
    raise_curl_multi_error_exception(mcode);
  }

  rbcm->active++;

  /* Increase the running count, so that the perform loop keeps running.
   * If this number is not correct, the next call to curl_multi_perform will correct it. */
  rbcm->running++;

  rb_hash_aset( rbcm->requests, easy, easy );

  return self;
}

#cancel!Object

Cancels all requests currently being made on this Curl::Multi handle.



326
327
328
329
330
331
332
333
334
335
# File 'ext/curb/curb_multi.c', line 326

static VALUE ruby_curl_multi_cancel(VALUE self) {
  ruby_curl_multi *rbcm;

  Data_Get_Struct(self, ruby_curl_multi, rbcm);
  
  rb_hash_foreach( rbcm->requests, ruby_curl_multi_cancel_callback, (VALUE)rbcm );
  
  /* for chaining */
  return self;
}

#clear!Object

Used for Curl.reset_carier!



20
21
22
# File 'lib/rhack/curl/multi.rb', line 20

def clear!
  reqs.each {|k| remove k rescue()}
end

#dropObject

Emergency debug methods, not used inside a framework



25
26
27
28
# File 'lib/rhack/curl/multi.rb', line 25

def drop
  while running > 0 do perform rescue() end
  Curl.recall
end

#drop!Object



30
31
32
33
# File 'lib/rhack/curl/multi.rb', line 30

def drop!
  drop
  Curl.reset_carier! if reqs.size + running > 0
end

#idle?Boolean

Returns whether or not this Curl::Multi handle is processing any requests. E.g. this returns true when multi.requests.length == 0.

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
185
186
187
# File 'ext/curb/curb_multi.c', line 177

static VALUE ruby_curl_multi_idle(VALUE self) {
  ruby_curl_multi *rbcm;
  
  Data_Get_Struct(self, ruby_curl_multi, rbcm);
  
  if ( FIX2INT( rb_funcall(rbcm->requests, rb_intern("length"), 0) ) == 0 ) {
    return Qtrue;
  } else {
    return Qfalse;
  }
}

#inspectObject



13
14
15
16
# File 'lib/rhack/curl/multi.rb', line 13

def inspect
  rsize = reqs.size
  "<#Carier #{rsize} #{rsize == 1 ? 'unit' : 'units'}, #{running} executing>"
end

#max_connects=(count) ⇒ Object

multi = Curl::Multi.new multi.max_connects = 800

Set the max connections in the cache for a multi handle



196
197
198
199
200
201
202
203
204
205
# File 'ext/curb/curb_multi.c', line 196

static VALUE ruby_curl_multi_max_connects(VALUE self, VALUE count) {
#ifdef HAVE_CURLMOPT_MAXCONNECTS
  ruby_curl_multi *rbcm;

  Data_Get_Struct(self, ruby_curl_multi, rbcm);
  curl_multi_setopt(rbcm->handle, CURLMOPT_MAXCONNECTS, NUM2INT(count));
#endif

  return count;
}

#perform(*args) ⇒ Object

multi = Curl::Multi.new easy1 = Curl::Easy.new(‘url’) easy2 = Curl::Easy.new(‘url’)

multi.add(easy1) multi.add(easy2)

multi.perform do

# while idle other code my execute here

end

Run multi handles, looping selecting when data can be transfered



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'ext/curb/curb_multi.c', line 654

static VALUE ruby_curl_multi_perform(int argc, VALUE *argv, VALUE self) {
  ruby_curl_multi *rbcm;
  VALUE idle;
  
  Data_Get_Struct(self, ruby_curl_multi, rbcm);
  
  if (!(rbcm->active || rbcm->running)) {
    rb_scan_args(argc, argv, "01", &idle);
    if (idle == Qtrue) {
      
      if (rb_funcall(mCurl, rb_intern("joined"), 0) == Qtrue) {
        ruby_log("Nothing to perform; recalling...");
        return Qfalse;
      }
      //ruby_log("Nothing to perform; idling...");
      rb_curl_multi_idle_perform(self, rbcm);
      
    }
    else {
      rb_raise(rb_eRuntimeError, "Nothing to perform");
    }
    return Qtrue;
  }
  
  rb_curl_multi_perform(self, rbcm);
  return Qtrue;
}

#pipeline=(onoff) ⇒ Object

multi = Curl::Multi.new multi.pipeline = true

Pass a long set to 1 to enable or 0 to disable. Enabling pipelining on a multi handle will make it attempt to perform HTTP Pipelining as far as possible for transfers using this handle. This means that if you add a second request that can use an already existing connection, the second request will be “piped” on the same connection rather than being executed in parallel. (Added in 7.16.0)



218
219
220
221
222
223
224
225
226
# File 'ext/curb/curb_multi.c', line 218

static VALUE ruby_curl_multi_pipeline(VALUE self, VALUE onoff) {
#ifdef HAVE_CURLMOPT_PIPELINING
  ruby_curl_multi *rbcm;

  Data_Get_Struct(self, ruby_curl_multi, rbcm);
  curl_multi_setopt(rbcm->handle, CURLMOPT_PIPELINING, onoff == Qtrue ? 1 : 0);
#endif
  return onoff;
}

#remove(easy) ⇒ Object

multi = Curl::Multi.new easy = Curl::Easy.new(‘url’)

multi.add(easy)

# sometime later multi.remove(easy)

Remove an easy handle from a multi stack.

Will raise an exception if the easy handle is not found



278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/curb/curb_multi.c', line 278

VALUE ruby_curl_multi_remove(VALUE self, VALUE easy) {
  ruby_curl_multi *rbcm;
  ruby_curl_easy *rbce;

  Data_Get_Struct(self, ruby_curl_multi, rbcm);

  Data_Get_Struct(easy, ruby_curl_easy, rbce);

  rb_curl_multi_remove(rbcm,easy);

  return self;
}

#requestsArray Also known as: reqs

Returns an array containing all the active requests on this Curl::Multi object.

Returns:



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'ext/curb/curb_multi.c', line 149

static VALUE ruby_curl_multi_requests(VALUE self) {
  ruby_curl_multi *rbcm;
  VALUE result_array;

  Data_Get_Struct(self, ruby_curl_multi, rbcm);
  
  result_array = rb_ary_new();
  
  /* iterate over the requests hash, and stuff references into the array. */
  rb_hash_foreach(rbcm->requests, ruby_curl_multi_requests_callback, result_array);
  
  return result_array;
}

#runningObject



162
163
164
165
166
167
168
# File 'ext/curb/curb_multi.c', line 162

static VALUE ruby_curl_multi_running(VALUE self) {
  ruby_curl_multi *rbcm;

  Data_Get_Struct(self, ruby_curl_multi, rbcm);

  return INT2FIX(rbcm->running);
}

#sheduledObject



9
10
11
# File 'lib/rhack/curl/multi.rb', line 9

def sheduled
  0 < running and running <= reqs.size
end