Skip to content

Instantly share code, notes, and snippets.

@michaljemala
Created September 12, 2019 08:03
Show Gist options
  • Save michaljemala/f8c07158b3ca3a92f2f858bba6f761fb to your computer and use it in GitHub Desktop.
Save michaljemala/f8c07158b3ca3a92f2f858bba6f761fb to your computer and use it in GitHub Desktop.
A slow HTTP client
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"time"
)
var data = []byte(`{"commands": [{"name": "system/time"}]}`)
func main() {
sr := slowReader{bytes.NewBuffer(data)}
req, err := http.NewRequest(http.MethodPost, "http://localhost:8000/v1/bulk", sr)
if err != nil {
log.Fatal(err)
}
t0 := time.Now()
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
log.Printf("\n%s in %v", resp.Status, time.Since(t0))
}
type slowReader struct {
r io.Reader
}
func (r slowReader) Read(data []byte) (int, error) {
time.Sleep(250 * time.Millisecond) // Write speed: 4B/s
n, err := r.r.Read(data[:1])
if n > 0 {
fmt.Printf("%s", data[:1])
}
return n, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment