From 18b212985d3fb44f91fedf43f70573a57d12a4b3 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 30 Oct 2024 16:48:59 +0800 Subject: [PATCH 1/2] Fix memory leaks and optimize the download process. --- main.go | 5 ++++- widgets/dashboard/handler.go | 4 +++- widgets/form/handler.go | 4 +++- widgets/form/process.go | 6 +++--- widgets/list/handler.go | 4 +++- widgets/list/process.go | 6 +++--- widgets/table/handler.go | 4 +++- widgets/table/process.go | 6 +++--- 8 files changed, 25 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 25d3bfa2b..eceea115e 100644 --- a/main.go +++ b/main.go @@ -10,10 +10,13 @@ import ( _ "github.com/yaoapp/yao/helper" _ "github.com/yaoapp/yao/openai" _ "github.com/yaoapp/yao/wework" + // _ "net/http/pprof" ) -// 主程序 func main() { + // go func() { + // log.Println(http.ListenAndServe("localhost:6060", nil)) + // }() utils.Init() cmd.Execute() } diff --git a/widgets/dashboard/handler.go b/widgets/dashboard/handler.go index 8cc99df49..69dcee5ea 100644 --- a/widgets/dashboard/handler.go +++ b/widgets/dashboard/handler.go @@ -57,11 +57,13 @@ func processHandler(p *action.Process, process *gouProcess.Process) (interface{} return nil, fmt.Errorf("[dashboard] %s %s -> %s %s", dashboard.ID, p.Name, name, err.Error()) } - res, err := act.WithGlobal(process.Global).WithSID(process.Sid).Exec() + err = act.WithGlobal(process.Global).WithSID(process.Sid).Execute() if err != nil { log.Error("[dashboard] %s %s -> %s %s", dashboard.ID, p.Name, name, err.Error()) return nil, fmt.Errorf("[dashboard] %s %s -> %s %s", dashboard.ID, p.Name, name, err.Error()) } + defer act.Release() + res := act.Value() // Compute View err = dashboard.ComputeView(p.Name, process, res, dashboard.getField()) diff --git a/widgets/form/handler.go b/widgets/form/handler.go index e130b7d8b..148ad55db 100644 --- a/widgets/form/handler.go +++ b/widgets/form/handler.go @@ -63,11 +63,13 @@ func processHandler(p *action.Process, process *gouProcess.Process) (interface{} return nil, fmt.Errorf("[form] %s %s -> %s %s", form.ID, p.Name, name, err.Error()) } - res, err := act.WithGlobal(process.Global).WithSID(process.Sid).Exec() + err = act.WithGlobal(process.Global).WithSID(process.Sid).Execute() if err != nil { log.Error("[form] %s %s -> %s %s", form.ID, p.Name, name, err.Error()) return nil, fmt.Errorf("[form] %s %s -> %s %s", form.ID, p.Name, name, err.Error()) } + defer act.Release() + res := act.Value() // Compute View err = form.ComputeView(p.Name, process, res, form.getField()) diff --git a/widgets/form/process.go b/widgets/form/process.go index 7ccd2e2e9..26d519fea 100644 --- a/widgets/form/process.go +++ b/widgets/form/process.go @@ -118,13 +118,13 @@ func processDownload(process *gouProcess.Process) interface{} { } // Excute process - res, err := p.WithGlobal(process.Global).WithSID(claims.SID).Exec() + err = p.WithGlobal(process.Global).WithSID(claims.SID).Execute() if err != nil { log.Error("[downalod] %s.%s %s", form.ID, field, err.Error()) exception.New("[downalod] %s.%s %s", 500, form.ID, field, err.Error()).Throw() } - - return res + defer p.Release() + return p.Value() } func processUpload(process *gouProcess.Process) interface{} { diff --git a/widgets/list/handler.go b/widgets/list/handler.go index 13e83c1fe..b77423a25 100644 --- a/widgets/list/handler.go +++ b/widgets/list/handler.go @@ -63,11 +63,13 @@ func processHandler(p *action.Process, process *gouProcess.Process) (interface{} return nil, fmt.Errorf("[list] %s %s -> %s %s", list.ID, p.Name, name, err.Error()) } - res, err := act.WithGlobal(process.Global).WithSID(process.Sid).Exec() + err = act.WithGlobal(process.Global).WithSID(process.Sid).Execute() if err != nil { log.Error("[list] %s %s -> %s %s", list.ID, p.Name, name, err.Error()) return nil, fmt.Errorf("[list] %s %s -> %s %s", list.ID, p.Name, name, err.Error()) } + defer act.Release() + res := act.Value() // Compute View err = list.ComputeView(p.Name, process, res, list.getField()) diff --git a/widgets/list/process.go b/widgets/list/process.go index 00b89cb65..9cd29fc4a 100644 --- a/widgets/list/process.go +++ b/widgets/list/process.go @@ -100,13 +100,13 @@ func processDownload(process *gouProcess.Process) interface{} { } // Excute process - res, err := p.WithGlobal(process.Global).WithSID(claims.SID).Exec() + err = p.WithGlobal(process.Global).WithSID(claims.SID).Execute() if err != nil { log.Error("[downalod] %s.%s %s", list.ID, field, err.Error()) exception.New("[downalod] %s.%s %s", 500, list.ID, field, err.Error()).Throw() } - - return res + defer p.Release() + return p.Value() } func processUpload(process *gouProcess.Process) interface{} { diff --git a/widgets/table/handler.go b/widgets/table/handler.go index 7c567b63a..b0c83c969 100644 --- a/widgets/table/handler.go +++ b/widgets/table/handler.go @@ -70,11 +70,13 @@ func processHandler(p *action.Process, process *gouProcess.Process) (interface{} return nil, fmt.Errorf("[table] %s %s -> %s %s", tab.ID, p.Name, name, err.Error()) } - res, err := act.WithGlobal(process.Global).WithSID(process.Sid).Exec() + err = act.WithGlobal(process.Global).WithSID(process.Sid).Execute() if err != nil { log.Error("[table] %s %s -> %s %s %v", tab.ID, p.Name, name, err.Error(), args) return nil, fmt.Errorf("[table] %s %s -> %s %s", tab.ID, p.Name, name, err.Error()) } + defer act.Release() + res := act.Value() // Compute View err = tab.ComputeView(p.Name, process, res, tab.getField()) diff --git a/widgets/table/process.go b/widgets/table/process.go index e54a3d800..444c41e53 100644 --- a/widgets/table/process.go +++ b/widgets/table/process.go @@ -103,13 +103,13 @@ func processDownload(process *gouProcess.Process) interface{} { } // Excute process - res, err := p.WithGlobal(process.Global).WithSID(claims.SID).Exec() + err = p.WithGlobal(process.Global).WithSID(claims.SID).Execute() if err != nil { log.Error("[downalod] %s.%s %s", tab.ID, field, err.Error()) exception.New("[downalod] %s.%s %s", 500, tab.ID, field, err.Error()).Throw() } - - return res + defer p.Release() + return p.Value() } func processUpload(process *gouProcess.Process) interface{} { From 00b064929693d9555c615bf344d26bff88da80d1 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 30 Oct 2024 17:10:56 +0800 Subject: [PATCH 2/2] Fix the Download Process tests --- widgets/dashboard/process_test.go | 3 +++ widgets/form/process_test.go | 16 ++++++++++++++-- widgets/table/process_test.go | 13 ++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/widgets/dashboard/process_test.go b/widgets/dashboard/process_test.go index 64818f007..8ac38a9c7 100644 --- a/widgets/dashboard/process_test.go +++ b/widgets/dashboard/process_test.go @@ -1,6 +1,7 @@ package dashboard import ( + "fmt" "net/url" "testing" @@ -45,6 +46,8 @@ func TestProcessComponent(t *testing.T) { t.Fatal(err) } + fmt.Println(res) + pets, ok := res.([]maps.MapStr) assert.True(t, ok) assert.Equal(t, 2, len(pets)) diff --git a/widgets/form/process_test.go b/widgets/form/process_test.go index e6d240a70..f9f879bcc 100644 --- a/widgets/form/process_test.go +++ b/widgets/form/process_test.go @@ -2,6 +2,7 @@ package form import ( "fmt" + "io" "net/url" "os" "testing" @@ -152,7 +153,8 @@ func TestProcessDelete(t *testing.T) { t.Fatal(err) } - _, err = process.New("yao.form.find", "pet", 1).Exec() + res, err := process.New("yao.form.find", "pet", 1).Exec() + fmt.Println("err", res, err) assert.Contains(t, err.Error(), "ID=1") } @@ -251,8 +253,18 @@ func TestProcessDownload(t *testing.T) { } body, ok := res.(map[string]interface{}) + reader, ok := body["content"].(io.ReadCloser) + if !ok { + t.Fatal("content not found") + } + defer reader.Close() + content, err := io.ReadAll(reader) + if err != nil { + t.Fatal(err) + } + assert.True(t, ok) - assert.Equal(t, []byte("Hello"), body["content"]) + assert.Equal(t, []byte("Hello"), content) assert.Equal(t, "text/plain; charset=utf-8", body["type"]) } diff --git a/widgets/table/process_test.go b/widgets/table/process_test.go index d40d52df4..1f7c283c9 100644 --- a/widgets/table/process_test.go +++ b/widgets/table/process_test.go @@ -2,6 +2,7 @@ package table import ( "fmt" + "io" "net/url" "os" "testing" @@ -451,8 +452,18 @@ func TestProcessDownload(t *testing.T) { } body, ok := res.(map[string]interface{}) + reader, ok := body["content"].(io.ReadCloser) + if !ok { + t.Fatal("content not found") + } + defer reader.Close() + content, err := io.ReadAll(reader) + if err != nil { + t.Fatal(err) + } + assert.True(t, ok) - assert.Equal(t, []byte("Hello"), body["content"]) + assert.Equal(t, []byte("Hello"), content) assert.Equal(t, "text/plain; charset=utf-8", body["type"]) }