This repository has been archived by the owner on Jan 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.go
76 lines (64 loc) · 1.58 KB
/
examples.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
package srvproxy
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/peterbourgon/srvproxy/pool"
"github.com/peterbourgon/srvproxy/proxy"
"github.com/peterbourgon/srvproxy/retry"
)
// ExampleDefaultUsage shows how to wire up a default DNS SRV proxy into
// http.DefaultClient.
func ExampleDefaultUsage() {
var rt http.RoundTripper
rt = http.DefaultTransport
rt = proxy.Proxy(proxy.Next(rt))
rt = retry.Retry(retry.Next(rt))
t := &http.Transport{}
t.RegisterProtocol("dnssrv", rt)
http.DefaultClient.Transport = t
resp, err := http.Get("dnssrv://foo.bar.srv.internal.name/normal/path?key=value")
if err != nil {
log.Fatal(err)
}
io.Copy(os.Stdout, resp.Body)
resp.Body.Close()
}
// ExampleCustomUsage shows how to wire up a customized DNS SRV proxy into
// http.DefaultClient.
func ExampleCustomUsage() {
var rt http.RoundTripper
rt = http.DefaultTransport
rt = proxy.Proxy(
proxy.Next(rt),
proxy.Scheme("https"),
proxy.PoolReporter(os.Stderr),
proxy.Factory(pool.RoundRobin),
)
rt = retry.Retry(
retry.Next(rt),
retry.Pass(func(resp *http.Response, err error) error {
if err != nil {
return err
}
if resp.StatusCode >= 500 {
return fmt.Errorf("HTTP %d %s", resp.StatusCode, resp.Status)
}
return nil
}),
retry.MaxAttempts(10),
retry.Timeout(750*time.Millisecond),
)
t := &http.Transport{}
t.RegisterProtocol("dnssrv", rt)
http.DefaultClient.Transport = t
resp, err := http.Get("dnssrv://foo.bar.srv.internal.name/normal/path?key=value")
if err != nil {
log.Fatal(err)
}
io.Copy(os.Stdout, resp.Body)
resp.Body.Close()
}