Circuits: prevent cyclical parent dependency (#24882)
This commit is contained in:
parent
d000a3863d
commit
d818bf1467
2 changed files with 16 additions and 0 deletions
|
|
@ -148,6 +148,12 @@ func (c *Circuit) GetParent() api.Circuit {
|
|||
|
||||
// setParent set parent circuit
|
||||
func (c *Circuit) setParent(parent api.Circuit) error {
|
||||
// prevent cyclical dependency
|
||||
for p := parent.GetParent(); p != nil; p = p.GetParent() {
|
||||
if c == p {
|
||||
return fmt.Errorf("cycle detected: %s and %s cannot be mutual parents", c.GetTitle(), parent.GetTitle())
|
||||
}
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.parent != nil {
|
||||
|
|
|
|||
|
|
@ -139,3 +139,13 @@ func TestCircuitCurrents(t *testing.T) {
|
|||
ctrl.Finish()
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapCycleDetection(t *testing.T) {
|
||||
log := util.NewLogger("foo")
|
||||
|
||||
pc, _ := New(log, "root", 0, 0, nil, 0)
|
||||
lpc, _ := New(log, "lpc", 0, 0, nil, 0)
|
||||
|
||||
require.NoError(t, lpc.setParent(pc))
|
||||
require.Error(t, pc.Wrap(lpc))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue