Skip to content

Commit 04390cf

Browse files
committed
use a sync.Map
1 parent e9d5c4f commit 04390cf

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

dbos/workflow.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,12 @@ func setStepParamDefaults(params *StepParams, stepName string) *StepParams {
719719
BackoffFactor: 2.0,
720720
BaseInterval: 100 * time.Millisecond, // Default base interval
721721
MaxInterval: 5 * time.Second, // Default max interval
722-
StepName: getTypeErasedStepName(stepName),
722+
StepName: func() string {
723+
if value, ok := typeErasedStepNameToStepName.Load(stepName); ok {
724+
return value.(string)
725+
}
726+
return "" // This should never happen
727+
}(),
723728
}
724729
}
725730

@@ -733,24 +738,17 @@ func setStepParamDefaults(params *StepParams, stepName string) *StepParams {
733738
if params.MaxInterval == 0 {
734739
params.MaxInterval = 5 * time.Second // Default max interval
735740
}
736-
if params.StepName == "" {
741+
if len(params.StepName) == 0 {
737742
// If the step name is not provided, use the function name
738-
params.StepName = getTypeErasedStepName(stepName)
743+
if value, ok := typeErasedStepNameToStepName.Load(stepName); ok {
744+
params.StepName = value.(string)
745+
}
739746
}
740747

741748
return params
742749
}
743750

744-
var (
745-
typeErasedStepNameToStepName = make(map[string]string)
746-
typeErasedStepNameMutex sync.RWMutex
747-
)
748-
749-
func getTypeErasedStepName(stepName string) string {
750-
typeErasedStepNameMutex.RLock()
751-
defer typeErasedStepNameMutex.RUnlock()
752-
return typeErasedStepNameToStepName[stepName]
753-
}
751+
var typeErasedStepNameToStepName sync.Map
754752

755753
func RunAsStep[R any](ctx DBOSContext, fn GenericStepFunc[R]) (R, error) {
756754
if ctx == nil {
@@ -766,9 +764,7 @@ func RunAsStep[R any](ctx DBOSContext, fn GenericStepFunc[R]) (R, error) {
766764
// Type-erase the function
767765
typeErasedFn := StepFunc(func(ctx context.Context) (any, error) { return fn(ctx) })
768766
typeErasedFnName := runtime.FuncForPC(reflect.ValueOf(typeErasedFn).Pointer()).Name()
769-
typeErasedStepNameMutex.Lock()
770-
typeErasedStepNameToStepName[typeErasedFnName] = stepName
771-
typeErasedStepNameMutex.Unlock()
767+
typeErasedStepNameToStepName.LoadOrStore(typeErasedFnName, stepName)
772768

773769
// Call the executor method and pass through the result/error
774770
result, err := ctx.RunAsStep(ctx, typeErasedFn)

0 commit comments

Comments
 (0)