Skip to content

Commit

Permalink
Merge pull request #108 from sachaos/fix-suspend-bug
Browse files Browse the repository at this point in the history
Do not execute command when suspended
  • Loading branch information
sachaos authored Oct 15, 2023
2 parents 5a6a620 + e676bad commit 8641b5e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
49 changes: 43 additions & 6 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,58 @@ import "time"

type newSnapFunc func(int64, *Snapshot, chan<- struct{}) *Snapshot

func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)

go func() {
var s *Snapshot

t := time.Tick(interval)

for now := range t {
select {
case isSuspended = <-isSuspendedQueue:
default:
}

if isSuspended {
continue
}

finish := make(chan struct{})
id := (now.UnixNano() - begin) / int64(time.Millisecond)
s = newSnap(id, s, finish)
c <- s
}
}()

return c
return c, isSuspendedQueue
}

func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)

go func() {
var s *Snapshot

begin := time.Now().UnixNano()

for {
select {
case isSuspended = <-isSuspendedQueue:
default:
}

if isSuspended {
time.Sleep(interval)

continue
}

finish := make(chan struct{})
start := time.Now()
id := (start.UnixNano() - begin) / int64(time.Millisecond)
Expand All @@ -52,18 +76,31 @@ func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapsh
}
}()

return c
return c, isSuspendedQueue
}

func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) (<-chan *Snapshot, chan<- bool) {
c := make(chan *Snapshot)
isSuspended := false
isSuspendedQueue := make(chan bool)

go func() {
var s *Snapshot

begin := time.Now().UnixNano()

for {
select {
case isSuspended = <-isSuspendedQueue:
default:
}

if isSuspended {
time.Sleep(interval)

continue
}

finish := make(chan struct{})
id := (time.Now().UnixNano() - begin) / int64(time.Millisecond)
s = newSnap(id, s, finish)
Expand All @@ -75,5 +112,5 @@ func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Sna
}
}()

return c
return c, isSuspendedQueue
}
36 changes: 19 additions & 17 deletions viddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ type Viddy struct {
statusView *tview.TextView
queryEditor *tview.InputField

snapshotQueue <-chan *Snapshot
queue chan int64
finishedQueue chan int64
diffQueue chan int64
snapshotQueue <-chan *Snapshot
isSuspendedQueue chan<- bool
queue chan int64
finishedQueue chan int64
diffQueue chan int64

currentID int64
latestFinishedID int64
Expand Down Expand Up @@ -103,15 +104,18 @@ func NewViddy(conf *config) *Viddy {
return NewSnapshot(id, conf.runtime.cmd, conf.runtime.args, conf.general.shell, conf.general.shellOptions, before, finish)
}

var snapshotQueue <-chan *Snapshot
var (
snapshotQueue <-chan *Snapshot
isSuspendedQueue chan<- bool
)

switch conf.runtime.mode {
case ViddyIntervalModeClockwork:
snapshotQueue = ClockSnapshot(begin, newSnap, conf.runtime.interval)
snapshotQueue, isSuspendedQueue = ClockSnapshot(begin, newSnap, conf.runtime.interval)
case ViddyIntervalModeSequential:
snapshotQueue = SequentialSnapshot(newSnap, conf.runtime.interval)
snapshotQueue, isSuspendedQueue = SequentialSnapshot(newSnap, conf.runtime.interval)
case ViddyIntervalModePrecise:
snapshotQueue = PreciseSnapshot(newSnap, conf.runtime.interval)
snapshotQueue, isSuspendedQueue = PreciseSnapshot(newSnap, conf.runtime.interval)
}

return &Viddy{
Expand All @@ -126,10 +130,11 @@ func NewViddy(conf *config) *Viddy {

historyRowCount: map[int64]int{},

snapshotQueue: snapshotQueue,
queue: make(chan int64),
finishedQueue: make(chan int64),
diffQueue: make(chan int64, 100),
snapshotQueue: snapshotQueue,
isSuspendedQueue: isSuspendedQueue,
queue: make(chan int64),
finishedQueue: make(chan int64),
diffQueue: make(chan int64, 100),

isRingBell: conf.general.bell,
isShowDiff: conf.general.differences,
Expand Down Expand Up @@ -253,7 +258,7 @@ func (v *Viddy) diffQueueHandler() {
}
}

//nolint:funlen,cyclop
//nolint:funlen
func (v *Viddy) queueHandler() {
for {
func() {
Expand Down Expand Up @@ -285,10 +290,6 @@ func (v *Viddy) queueHandler() {
v.updateSelection()
}
case id := <-v.queue:
if v.isSuspend {
return
}

s := v.getSnapShot(id)
idCell := tview.NewTableCell(strconv.FormatInt(s.id, 10)).SetTextColor(tview.Styles.SecondaryTextColor)
additionCell := tview.NewTableCell("").SetTextColor(tcell.ColorGreen)
Expand Down Expand Up @@ -611,6 +612,7 @@ func (v *Viddy) Run() error {
switch event.Rune() {
case 's':
v.isSuspend = !v.isSuspend
v.isSuspendedQueue <- v.isSuspend
case 'b':
v.isRingBell = !v.isRingBell
case 'd':
Expand Down

0 comments on commit 8641b5e

Please sign in to comment.