Skip to content

Commit

Permalink
refactor: unify blocking accept
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 25, 2024
1 parent f788b49 commit 2cf0587
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/port/inport.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (p *InPort) Open(proc *process.Process) *packet.Reader {
}))

openHooks.Open(proc)
listeners.Accept(proc)
go listeners.Accept(proc)

return reader
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/port/listener.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package port

import "github.com/siyul-park/uniflow/pkg/process"
import (
"github.com/siyul-park/uniflow/pkg/process"
"sync"
)

// Listener is an interface for handling process events.
type Listener interface {
Expand All @@ -24,10 +27,16 @@ func ListenFunc(accept func(proc *process.Process)) Listener {
}

func (l Listeners) Accept(proc *process.Process) {
wg := sync.WaitGroup{}
for _, listener := range l {
listener := listener
go listener.Accept(proc)
wg.Add(1)
go func() {
defer wg.Done()
listener.Accept(proc)
}()
}
wg.Wait()
}

func (l *listener) Accept(proc *process.Process) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/port/outport.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (p *OutPort) Open(proc *process.Process) *packet.Writer {
p.mu.Unlock()

openHooks.Open(proc)
listeners.Accept(proc)
go listeners.Accept(proc)

proc.AddExitHook(process.ExitFunc(func(_ error) {
p.mu.Lock()
Expand Down

0 comments on commit 2cf0587

Please sign in to comment.