Skip to content

Instantly share code, notes, and snippets.

@joaolsilva
Created August 3, 2017 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaolsilva/bc4d73122049d6fe5bb9e2d79f81579c to your computer and use it in GitHub Desktop.
Save joaolsilva/bc4d73122049d6fe5bb9e2d79f81579c to your computer and use it in GitHub Desktop.
Crash demonstration for go-curl
package main
import (
"github.com/andelf/go-curl"
"log"
"sync"
"time"
)
const maxRequests = 100
var mutex = &sync.Mutex{}
var nActiveRequests int
func GetURL(url string, maxBytes int, timeout int) (result []byte, err error) {
mutex.Lock()
nActiveRequests++
mutex.Unlock()
easy := curl.EasyInit()
defer easy.Cleanup()
easy.Setopt(curl.OPT_URL, url)
easy.Setopt(curl.OPT_CONNECTTIMEOUT, timeout)
easy.Setopt(curl.OPT_TIMEOUT, timeout)
easy.Setopt(curl.OPT_FOLLOWLOCATION, true)
easy.Setopt(curl.OPT_MAXREDIRS, 3)
easy.Setopt(curl.OPT_NOSIGNAL, 1)
easy.Setopt(curl.OPT_VERBOSE, false)
result = []byte{}
// make a callback function
writeFunc := func(buf []byte, userdata interface{}) bool {
result = append(result, buf...)
//log.Printf("DEBUG: size=> %v", len(result))
//log.Printf("DEBUG: content=> %v", string(result))
//log.Printf("DEBUG: userdata=> %v", userdata)
if len(result) >= maxBytes {
return false
}
return true
}
easy.Setopt(curl.OPT_WRITEFUNCTION, writeFunc)
easy.Setopt(curl.OPT_NOPROGRESS, false)
easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool {
//log.Printf("OPT_PROGRESSFUNCTION: dltotal %v dlnow %v ultotal %v ulnow %v", dltotal, dlnow, ultotal, ulnow)
if int(dltotal) >= maxBytes {
return false
}
return true
})
err = easy.Perform()
if err != nil {
if v, ok := err.(curl.CurlError); ok && v == curl.E_ABORTED_BY_CALLBACK {
err = nil
}
}
if err != nil {
log.Printf("GetURL: %v", err)
}
mutex.Lock()
nActiveRequests--
mutex.Unlock()
return result, err
}
func main() {
var n int
for {
mutex.Lock()
n = nActiveRequests
mutex.Unlock()
if n < maxRequests {
go GetURL("https://assets-cdn.github.com/images/modules/site/universe-logo.png", 16, 8)
} else {
time.Sleep(20 * time.Millisecond)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment