This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp_conn.go
115 lines (98 loc) · 2.34 KB
/
http_conn.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
package edgec
import (
"net/http"
"time"
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/log"
"github.com/ronaksoft/rony/pools"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
/*
Creation Time: 2021 - Jan - 05
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type httpConn struct {
h *Http
replicaSet uint64
id string
hostPorts []string
secure bool
}
func (c *httpConn) send(req, res *rony.MessageEnvelope, timeout time.Duration) (replicaSet uint64, err error) {
replicaSet = c.replicaSet
mo := proto.MarshalOptions{UseCachedSize: true}
buf := pools.Buffer.GetCap(mo.Size(req))
defer pools.Buffer.Put(buf)
b, err := mo.MarshalAppend(*buf.Bytes(), req)
if err != nil {
return
}
httpReq := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(httpReq)
httpReq.Header.SetMethod(http.MethodPost)
if c.secure {
httpReq.URI().SetScheme("https")
}
httpReq.SetHost(c.hostPorts[0])
httpReq.SetBody(b)
if hf := c.h.cfg.HeaderFunc; hf != nil {
for k, v := range hf() {
httpReq.Header.Set(k, v)
}
}
httpRes := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(httpRes)
SendLoop:
err = c.h.c.DoTimeout(httpReq, httpRes, timeout)
switch err {
case fasthttp.ErrNoFreeConns:
goto SendLoop
}
if err != nil {
return
}
err = res.Unmarshal(httpRes.Body())
if err != nil {
return
}
switch res.GetConstructor() {
case rony.C_Redirect:
x := &rony.Redirect{}
err = proto.Unmarshal(res.Message, x)
if err != nil {
return
}
replicaSet, err = c.redirect(x)
return
}
return
}
func (c *httpConn) redirect(x *rony.Redirect) (replicaSet uint64, err error) {
if ce := c.h.logger.Check(log.InfoLevel, "Redirect"); ce != nil {
ce.Write(
zap.Any("Edges", x.Edges),
zap.Any("Wait", x.WaitInSec),
)
}
replicaSet = x.Edges[0].ReplicaSet
for _, n := range x.Edges {
c.h.addConn(n.ServerID, n.ReplicaSet, c.h.newConn(n.ServerID, n.ReplicaSet, n.HostPorts...))
}
switch x.Reason {
case rony.RedirectReason_ReplicaSetSession:
c.h.sessionReplica = replicaSet
err = ErrReplicaSetSession
case rony.RedirectReason_ReplicaSetRequest:
replicaSet = x.Edges[0].ReplicaSet
err = ErrReplicaSetRequest
default:
err = ErrUnknownResponse
}
return
}