-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgothriftpool.go
268 lines (202 loc) · 4.89 KB
/
gothriftpool.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package gothriftpool
import (
"bytes"
"fmt"
"go/format"
"text/template"
"github.com/koofr/gointerfacer"
)
const proxyTemplate = `// Autogenerated by gothriftpool generator
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
package {{.ProxyPackage}}
import (
"context"
"github.com/koofr/go-resourcepool"
"{{.InterfaceImport}}"
)
type ErrCreateResource struct {
Err error
}
func NewErrCreateResource(err error) *ErrCreateResource {
return &ErrCreateResource{Err: err}
}
func (e *ErrCreateResource) Error() string {
return "{{.ClientType}} resource could not be created: " + e.Err.Error()
}
func (e *ErrCreateResource) Unwrap() error {
return e.Err
}
type ClientFactory func(context.Context) (client {{.ClientType}}, close func(), err error)
type resource struct {
client {{.ClientType}}
close func()
isClosed bool
}
func (r *resource) Close() {
r.isClosed = true
r.close()
}
func (r *resource) IsClosed() bool {
return r.isClosed
}
type ClientPool struct {
clientFactory ClientFactory
pool *resourcepool.ResourcePool
retries int
}
func NewClientPool(clientFactory ClientFactory, idleCapacity int, maxResources int) *ClientPool {
p := &ClientPool{
clientFactory: clientFactory,
retries: 2,
}
p.pool = resourcepool.NewResourcePool(p.createResource, idleCapacity, maxResources)
return p
}
func (p *ClientPool) Close() {
p.pool.Close()
}
func (p *ClientPool) SetRetries(retries int) {
p.retries = retries
}
func (p *ClientPool) GetPool() *resourcepool.ResourcePool {
return p.pool
}
func (p *ClientPool) createResource(ctx context.Context) (r resourcepool.Resource, err error) {
client, close, err := p.clientFactory(ctx)
if err != nil {
return nil, err
}
r = &resource{
client: client,
close: close,
isClosed: false,
}
return
}
func (p *ClientPool) GetClient(ctx context.Context) (client {{.ClientType}}, release func(), err error) {
var lastErr error
for i := 0; i < p.retries; i++ {
r, err := p.pool.Acquire(ctx)
if err != nil {
if err == context.Canceled || err == context.DeadlineExceeded {
return nil, nil, err
}
p.pool.Empty()
lastErr = err
continue
}
err = r.(*resource).client.Ping(ctx)
if err != nil {
r.Close()
p.pool.Release(r)
p.pool.Empty()
lastErr = err
continue
}
release = func() {
if ctx.Err() != nil {
r.Close()
}
p.pool.Release(r)
}
return r.(*resource).client, release, nil
}
return nil, nil, NewErrCreateResource(lastErr)
}
type Proxy struct {
clientFactory ClientFactory
}
func New(clientFactory ClientFactory) *Proxy {
return &Proxy{
clientFactory: clientFactory,
}
}
{{range .Functions}}
func (p *Proxy) {{.Name}}({{range .Params}}{{.Name}} {{.Type}}, {{end}})({{range .Res}}{{.Name}} {{.Type}}, {{end}}){
thriftClient, thriftClientClose, err := p.clientFactory(ctx)
if err != nil {
return
}
defer thriftClientClose()
return thriftClient.{{.Name}}({{range .Params}}{{.Name}}, {{end}})
}
{{end}}
`
type Generator struct {
InterfaceImport string
InterfacePackage string
InterfaceName string
ProxyPackage string
Functions []gointerfacer.Func
}
type ThriftInterface struct {
InterfaceImport string
ProxyPackage string
ClientType string
Functions []gointerfacer.Func
}
func NewGenerator(iface string) (g *Generator, err error) {
ifaceImport, ifacePkg, ifaceName, err := gointerfacer.FindInterface(iface)
if err != nil {
return nil, err
}
proxyPackage := ifacePkg + "proxy"
functions, err := gointerfacer.Functions(iface)
if err != nil {
return nil, err
}
var pingFunc *gointerfacer.Func
for _, f := range functions {
if f.Name == "Ping" &&
len(f.Params) == 1 &&
f.Params[0].Name == "ctx" &&
f.Params[0].Type == "context.Context" &&
len(f.Res) == 1 &&
f.Res[0].Type == "error" {
pingFunc = &f
}
}
if pingFunc == nil {
return nil, fmt.Errorf("Interface %s does not contain function Ping() error", iface)
}
g = &Generator{
InterfaceImport: ifaceImport,
InterfacePackage: ifacePkg,
InterfaceName: ifaceName,
ProxyPackage: proxyPackage,
Functions: functions,
}
return g, nil
}
func (g *Generator) render() (source []byte, err error) {
ti := &ThriftInterface{
InterfaceImport: g.InterfaceImport,
ProxyPackage: g.ProxyPackage,
ClientType: g.InterfacePackage + "." + g.InterfaceName,
Functions: g.Functions,
}
tmpl, err := template.New("tpl").Parse(proxyTemplate)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, ti)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (g *Generator) format(source []byte) (code []byte, err error) {
return format.Source(source)
}
func (g *Generator) Generate() (code []byte, err error) {
source, err := g.render()
if err != nil {
return nil, err
}
code, err = g.format(source)
if err != nil {
return nil, err
}
return code, nil
}