Files
tasks/common/priorities/priority_util.go
David ReissandGitHub 46c6b1b2f4 Validate Priority metadata (#8159)
## What changed?
Add validation for Priority metadata.

## Why?
Negative keys/weights are ignored but we should reject requests with
too-long keys.

## How did you test it?
- [x] added new unit test(s)
2025-08-07 09:00:58 -07:00

49 lines
1.1 KiB
Go

package priorities
import (
"cmp"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/serviceerror"
)
const (
fairnessKeyMaxLength = 64
)
var (
ErrInvalidPriority = serviceerror.NewInvalidArgument("PriorityKey can't be negative")
ErrFairnessKeyLength = serviceerror.NewInvalidArgument("FairnessKey length exceeds limit")
ErrInvalidFairnessWeight = serviceerror.NewInvalidArgument("FairnessWeight can't be negative")
)
func Merge(
base *commonpb.Priority,
override *commonpb.Priority,
) *commonpb.Priority {
if base == nil {
return override
} else if override == nil {
return base
}
return &commonpb.Priority{
PriorityKey: cmp.Or(override.PriorityKey, base.PriorityKey),
FairnessKey: cmp.Or(override.FairnessKey, base.FairnessKey),
FairnessWeight: cmp.Or(override.FairnessWeight, base.FairnessWeight),
}
}
func Validate(p *commonpb.Priority) error {
if p == nil {
return nil
} else if p.PriorityKey < 0 {
return ErrInvalidPriority
} else if len(p.FairnessKey) > fairnessKeyMaxLength {
return ErrFairnessKeyLength
} else if p.FairnessWeight < 0 {
return ErrInvalidFairnessWeight
}
return nil
}