/
connection.go
106 lines (87 loc) · 2.43 KB
/
connection.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
package cap
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"io"
"net/http"
"time"
"github.com/myfantasy/mft"
)
// Connection - to host
type Connection struct {
Server string `json:"server"`
IgnoreSSLValidation bool `json:"ignore_ssql_validation"`
QueryWait time.Duration `json:"query_wait"`
MaxConnsPerHost int `json:"max_conn"`
MaxIdleConnDuration time.Duration `json:"max_idle_duration"`
client *http.Client `json:"-"`
}
func CreateConnection(server string,
ignoreSSLValidation bool,
queryWait time.Duration,
maxConnsPerHost int,
maxIdleConnDuration time.Duration) (c *Connection) {
c = &Connection{
Server: server,
IgnoreSSLValidation: ignoreSSLValidation,
QueryWait: queryWait,
MaxConnsPerHost: maxConnsPerHost,
MaxIdleConnDuration: maxIdleConnDuration,
}
return c
}
// Init connection
func (c *Connection) Init() {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: c.IgnoreSSLValidation,
},
MaxConnsPerHost: c.MaxConnsPerHost,
MaxIdleConns: c.MaxConnsPerHost,
IdleConnTimeout: c.MaxIdleConnDuration,
DisableCompression: true,
}
c.client = &http.Client{Transport: tr}
}
func ConnectionFromJson(body []byte) (c *Connection, err *mft.Error) {
c = &Connection{}
er0 := json.Unmarshal(body, c)
if er0 != nil {
return nil, GenerateErrorE(10190000, er0)
}
return c, nil
}
// DoRawQuery do some query
func (c *Connection) DoRawQuery(queryWait time.Duration, path string, headersIn map[string]string, query []byte) (body []byte, headersOut map[string]string, statusCode int, err error) {
if queryWait == 0 {
queryWait = c.QueryWait
}
ctx, cancel := context.WithTimeout(context.Background(), queryWait)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Server+path, bytes.NewBuffer(query))
if err != nil {
return body, nil, 0, GenerateErrorE(10190001, err, c.Server)
}
for k, v := range headersIn {
req.Header.Add(k, v)
}
resp, err := c.client.Do(req)
if err != nil {
return body, nil, 0, GenerateErrorE(10190002, err, c.Server)
}
headersOut = map[string]string{}
for k, v := range resp.Header {
if len(v) == 0 {
headersOut[k] = ""
} else {
headersOut[k] = v[0]
}
}
body, err = io.ReadAll(resp.Body)
if err != nil {
return body, nil, 0, GenerateErrorE(10190003, err, c.Server)
}
return body, headersOut, resp.StatusCode, nil
}