-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shutdown): make shutdown async and parallel (shutdown in reverse…
… invocation order)
- Loading branch information
Showing
12 changed files
with
310 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,65 @@ | ||
package do | ||
|
||
import "errors" | ||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var ErrServiceNotFound = errors.New("DI: could not find service") | ||
var ErrCircularDependency = errors.New("DI: circular dependency detected") | ||
var ErrHealthCheckTimeout = errors.New("DI: health check timeout") | ||
|
||
func newShutdownErrors() *ShutdownErrors { | ||
return &ShutdownErrors{} | ||
} | ||
|
||
type ShutdownErrors map[EdgeService]error | ||
|
||
func (e *ShutdownErrors) Add(scopeID string, scopeName string, serviceName string, err error) { | ||
if err != nil { | ||
(*e)[newEdgeService(scopeID, scopeName, serviceName)] = err | ||
} | ||
} | ||
|
||
func (e ShutdownErrors) Len() int { | ||
out := 0 | ||
for _, v := range e { | ||
if v != nil { | ||
out++ | ||
} | ||
} | ||
return out | ||
} | ||
|
||
func (e ShutdownErrors) Error() string { | ||
lines := []string{} | ||
for k, v := range e { | ||
if v != nil { | ||
lines = append(lines, fmt.Sprintf(" - %s > %s: %s", k.ScopeName, k.Service, v.Error())) | ||
} | ||
} | ||
|
||
if len(lines) == 0 { | ||
return "DI: no shutdown errors" | ||
} | ||
|
||
return "DI: shutdown errors:\n" + strings.Join(lines, "\n") | ||
} | ||
|
||
func mergeShutdownErrors(ins ...*ShutdownErrors) *ShutdownErrors { | ||
out := newShutdownErrors() | ||
|
||
for _, in := range ins { | ||
if in != nil { | ||
se := &ShutdownErrors{} | ||
if ok := errors.As(in, &se); ok { | ||
for k, v := range *se { | ||
(*out)[k] = v | ||
} | ||
} | ||
} | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package do | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShutdownErrors_Add(t *testing.T) { | ||
is := assert.New(t) | ||
|
||
se := newShutdownErrors() | ||
is.Equal(0, len(*se)) | ||
is.Equal(0, se.Len()) | ||
|
||
se.Add("scope-1", "scope-a", "service-a", nil) | ||
is.Equal(0, len(*se)) | ||
is.Equal(0, se.Len()) | ||
is.EqualValues(&ShutdownErrors{}, se) | ||
|
||
se.Add("scope-2", "scope-b", "service-b", assert.AnError) | ||
is.Equal(1, len(*se)) | ||
is.Equal(1, se.Len()) | ||
is.EqualValues(&ShutdownErrors{ | ||
{ScopeID: "scope-2", ScopeName: "scope-b", Service: "service-b"}: assert.AnError, | ||
}, se) | ||
} | ||
|
||
func TestShutdownErrors_Error(t *testing.T) { | ||
is := assert.New(t) | ||
|
||
se := newShutdownErrors() | ||
is.Equal(0, len(*se)) | ||
is.Equal(0, se.Len()) | ||
is.EqualValues("DI: no shutdown errors", se.Error()) | ||
|
||
se.Add("scope-1", "scope-a", "service-a", nil) | ||
is.Equal(0, len(*se)) | ||
is.Equal(0, se.Len()) | ||
is.EqualValues("DI: no shutdown errors", se.Error()) | ||
|
||
se.Add("scope-2", "scope-b", "service-b", assert.AnError) | ||
is.Equal(1, len(*se)) | ||
is.Equal(1, se.Len()) | ||
is.EqualValues("DI: shutdown errors:\n - scope-b > service-b: assert.AnError general error for testing", se.Error()) | ||
} | ||
|
||
func TestMergeShutdownErrors(t *testing.T) { | ||
is := assert.New(t) | ||
|
||
se1 := newShutdownErrors() | ||
se2 := newShutdownErrors() | ||
se3 := newShutdownErrors() | ||
|
||
se1.Add("scope-1", "scope-a", "service-a", assert.AnError) | ||
se2.Add("scope-2", "scope-b", "service-b", assert.AnError) | ||
|
||
result := mergeShutdownErrors(se1, se2, se3, nil) | ||
is.Equal(2, result.Len()) | ||
is.EqualValues( | ||
&ShutdownErrors{ | ||
{ScopeID: "scope-1", ScopeName: "scope-a", Service: "service-a"}: assert.AnError, | ||
{ScopeID: "scope-2", ScopeName: "scope-b", Service: "service-b"}: assert.AnError, | ||
}, | ||
result, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.