Skip to content

Commit

Permalink
fix: [SyslogRelay] adjust the logic of stop
Browse files Browse the repository at this point in the history
  • Loading branch information
electricbubble committed Apr 24, 2021
1 parent c40aa39 commit b6b9bb3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions lockdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Test_lockdown_SyslogRelayService(t *testing.T) {
if err != nil {
t.Fatal(err)
}
syslogRelaySrv.Stop()

lines := syslogRelaySrv.Lines()

Expand Down
20 changes: 14 additions & 6 deletions syslogrelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ var _ SyslogRelay = (*syslogRelay)(nil)

func newSyslogRelay(client *libimobiledevice.SyslogRelayClient) *syslogRelay {
r := &syslogRelay{
client: client,
stop: make(chan bool),
client: client,
stop: make(chan bool),
isReading: false,
}
r.reader = bufio.NewReader(r.client.InnerConn().RawConn())
return r
Expand All @@ -22,18 +23,23 @@ func newSyslogRelay(client *libimobiledevice.SyslogRelayClient) *syslogRelay {
type syslogRelay struct {
client *libimobiledevice.SyslogRelayClient

reader *bufio.Reader
stop chan bool
reader *bufio.Reader
stop chan bool
isReading bool
}

func (r *syslogRelay) Lines() <-chan string {
out := make(chan string)
r.isReading = true

go func() {
defer func() {
close(out)
r.isReading = false
}()
for {
select {
case <-r.stop:
close(out)
return
default:
bs, err := r.readLine()
Expand All @@ -54,7 +60,9 @@ func (r *syslogRelay) Lines() <-chan string {
}

func (r *syslogRelay) Stop() {
r.stop <- true
if r.isReading {
r.stop <- true
}
}

func (r *syslogRelay) readLine() ([]byte, error) {
Expand Down

0 comments on commit b6b9bb3

Please sign in to comment.