mirror of
https://github.com/traefik/traefik
synced 2026-02-03 08:50:32 +00:00
27 lines
622 B
Go
27 lines
622 B
Go
package recursion
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
type stackType int
|
|
|
|
const (
|
|
stackKey stackType = iota
|
|
)
|
|
|
|
func CheckRecursion(ctx context.Context, itemType, itemName string) (context.Context, error) {
|
|
currentStack, ok := ctx.Value(stackKey).([]string)
|
|
if !ok {
|
|
currentStack = []string{}
|
|
}
|
|
name := itemType + ":" + itemName
|
|
if slices.Contains(currentStack, name) {
|
|
return ctx, fmt.Errorf("could not instantiate %s %s: recursion detected in %s", itemType, itemName, strings.Join(append(currentStack, name), "->"))
|
|
}
|
|
return context.WithValue(ctx, stackKey, append(currentStack, name)), nil
|
|
}
|