/
codec.go
126 lines (107 loc) · 2.66 KB
/
codec.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
package httpcodec;
import(
"fmt"
"net/http"
"net/rpc"
)
type ActiveCall struct {
codec *MethodCodec
seq uint64
request *http.Request
reply *http.Response
err error
}
type HTTPCodec struct {
Service string
Open bool
config Config
client *http.Client
methods map[string]*MethodCodec
requestQueue chan *ActiveCall
replyQueue chan *ActiveCall
bodyQueue chan *ActiveCall
}
type Config struct {
OAuth OAuthConfig
}
func NewHTTPCodec(Service string, client *http.Client, config Config) *HTTPCodec {
return &HTTPCodec{Service: Service, Open: true, client: client, methods: make(map[string]*MethodCodec)}
}
func (c *HTTPCodec) Register(method string, codec *MethodCodec) {
if c.IsStarted() {
panic("Register called after HTTPCodec started!")
}
c.methods[method] = codec
}
func (c HTTPCodec) IsStarted() bool {
return c.requestQueue != nil
}
func (c *HTTPCodec) Close() error {
close(c.requestQueue)
close(c.replyQueue)
close(c.bodyQueue)
c.bodyQueue = nil
c.replyQueue = nil
c.requestQueue = nil
return nil
}
func (c *HTTPCodec) Start() {
if c.IsStarted() {
panic("Started HTTPCodec twice!")
}
c.requestQueue = make(chan *ActiveCall, 1)
c.replyQueue = make(chan *ActiveCall, 1)
c.bodyQueue = make(chan *ActiveCall, 1)
go c.requestSender()
}
func (c *HTTPCodec) requestSender() {
for req, ok := <- c.requestQueue; ok; req = <- c.requestQueue {
fmt.Printf("URL: %s\n", req.request.URL.String())
req.reply, req.err = c.client.Do(req.request)
c.replyQueue <- req
}
}
func (c *HTTPCodec) RunRequest(codec *MethodCodec, req *rpc.Request, v interface{}) (err error) {
var httpReq *http.Request
if httpReq, err = codec.CreateRequest(req, v); err != nil {
return
}
c.requestQueue <- &ActiveCall{codec: codec, request: httpReq}
return
}
type ErrNoSuchMethod struct {
Service string
Method string
}
func (e ErrNoSuchMethod) Error() string {
return fmt.Sprintf("No such RPC method: %s.%s", e.Service, e.Method)
}
func (c *HTTPCodec) WriteRequest(req *rpc.Request, v interface{}) (err error) {
if codec, ok := c.methods[req.ServiceMethod]; !ok {
err = ErrNoSuchMethod{Service: c.Service, Method: req.ServiceMethod}
} else {
err = c.RunRequest(codec, req, v)
}
return
}
func (c *HTTPCodec) ReadResponseHeader(resp *rpc.Response) (err error) {
req := <-c.replyQueue
resp.Seq = req.seq
if req.err != nil {
resp.Error = req.err.Error()
} else {
if e2 := req.codec.ReadStatus(req.reply, resp); e2 != nil {
resp.Error = e2.Error()
}
}
c.bodyQueue <- req
return
}
func (c *HTTPCodec) ReadResponseBody(v interface{}) (err error) {
req := <-c.bodyQueue
if v != nil {
err = req.codec.ReadReturnValue(req.reply, v)
}
req.reply.Body.Close()
return
}