Skip to content

Commit

Permalink
Fixed fallback template
Browse files Browse the repository at this point in the history
  • Loading branch information
hexdigest committed Oct 22, 2022
1 parent 624a732 commit b11334c
Show file tree
Hide file tree
Showing 16 changed files with 304 additions and 104 deletions.
89 changes: 57 additions & 32 deletions templates/fallback
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,72 @@ func New{{$decorator}}(interval time.Duration, impls ...{{.Interface.Type}}) {{$
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
type _resultStruct {{$method.ResultsStruct}}
{{if $method.HasResults}}var _res _resultStruct{{end}}
var _ch = make(chan _resultStruct, 0)
{{if $method.ReturnsError}}var _errorsList []string{{end}}
var _ticker = time.NewTicker(_d.interval)
defer _ticker.Stop()

{{- if $method.AcceptsContext}}
ctx, _cancelFunc := context.WithCancel(ctx)
defer _cancelFunc()
_loop:{{end}}
for _i :=0; _i < len(_d.implementations); _i++ {
go func(_impl {{$.Interface.Type}}) {
{{if $method.HasResults}}{{$method.ResultsNames}} := {{end}}_impl.{{$method.Call}}
{{- if $method.ReturnsError}}
if err != nil {
err = fmt.Errorf("%T: %v", _impl, err)
}
{{end}}
select {
case _ch <- _resultStruct{ {{$method.ResultsNames}} }:
default:
}
}(_d.implementations[_i])
{{end}}

go func() {
for _i :=0; _i < len(_d.implementations); _i++ {
go func(_impl {{$.Interface.Type}}) {
{{if $method.HasResults}}{{$method.ResultsNames}} := {{end}}_impl.{{$method.Call}}
{{- if $method.ReturnsError}}
if err != nil {
err = fmt.Errorf("%T: %v", _impl, err)
}
{{end}}

_tickerCh := _ticker.C
if _i == len(_d.implementations) - 1 {
_tickerCh = nil
{{if $method.AcceptsContext}}
select {
case _ch <- _resultStruct{ {{$method.ResultsNames}} }:
case <-ctx.Done():
}
{{else}}
select {
case _ch <- _resultStruct{ {{$method.ResultsNames}} }:
default:
}
{{end}}
}(_d.implementations[_i])

if _i < len(_d.implementations) - 1 {
<-_ticker.C
}
}
}()

select {
case {{if $method.HasResults}}_res = {{end}}<- _ch:
{{if $method.AcceptsContext}}
for {
select {
case {{if $method.HasResults}}_res := {{end}} <- _ch:
{{- if $method.ReturnsError}}
if _res.err == nil {
{{ $method.ReturnStruct "_res" }}
}
_errorsList = append(_errorsList, _res.err.Error())
if len(_errorsList) == len(_d.implementations) {
err = fmt.Errorf(strings.Join(_errorsList, ";"))
return
}
{{else}}
{{ $method.ReturnStruct "_res" }}
{{end -}}
{{if $method.AcceptsContext}}case <-ctx.Done():
{{if $method.ReturnsError}}
_errorsList = append(_errorsList, ctx.Err().Error())
err = fmt.Errorf(strings.Join(_errorsList, ";"))
{{end}}
return
{{end -}}
}
}
{{else}}
{{if $method.HasResults}}_res := {{end}} <- _ch
{{- if $method.ReturnsError}}
if _res.err == nil {
{{ $method.ReturnStruct "_res" }}
Expand All @@ -60,17 +96,6 @@ func New{{$decorator}}(interval time.Duration, impls ...{{.Interface.Type}}) {{$
{{else}}
{{ $method.ReturnStruct "_res" }}
{{end -}}
{{if $method.AcceptsContext}}case <-ctx.Done():
_errorsList = append(_errorsList, ctx.Err().Error())
break _loop
{{end -}}
case <-_tickerCh:
{{- if $method.ReturnsError}}
_errorsList = append(_errorsList, fmt.Sprintf("%T: timeout", _d.implementations[_i]))
{{end}}
}
}
{{if $method.ReturnsError}}err = fmt.Errorf(strings.Join(_errorsList, ";")){{end}}
return
{{end}}
}
{{end}}
1 change: 1 addition & 0 deletions templates_tests/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "context"
// TestInterface is used to test templates
type TestInterface interface {
F(ctx context.Context, a1 string, a2 ...string) (result1, result2 string, err error)
ContextNoError(ctx context.Context, a1 string, a2 string)
NoError(string) string
NoParamsOrResults()
Channels(chA chan bool, chB chan<- bool, chanC <-chan bool)
Expand Down
3 changes: 3 additions & 0 deletions templates_tests/interface_with_circuitbreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (c *consecutiveErrorsImpl) NoError(string) string {
return ""
}

func (c *consecutiveErrorsImpl) ContextNoError(ctx context.Context, a1 string, a2 string) {
}

func (c *consecutiveErrorsImpl) NoParamsOrResults() {
}

Expand Down
188 changes: 116 additions & 72 deletions templates_tests/interface_with_fallback.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b11334c

Please sign in to comment.