Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit 8499381

Browse files
committed
fix leak memory
1 parent 0b2e4cd commit 8499381

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ In restart case, children will re-use last parameters (if task don't change it)
7575
Child, doesn't return any value from task.
7676
You need add code to get value from task if you needed.
7777

78+
After use the supervisor done, you need to remove by `RemoveSupervisor` or `RemoveSupervisorById` to avoid leak memory.
79+
7880
Supervisor -> Child -> call user functions
7981

8082
Basic supervisor's flow:
@@ -135,6 +137,9 @@ time.Sleep(15 * time.Second)
135137
// stop all worker.
136138
// this function depends how long fun return.
137139
sup.Stop()
140+
141+
// clear all children in supervisor to avoid memory leak.
142+
sup.Done()
138143
```
139144

140145
Supervisor support context by create supervisor by function `NewSupervisorWithContext`.

caller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func invokeFun(fun any, args ...any) (ret []any, err error) {
2525
fn := reflect.ValueOf(fun)
2626
fnType := fn.Type()
2727
numIn := fnType.NumIn()
28+
2829
if numIn > len(args) {
2930
return nil, fmt.Errorf("function must have minimum %d params. Have %d", numIn, len(args))
3031
}

stream.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ func (p *EasyStream) Run() (retErr error) {
127127
cmd := <-p.cmdCh
128128
switch cmd {
129129
case iQUIT:
130-
for _, w := range p.workerList {
130+
for i, w := range p.workerList {
131131
w.cmd <- msg{msgType: iQUIT}
132+
delete(p.workerList, i)
132133
}
133134
}
134135
}

supervisor.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ func (s *Supervisor) StopChild(id int64) {
226226
}
227227
}
228228

229+
/*
230+
Clear all children. Call after Stop.
231+
*/
232+
func (s *Supervisor) Done() {
233+
for k := range s.children {
234+
delete(s.children, k)
235+
}
236+
}
237+
229238
/*
230239
Return statistics of supervisor.
231240
total: Number of children in supervisor.

0 commit comments

Comments
 (0)