-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
154 lines (128 loc) · 3.1 KB
/
types.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Copyright (C) 2018 - 2020 assay.it
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/assay-it/sdk-go
//
package http
import (
"crypto/tls"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"time"
"github.com/assay-it/sdk-go/assay"
)
/*
Arrow is a morphism applied to HTTP
*/
type Arrow func(*assay.IOCat) *assay.IOCat
/*
Join composes HTTP arrows to high-order function
(a ⟼ b, b ⟼ c, c ⟼ d) ⤇ a ⟼ d
*/
func Join(arrows ...Arrow) assay.Arrow {
return func(cat *assay.IOCat) *assay.IOCat {
if cat.Fail != nil {
return cat
}
for _, f := range arrows {
if cat = f(cat); cat.Fail != nil {
return cat
}
}
if cat.HTTP != nil && cat.HTTP.Recv != nil && cat.HTTP.Recv.Response != nil {
// Note: due to Golang HTTP pool implementation we need to consume and
// discard body. Otherwise, HTTP connection is not returned to
// to the pool.
io.Copy(ioutil.Discard, cat.HTTP.Recv.Response.Body)
cat.Fail = cat.HTTP.Recv.Body.Close()
cat.HTTP.Recv.Response = nil
}
return cat
}
}
/*
Stack configures custom HTTP stack for the category.
*/
func Stack(client *http.Client) assay.Config {
pool := pool{client}
return assay.SideEffect(pool.Unsafe)
}
/*
Default configures default HTTP stack for the category.
*/
func Default() assay.Config {
pool := pool{defaultClient()}
return assay.SideEffect(pool.Unsafe)
}
func defaultClient() *http.Client {
return &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
ReadBufferSize: 128 * 1024,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
}
// DefaultIO creates default HTTP IO category
// use for development only
func DefaultIO(opts ...assay.Config) *assay.IOCat {
args := append(opts, Default())
return assay.IO(args...)
}
//
//
type pool struct{ *http.Client }
func (p pool) Unsafe(cat *assay.IOCat) *assay.IOCat {
if cat.Fail != nil {
return cat
}
var eg *http.Request
eg, cat.Fail = http.NewRequest(
cat.HTTP.Send.Method,
cat.HTTP.Send.URL.String(),
cat.HTTP.Send.Payload,
)
if cat.Fail != nil {
return cat
}
for head, value := range cat.HTTP.Send.Header {
eg.Header.Set(head, *value)
}
var in *http.Response
in, cat.Fail = p.Client.Do(eg)
if cat.Fail != nil {
return cat
}
cat.HTTP.Recv = &assay.DnStreamHTTP{Response: in}
logSend(cat.LogLevel, eg)
logRecv(cat.LogLevel, in)
return cat
}
func logSend(level int, eg *http.Request) {
if level >= assay.LogLevelEgress {
if msg, err := httputil.DumpRequest(eg, level == assay.LogLevelDebug); err == nil {
log.Printf(">>>>\n%s\n", msg)
}
}
}
func logRecv(level int, in *http.Response) {
if level >= assay.LogLevelIngress {
if msg, err := httputil.DumpResponse(in, level == assay.LogLevelDebug); err == nil {
log.Printf("<<<<\n%s\n", msg)
}
}
}