Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions statefun-sdk-go/v3/pkg/statefun/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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_{
Expand Down
6 changes: 5 additions & 1 deletion statefun-sdk-go/v3/pkg/statefun/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down