Better handling of concurrent calls to observers

We now call wg.Add(n) before we loop over n observers.  We also run the
loop inside a goroutine and therefore more quickly handle all (pid,
offset) pairs.
This commit is contained in:
Özgür Kesim 2020-01-16 13:30:41 +01:00
parent 8033525f3f
commit 4ba4442100

View File

@ -122,7 +122,8 @@ func (S *Scanner) Run() error {
proc.Close() proc.Close()
var wg sync.WaitGroup var wg sync.WaitGroup // To be able to wait for all the observers to finish
for _, pinfo := range infos { for _, pinfo := range infos {
var pid_s = pinfo.Name() var pid_s = pinfo.Name()
@ -134,22 +135,25 @@ func (S *Scanner) Run() error {
} else if offset, found := S.searchSymbolIn(pid); !found { } else if offset, found := S.searchSymbolIn(pid); !found {
continue continue
} else { } else {
// Notify the observers. // Call the observers with (pid, offset), in the
for n, observer := range S.observers { // background.
wg.Add(1) wg.Add(len(S.observers)) // Wait for this many goroutines
go func() { go func() {
err = observer(pid, offset) for n, observer := range S.observers {
if err != nil { go func() {
S.Printf("S.observer[%d](%d, %d) error: %v", n, pid, offset, err) err = observer(pid, offset)
// TODO: accumulate errors from all Observers. if err != nil {
} S.Printf("S.observer[%d](%d, %d) error: %v", n, pid, offset, err)
wg.Done() // TODO: accumulate errors from all Observers.
}() }
} wg.Done()
}()
}
}()
} }
} }
wg.Wait() wg.Wait() // Wait for all observers to finish
return S.err return S.err
} }