From c0d336ca456093b2b7c0b585dbe08f62cbc8ca83 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 22 Dec 2023 02:04:11 +0700 Subject: all: add [context.Context] to Local and Play Passing context allow the command Local or Play to be cancelled when running in asynchronous mode, in this case when awwan run with WUI. --- http_server.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'http_server.go') diff --git a/http_server.go b/http_server.go index 70dc7a8..c45cde2 100644 --- a/http_server.go +++ b/http_server.go @@ -5,6 +5,7 @@ package awwan import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -49,6 +50,10 @@ type httpServer struct { // idExecRes contains the execution ID and its response. idExecRes map[string]*ExecResponse + // idContextCancel contains the execution ID and its context + // cancellation function. + idContextCancel map[string]context.CancelFunc + aww *Awwan memfsBase *memfs.MemFS // The files caches. @@ -63,7 +68,8 @@ func newHTTPServer(aww *Awwan, address string) (httpd *httpServer, err error) { ) httpd = &httpServer{ - idExecRes: make(map[string]*ExecResponse), + idExecRes: make(map[string]*ExecResponse), + idContextCancel: make(map[string]context.CancelFunc), aww: aww, baseDir: aww.BaseDir, @@ -701,11 +707,20 @@ func (httpd *httpServer) Execute(epr *libhttp.EndpointRequest) (resb []byte, err httpd.idExecRes[execRes.ID] = execRes + var ( + ctx = context.Background() + ctxDoCancel context.CancelFunc + ) + + ctx, ctxDoCancel = context.WithCancel(ctx) + + httpd.idContextCancel[execRes.ID] = ctxDoCancel + go func() { if req.Mode == CommandModeLocal { - err = httpd.aww.Local(req) + err = httpd.aww.Local(ctx, req) } else { - err = httpd.aww.Play(req) + err = httpd.aww.Play(ctx, req) } execRes.end(err) }() -- cgit v1.3