lib: Replace done channel with contexts in and add names to util services (#6166)
This commit is contained in:
@@ -7,7 +7,9 @@
|
||||
package syncthing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/thejerf/suture"
|
||||
@@ -29,19 +31,19 @@ func newAuditService(w io.Writer, evLogger events.Logger) *auditService {
|
||||
w: w,
|
||||
sub: evLogger.Subscribe(events.AllEvents),
|
||||
}
|
||||
s.Service = util.AsService(s.serve)
|
||||
s.Service = util.AsService(s.serve, s.String())
|
||||
return s
|
||||
}
|
||||
|
||||
// serve runs the audit service.
|
||||
func (s *auditService) serve(stop chan struct{}) {
|
||||
func (s *auditService) serve(ctx context.Context) {
|
||||
enc := json.NewEncoder(s.w)
|
||||
|
||||
for {
|
||||
select {
|
||||
case ev := <-s.sub.C():
|
||||
enc.Encode(ev)
|
||||
case <-stop:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -52,3 +54,7 @@ func (s *auditService) Stop() {
|
||||
s.Service.Stop()
|
||||
s.sub.Unsubscribe()
|
||||
}
|
||||
|
||||
func (s *auditService) String() string {
|
||||
return fmt.Sprintf("auditService@%p", s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user