-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
89 lines (66 loc) · 1.6 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"crypto/rand"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"time"
"golang.org/x/net/http2"
)
func connectionUsingRandomClient(address string, numberOfBytes int, batchSize int, timeBetweenBatches int) error {
fmt.Fprintf(os.Stdout, "%d bytes will be send to %s \n", numberOfBytes, address)
transport := &http2.Transport{
// So http2.Transport doesn't complain the URL scheme isn't 'https'
AllowHTTP: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if strings.HasPrefix(address, "http:") {
transport.DialTLSContext = func(ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, network, addr)
}
}
client := http.Client{
Transport: transport,
}
r, w := io.Pipe()
go func() {
defer w.Close()
for i := 0; i < numberOfBytes || numberOfBytes < 0; {
size := batchSize
if size+i > numberOfBytes && numberOfBytes > -1 {
size = numberOfBytes - i
}
out := make([]byte, size)
rand.Read(out)
i += size
fmt.Println("Sending data")
_, _ = w.Write(out)
time.Sleep(time.Duration(timeBetweenBatches) * time.Millisecond)
}
}()
resp, err := client.Post(address, "", r)
if err != nil {
return err
}
bIn := make([]byte, 1024)
received := 0
n, err := resp.Body.Read(bIn)
for ; err == nil; n, err = resp.Body.Read(bIn) {
if n == 0 {
continue
}
fmt.Println("Receiving data")
received += n
}
fmt.Fprintf(os.Stdout, "%d bytes received from %s \n", received, address)
if err != io.EOF {
return err
}
return nil
}