Last active
April 12, 2017 18:49
-
-
Save flimzy/c7c0b3885cb3e4d4dede3e21d373976d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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