diff --git a/statefun-sdk-go/v3/pkg/statefun/handler.go b/statefun-sdk-go/v3/pkg/statefun/handler.go index 410955f5c..39f3af7de 100644 --- a/statefun-sdk-go/v3/pkg/statefun/handler.go +++ b/statefun-sdk-go/v3/pkg/statefun/handler.go @@ -58,40 +58,57 @@ type RequestReplyHandler interface { Invoke(ctx context.Context, payload []byte) ([]byte, error) } +type handler struct { + config StatefulFunctionsBuilderConfig + module map[TypeName]StatefulFunction + stateSpecs map[TypeName]map[string]*protocol.FromFunction_PersistedValueSpec +} + +type StatefulFunctionsBuilderConfig struct { + Verbose bool +} + // StatefulFunctionsBuilder creates a new StatefulFunctions registry. -func StatefulFunctionsBuilder() StatefulFunctions { - return &handler{ +func StatefulFunctionsBuilder(config ...StatefulFunctionsBuilderConfig) StatefulFunctions { + h := &handler{ + config: StatefulFunctionsBuilderConfig{ + Verbose: true, + }, module: map[TypeName]StatefulFunction{}, stateSpecs: map[TypeName]map[string]*protocol.FromFunction_PersistedValueSpec{}, } -} -type handler struct { - module map[TypeName]StatefulFunction - stateSpecs map[TypeName]map[string]*protocol.FromFunction_PersistedValueSpec + if len(config) > 0 { + h.config = config[0] + } + + return h } func (h *handler) WithSpec(spec StatefulFunctionSpec) error { - log.Printf("registering Stateful Function %v\n", spec.FunctionType) + if h.config.Verbose { + log.Printf("registering Stateful Function %v\n", spec.FunctionType) + } + if _, exists := h.module[spec.FunctionType]; exists { err := fmt.Errorf("failed to register Stateful Function %s, there is already a spec registered under that type", spec.FunctionType) - log.Println(err.Error()) return err } if spec.Function == nil { err := fmt.Errorf("failed to register Stateful Function %s, the Function instance cannot be nil", spec.FunctionType) - log.Println(err.Error()) return err } valueSpecs := make(map[string]*protocol.FromFunction_PersistedValueSpec, len(spec.States)) for _, state := range spec.States { - log.Printf("registering state specification %v\n", state) + if h.config.Verbose { + log.Printf("registering state specification %v\n", state) + } + if err := validateValueSpec(state); err != nil { err := fmt.Errorf("failed to register Stateful Function %s: %w", spec.FunctionType, err) - log.Println(err.Error()) return err } @@ -149,12 +166,11 @@ func (h *handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { response, err := h.Invoke(request.Context(), buffer.Bytes()) if err != nil { - log.Println(err.Error()) http.Error(writer, err.Error(), http.StatusInternalServerError) return } - _, _ = writer.Write(response) + writer.Write(response) } func (h *handler) Invoke(ctx context.Context, payload []byte) ([]byte, error) { @@ -194,9 +210,11 @@ func (h *handler) invoke(ctx context.Context, toFunction *protocol.ToFunction) ( storageFactory := newStorageFactory(batch, h.stateSpecs[self.FunctionType]) if missing := storageFactory.getMissingSpecs(); missing != nil { - log.Printf("missing state specs for function type %v", self) - for _, spec := range missing { - log.Printf("registering missing specs %v", spec) + if h.config.Verbose { + log.Printf("missing state specs for function type %v", self) + for _, spec := range missing { + log.Printf("registering missing specs %v", spec) + } } return &protocol.FromFunction{ Response: &protocol.FromFunction_IncompleteInvocationContext_{ diff --git a/statefun-sdk-go/v3/pkg/statefun/handler_test.go b/statefun-sdk-go/v3/pkg/statefun/handler_test.go index e315c4c5d..3740211b1 100644 --- a/statefun-sdk-go/v3/pkg/statefun/handler_test.go +++ b/statefun-sdk-go/v3/pkg/statefun/handler_test.go @@ -25,7 +25,11 @@ func toProtocolAddress(address *Address) *protocol.Address { // helper to create a handler and invoke the function func invokeStatefulFunction(ctx context.Context, target *Address, caller *Address, argument *protocol.TypedValue, statefulFunction StatefulFunction) error { - builder := StatefulFunctionsBuilder() + builder := StatefulFunctionsBuilder( + StatefulFunctionsBuilderConfig{ + Verbose: true, + }, + ) err := builder.WithSpec(StatefulFunctionSpec{ FunctionType: target.FunctionType, Function: statefulFunction,