Skip to content

Commit 374245f

Browse files
committedNov 5, 2020
fix: add concurrency cli opt + propagate timeout opt
1 parent 528101c commit 374245f

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed
 

‎cmd/autonats/main.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func main() {
2727
baseDir := ctx.String("dir")
2828
timeout := ctx.Int("timeout")
2929
outFile := ctx.String("out")
30+
conc := ctx.Int("concurrency")
3031

3132
if outFile == "" {
3233
outFile = "nats_client.go"
@@ -38,17 +39,27 @@ func main() {
3839
timeout = 5
3940
}
4041

42+
if conc <= 0 {
43+
conc = 5
44+
}
45+
4146
fmt.Printf("parsing '%s' and will export to '%s'\n", baseDir, outFile)
4247

43-
parser := autonats.NewParser()
48+
parser := autonats.NewParser(&autonats.ParserConfig{
49+
BaseDir: baseDir,
50+
DefaultTimeout: timeout,
51+
OutputFileName: outFile,
52+
DefaultConcurrency: conc,
53+
Tracing: ctx.Bool("tracing"),
54+
})
4455

4556
if err := parser.ParseDir(baseDir); err != nil {
4657
return fmt.Errorf("failed to parse the provided directory: %s", err.Error())
4758
}
4859

4960
parser.Run()
5061

51-
return parser.Render(baseDir, outFile, timeout, true)
62+
return parser.Render()
5263
},
5364
Flags: []cli.Flag{
5465
cli.StringFlag{
@@ -71,9 +82,15 @@ func main() {
7182
},
7283
cli.BoolFlag{
7384
Name: "tracing",
74-
Usage: "Enable tracing",
85+
Usage: "Generate tracing code using OpenTracing library",
7586
EnvVar: "AUTONATS_TRACING",
7687
},
88+
cli.IntFlag{
89+
Name: "concurrency, c",
90+
Usage: "Default handler concurrency",
91+
EnvVar: "AUTONATS_CONCURRENCY",
92+
Value: 5,
93+
},
7794
},
7895
},
7996
}

‎method.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"go/ast"
55
)
66

7+
// Describes a service method that's exposed to the service mesh
78
type Method struct {
89
Name string
910
Params []*Param
1011
Results []*Param
1112
imports map[string]bool
12-
HandlerConcurrency int
13+
HandlerConcurrency int // Method handler concurrency
14+
Timeout int // Method timeout
1315
}
1416

1517
func MethodFromField(field *ast.Field) *Method {
@@ -23,7 +25,8 @@ func MethodFromField(field *ast.Field) *Method {
2325
Results: make([]*Param, nResults, nResults),
2426
imports: make(map[string]bool),
2527
Name: field.Names[0].Name,
26-
HandlerConcurrency: 5,
28+
HandlerConcurrency: 0, // TODO: add custom tag/comment to define concurrency for each method
29+
Timeout: 0, // TODO: add custom tag/comment to define timeout for each method
2730
}
2831

2932
for ii, p := range fx.Params.List {

‎parser.go

+24-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import (
99

1010
// Parser config
1111
type ParserConfig struct {
12-
BaseDir string // Directory containing interfaces to scan
13-
DefaultTimeout int // Timeout for NATS requests
14-
OutputFileName string // Output file name
12+
BaseDir string // Directory containing interfaces to scan
13+
DefaultTimeout int // Timeout for NATS requests
14+
OutputFileName string // Output file name
15+
DefaultConcurrency int // Default handler concurrency
16+
Tracing bool // Generate tracing code
1517
}
1618

1719
// Parser object
1820
type Parser struct {
21+
config *ParserConfig
1922
services []*Service
2023
rawPackages map[string]*ast.Package
2124
packages map[string]*Package
@@ -36,11 +39,12 @@ func ParseDir(path string) (map[string]*ast.Package, error) {
3639
}
3740

3841
// Creates a new parser with the provided config
39-
func NewParser() *Parser {
42+
func NewParser(config *ParserConfig) *Parser {
4043
return &Parser{
44+
config: config,
4145
services: make([]*Service, 0),
42-
packages: make(map[string]*Package),
4346
rawPackages: make(map[string]*ast.Package),
47+
packages: make(map[string]*Package),
4448
}
4549
}
4650

@@ -79,14 +83,24 @@ func (par *Parser) Run() {
7983
packages[service.FileName] = pkg
8084
}
8185

86+
for _, m := range service.Methods {
87+
if m.Timeout <= 0 {
88+
m.Timeout = par.config.DefaultTimeout
89+
}
90+
91+
if m.HandlerConcurrency <= 0 {
92+
m.HandlerConcurrency = par.config.DefaultConcurrency
93+
}
94+
}
95+
8296
pkg.AddService(service)
8397
}
8498

8599
par.services = services
86100
par.packages = packages
87101
}
88102

89-
func (par *Parser) Render(baseDir, outfile string, timeout int, tracing bool) error {
103+
func (par *Parser) Render() error {
90104
imports := make([]string, 0)
91105

92106
for pk := range par.packages {
@@ -96,13 +110,13 @@ func (par *Parser) Render(baseDir, outfile string, timeout int, tracing bool) er
96110
}
97111

98112
data := RenderData{
99-
FileName: outfile,
100-
Path: baseDir,
113+
FileName: par.config.OutputFileName,
114+
Path: par.config.BaseDir,
101115
Services: par.services,
102116
Imports: imports,
103-
Timeout: timeout,
117+
Timeout: par.config.DefaultTimeout,
104118
JsonLib: "jsoniter",
105-
Tracing: tracing,
119+
Tracing: par.config.Tracing,
106120
}
107121

108122
return Render(&data)

‎template.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ import (
8686
{{ end -}}
8787
)
8888
89-
const timeout = time.Second * {{ .Timeout }}
90-
9189
{{ range $srv := .Services }}
9290
{{ template "server_interface" $srv }}
9391
@@ -119,7 +117,7 @@ const timeout = time.Second * {{ .Timeout }}
119117
ext.Component.Set(replySpan, "autonats")
120118
121119
defer replySpan.Finish()
122-
innerCtx, _ := context.WithTimeout(ctx, timeout)
120+
innerCtx, _ := context.WithTimeout(ctx, time.Second * {{ $method.Timeout }})
123121
innerCtxT := opentracing.ContextWithSpan(innerCtx, replySpan)
124122
125123
{{ $hasResult := gt (len $method.Results) 1 }}
@@ -269,7 +267,7 @@ const timeout = time.Second * {{ .Timeout }}
269267
}
270268
{{ end }}
271269
272-
reqCtx, cancelFn := context.WithTimeout(reqCtx, timeout)
270+
reqCtx, cancelFn := context.WithTimeout(reqCtx, time.Second * {{ $method.Timeout }})
273271
defer cancelFn()
274272
var replyMsg *nats.Msg
275273
if replyMsg, err = client.NatsConn.RequestWithContext(ctx, "{{ $subject }}", t.Bytes()); err != nil {

0 commit comments

Comments
 (0)