Skip to content

Instantly share code, notes, and snippets.

@wlhee wlhee/client.go
Created Nov 23, 2019

Embed
What would you like to do?
slow client
package main
import (
"fmt"
"net/http"
"os"
"time"
"google3/base/go/flag"
)
var (
url = flag.String("url", "", "URL")
transportBuffer = flag.Int("transport_buffer", 10, "transport buffer size in bytes used by HTTP client")
readBuffer = flag.Int("read_buffer", 1, "read buffer size in bytes used for reading bytes from the trasport buffer")
readInterval = flag.Duration("read_interval", time.Second, "interval to read from the transport buffer into the read buffer")
)
func main() {
flag.Parse()
fmt.Println("=======================\n")
fmt.Printf("URL: %s\ntransport_buffer: %d\nread_buffer:%d\nread_interval: %v\n", *url, *transportBuffer, *readBuffer, *readInterval)
transport := &http.Transport{
ReadBufferSize: *transportBuffer,
}
fmt.Println("=======================\n\n")
client := &http.Client{Transport: transport}
response, err := client.Get(*url)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
defer response.Body.Close()
for k, v := range response.Header {
fmt.Printf("%s:%s\n", k, v)
}
buffer := make([]byte, *readBuffer)
for {
n, err := response.Body.Read(buffer)
if err != nil {
fmt.Printf("Read error: %v\n", err)
os.Exit(1)
}
fmt.Printf("Read %d bytes: %s\n", n, string(buffer))
time.Sleep(*readInterval)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.