Skip to content

Instantly share code, notes, and snippets.

@flimzy
Last active April 12, 2017 18:49
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 flimzy/c7c0b3885cb3e4d4dede3e21d373976d to your computer and use it in GitHub Desktop.
Save flimzy/c7c0b3885cb3e4d4dede3e21d373976d to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"net/http"
"strings"
"sync"
"sync/atomic"
)
func main() {
var count int32
wg := &sync.WaitGroup{}
for {
wg.Add(5)
go doRequest(wg, atomic.AddInt32(&count, 1))
go doRequest(wg, atomic.AddInt32(&count, 1))
go doRequest(wg, atomic.AddInt32(&count, 1))
go doRequest(wg, atomic.AddInt32(&count, 1))
go doRequest(wg, atomic.AddInt32(&count, 1))
wg.Wait()
}
}
func doRequest(wg *sync.WaitGroup, count int32) {
defer wg.Done()
path := fmt.Sprintf("http://localhost:9000/foo/%d.txt", count)
fmt.Printf("PUT %s\n", path)
req, _ := http.NewRequest("PUT", path, strings.NewReader("test content"))
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Consuming the body is important; without this, the error never occurs.
buf := &bytes.Buffer{}
buf.ReadFrom(resp.Body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment