Skip to content

Commit 4df86fd

Browse files
committed
curl_multi_wait implementation
(based on andelf#69 in @andelf repo) Signed-off-by: Vadim A. Misbakh-Soloviov <[email protected]>
1 parent d333122 commit 4df86fd

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

examples/multi_and_wait.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/msva/go-curl"
6+
"time"
7+
)
8+
9+
func main() {
10+
ch1 := curl.EasyInit()
11+
ch2 := curl.EasyInit()
12+
13+
ch1.Setopt(curl.OPT_URL, "http://www.google.com")
14+
ch1.Setopt(curl.OPT_HEADER, 1)
15+
ch1.Setopt(curl.OPT_VERBOSE, true)
16+
ch2.Setopt(curl.OPT_URL, "http://www.microsoft.com")
17+
ch2.Setopt(curl.OPT_HEADER, 1)
18+
ch2.Setopt(curl.OPT_VERBOSE, true)
19+
20+
mh := curl.MultiInit()
21+
22+
mh.AddHandle(ch1)
23+
mh.AddHandle(ch2)
24+
25+
var repeats int
26+
27+
for {
28+
stillRunning, err := mh.Perform()
29+
if err != nil {
30+
fmt.Printf("Error perform: %s\n", err)
31+
break
32+
} else if stillRunning > 0 {
33+
fmt.Printf("Still running: %d\n", stillRunning)
34+
} else {
35+
break
36+
}
37+
38+
timeoutMs := 1000
39+
curlTimeout, err := mh.Timeout()
40+
if err != nil {
41+
fmt.Printf("Error multi_timeout: %s\n", err)
42+
}
43+
if curlTimeout >= 0 {
44+
timeoutMs = curlTimeout
45+
}
46+
47+
numFds, err := mh.Wait(timeoutMs)
48+
if err != nil {
49+
fmt.Printf("Error wait: %s\n", err)
50+
break
51+
}
52+
53+
// 'numFds' being zero means either a timeout or no file descriptors to
54+
// wait for. Try timeout on first occurrence, then assume no file
55+
// descriptors, which means wait for 100 milliseconds to prevent spinning
56+
// in Perform + Wait.
57+
if numFds == 0 {
58+
repeats++ // count number of repeated zero numFds
59+
if repeats > 1 {
60+
time.Sleep(100 * time.Millisecond)
61+
}
62+
} else {
63+
repeats = 0
64+
}
65+
}
66+
}

multi.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func (mcurl *CURLM) Perform() (int, error) {
9696
return int(running_handles), err
9797
}
9898

99+
// curl_multi_wait - polls on all easy handles in a multi handle
100+
func (mcurl *CURLM) Wait(timeoutMs int) (int, error) {
101+
p := mcurl.handle
102+
handles_with_events := C.int(0)
103+
err := newCurlMultiError(C.curl_multi_wait(p, nil, 0, C.int(timeoutMs), &handles_with_events))
104+
return int(handles_with_events), err
105+
}
106+
99107
// curl_multi_add_handle - add an easy handle to a multi session
100108
func (mcurl *CURLM) AddHandle(easy *CURL) error {
101109
mp := mcurl.handle

0 commit comments

Comments
 (0)