Skip to content

Commit

Permalink
fix: add concurrency cli opt + propagate timeout opt
Browse files Browse the repository at this point in the history
  • Loading branch information
ihadeed committed Nov 5, 2020
1 parent 528101c commit 374245f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
23 changes: 20 additions & 3 deletions cmd/autonats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
baseDir := ctx.String("dir")
timeout := ctx.Int("timeout")
outFile := ctx.String("out")
conc := ctx.Int("concurrency")

if outFile == "" {
outFile = "nats_client.go"
Expand All @@ -38,17 +39,27 @@ func main() {
timeout = 5
}

if conc <= 0 {
conc = 5
}

fmt.Printf("parsing '%s' and will export to '%s'\n", baseDir, outFile)

parser := autonats.NewParser()
parser := autonats.NewParser(&autonats.ParserConfig{
BaseDir: baseDir,
DefaultTimeout: timeout,
OutputFileName: outFile,
DefaultConcurrency: conc,
Tracing: ctx.Bool("tracing"),
})

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

parser.Run()

return parser.Render(baseDir, outFile, timeout, true)
return parser.Render()
},
Flags: []cli.Flag{
cli.StringFlag{
Expand All @@ -71,9 +82,15 @@ func main() {
},
cli.BoolFlag{
Name: "tracing",
Usage: "Enable tracing",
Usage: "Generate tracing code using OpenTracing library",
EnvVar: "AUTONATS_TRACING",
},
cli.IntFlag{
Name: "concurrency, c",
Usage: "Default handler concurrency",
EnvVar: "AUTONATS_CONCURRENCY",
Value: 5,
},
},
},
}
Expand Down
7 changes: 5 additions & 2 deletions method.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"go/ast"
)

// Describes a service method that's exposed to the service mesh
type Method struct {
Name string
Params []*Param
Results []*Param
imports map[string]bool
HandlerConcurrency int
HandlerConcurrency int // Method handler concurrency
Timeout int // Method timeout
}

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

for ii, p := range fx.Params.List {
Expand Down
34 changes: 24 additions & 10 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (

// Parser config
type ParserConfig struct {
BaseDir string // Directory containing interfaces to scan
DefaultTimeout int // Timeout for NATS requests
OutputFileName string // Output file name
BaseDir string // Directory containing interfaces to scan
DefaultTimeout int // Timeout for NATS requests
OutputFileName string // Output file name
DefaultConcurrency int // Default handler concurrency
Tracing bool // Generate tracing code
}

// Parser object
type Parser struct {
config *ParserConfig
services []*Service
rawPackages map[string]*ast.Package
packages map[string]*Package
Expand All @@ -36,11 +39,12 @@ func ParseDir(path string) (map[string]*ast.Package, error) {
}

// Creates a new parser with the provided config
func NewParser() *Parser {
func NewParser(config *ParserConfig) *Parser {
return &Parser{
config: config,
services: make([]*Service, 0),
packages: make(map[string]*Package),
rawPackages: make(map[string]*ast.Package),
packages: make(map[string]*Package),
}
}

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

for _, m := range service.Methods {
if m.Timeout <= 0 {
m.Timeout = par.config.DefaultTimeout
}

if m.HandlerConcurrency <= 0 {
m.HandlerConcurrency = par.config.DefaultConcurrency
}
}

pkg.AddService(service)
}

par.services = services
par.packages = packages
}

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

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

data := RenderData{
FileName: outfile,
Path: baseDir,
FileName: par.config.OutputFileName,
Path: par.config.BaseDir,
Services: par.services,
Imports: imports,
Timeout: timeout,
Timeout: par.config.DefaultTimeout,
JsonLib: "jsoniter",
Tracing: tracing,
Tracing: par.config.Tracing,
}

return Render(&data)
Expand Down
6 changes: 2 additions & 4 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ import (
{{ end -}}
)
const timeout = time.Second * {{ .Timeout }}
{{ range $srv := .Services }}
{{ template "server_interface" $srv }}
Expand Down Expand Up @@ -119,7 +117,7 @@ const timeout = time.Second * {{ .Timeout }}
ext.Component.Set(replySpan, "autonats")
defer replySpan.Finish()
innerCtx, _ := context.WithTimeout(ctx, timeout)
innerCtx, _ := context.WithTimeout(ctx, time.Second * {{ $method.Timeout }})
innerCtxT := opentracing.ContextWithSpan(innerCtx, replySpan)
{{ $hasResult := gt (len $method.Results) 1 }}
Expand Down Expand Up @@ -269,7 +267,7 @@ const timeout = time.Second * {{ .Timeout }}
}
{{ end }}
reqCtx, cancelFn := context.WithTimeout(reqCtx, timeout)
reqCtx, cancelFn := context.WithTimeout(reqCtx, time.Second * {{ $method.Timeout }})
defer cancelFn()
var replyMsg *nats.Msg
if replyMsg, err = client.NatsConn.RequestWithContext(ctx, "{{ $subject }}", t.Bytes()); err != nil {
Expand Down

0 comments on commit 374245f

Please sign in to comment.