[runtime] Add cancelation of bound method calls and context passing

This commit is contained in:
stffabi
2024-03-04 10:34:14 +01:00
parent 7e687750b2
commit bfe9a9d015
25 changed files with 319 additions and 124 deletions
+52 -2
View File
@@ -1,6 +1,7 @@
package application
import (
"context"
"encoding/json"
"fmt"
"net/http"
@@ -20,6 +21,28 @@ func (m *MessageProcessor) callCallback(window Window, callID *string, result st
window.CallResponse(*callID, result)
}
func (m *MessageProcessor) processCallCancelMethod(method int, rw http.ResponseWriter, r *http.Request, window Window, params QueryParams) {
args, err := params.Args()
if err != nil {
m.httpError(rw, "Unable to parse arguments: %s", err.Error())
return
}
callID := args.String("call-id")
if callID == nil || *callID == "" {
m.Error("call-id is required")
return
}
m.l.Lock()
cancel := m.runningCalls[*callID]
m.l.Unlock()
if cancel != nil {
cancel()
}
m.ok(rw)
}
func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter, r *http.Request, window Window, params QueryParams) {
args, err := params.Args()
if err != nil {
@@ -27,10 +50,11 @@ func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter,
return
}
callID := args.String("call-id")
if callID == nil {
if callID == nil || *callID == "" {
m.Error("call-id is required")
return
}
switch method {
case CallBinding:
var options CallOptions
@@ -53,8 +77,34 @@ func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter,
m.callErrorCallback(window, "Error getting binding for method: %s", callID, fmt.Errorf("method ID '%s' not found", options.Name()))
return
}
ctx, cancel := context.WithCancel(context.WithoutCancel(r.Context()))
ambigiousID := false
m.l.Lock()
if m.runningCalls[*callID] != nil {
ambigiousID = true
} else {
m.runningCalls[*callID] = cancel
}
m.l.Unlock()
if ambigiousID {
cancel()
m.callErrorCallback(window, "Error calling method: %s, a method call with the same id is already running", callID, err)
return
}
go func() {
result, err := boundMethod.Call(options.Args)
defer func() {
cancel()
m.l.Lock()
delete(m.runningCalls, *callID)
m.l.Unlock()
}()
result, err := boundMethod.Call(ctx, options.Args)
if err != nil {
m.callErrorCallback(window, "Error calling method: %s", callID, err)
return