Skip to content

Commit

Permalink
Merge pull request #767 from trheyi/main
Browse files Browse the repository at this point in the history
Fix memory leaks and optimize the download process.
  • Loading branch information
trheyi authored Oct 30, 2024
2 parents d4c0a23 + 00b0649 commit 809c2ca
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 17 deletions.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
4 changes: 3 additions & 1 deletion widgets/dashboard/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 3 additions & 0 deletions widgets/dashboard/process_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dashboard

import (
"fmt"
"net/url"
"testing"

Expand Down Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion widgets/form/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions widgets/form/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
16 changes: 14 additions & 2 deletions widgets/form/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package form

import (
"fmt"
"io"
"net/url"
"os"
"testing"
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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"])
}

Expand Down
4 changes: 3 additions & 1 deletion widgets/list/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions widgets/list/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
4 changes: 3 additions & 1 deletion widgets/table/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions widgets/table/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
13 changes: 12 additions & 1 deletion widgets/table/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package table

import (
"fmt"
"io"
"net/url"
"os"
"testing"
Expand Down Expand Up @@ -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"])
}

Expand Down

0 comments on commit 809c2ca

Please sign in to comment.