|
| 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 | +} |
0 commit comments