forked from TrueCloudLab/restic
ui/progress: extract signal handling into own package
This commit is contained in:
parent
023eea6463
commit
34afc93ddc
5 changed files with 30 additions and 17 deletions
|
@ -1,11 +1,11 @@
|
||||||
package progress
|
package progress
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
|
"github.com/restic/restic/internal/ui/signals"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Func is a callback for a Counter.
|
// A Func is a callback for a Counter.
|
||||||
|
@ -31,11 +31,6 @@ type Counter struct {
|
||||||
|
|
||||||
// New starts a new Counter.
|
// New starts a new Counter.
|
||||||
func New(interval time.Duration, report Func) *Counter {
|
func New(interval time.Duration, report Func) *Counter {
|
||||||
signals.Once.Do(func() {
|
|
||||||
signals.ch = make(chan os.Signal, 1)
|
|
||||||
setupSignals()
|
|
||||||
})
|
|
||||||
|
|
||||||
c := &Counter{
|
c := &Counter{
|
||||||
report: report,
|
report: report,
|
||||||
start: time.Now(),
|
start: time.Now(),
|
||||||
|
@ -93,12 +88,13 @@ func (c *Counter) run() {
|
||||||
if c.tick != nil {
|
if c.tick != nil {
|
||||||
tick = c.tick.C
|
tick = c.tick.C
|
||||||
}
|
}
|
||||||
|
signalsCh := signals.GetProgressChannel()
|
||||||
for {
|
for {
|
||||||
var now time.Time
|
var now time.Time
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case now = <-tick:
|
case now = <-tick:
|
||||||
case sig := <-signals.ch:
|
case sig := <-signalsCh:
|
||||||
debug.Log("Signal received: %v\n", sig)
|
debug.Log("Signal received: %v\n", sig)
|
||||||
now = time.Now()
|
now = time.Now()
|
||||||
case <-c.stop:
|
case <-c.stop:
|
||||||
|
@ -108,10 +104,3 @@ func (c *Counter) run() {
|
||||||
c.report(c.get(), now.Sub(c.start), false)
|
c.report(c.get(), now.Sub(c.start), false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX The fact that signals is a single global variable means that only one
|
|
||||||
// Counter receives each incoming signal.
|
|
||||||
var signals struct {
|
|
||||||
ch chan os.Signal
|
|
||||||
sync.Once
|
|
||||||
}
|
|
||||||
|
|
24
internal/ui/signals/signals.go
Normal file
24
internal/ui/signals/signals.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package signals
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetProgressChannel returns a channel with which a single listener
|
||||||
|
// receives each incoming signal.
|
||||||
|
func GetProgressChannel() <-chan os.Signal {
|
||||||
|
signals.Once.Do(func() {
|
||||||
|
signals.ch = make(chan os.Signal, 1)
|
||||||
|
setupSignals()
|
||||||
|
})
|
||||||
|
|
||||||
|
return signals.ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX The fact that signals is a single global variable means that only one
|
||||||
|
// listener receives each incoming signal.
|
||||||
|
var signals struct {
|
||||||
|
ch chan os.Signal
|
||||||
|
sync.Once
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
// +build darwin dragonfly freebsd netbsd openbsd
|
// +build darwin dragonfly freebsd netbsd openbsd
|
||||||
|
|
||||||
package progress
|
package signals
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/signal"
|
"os/signal"
|
|
@ -1,6 +1,6 @@
|
||||||
// +build aix linux solaris
|
// +build aix linux solaris
|
||||||
|
|
||||||
package progress
|
package signals
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/signal"
|
"os/signal"
|
|
@ -1,3 +1,3 @@
|
||||||
package progress
|
package signals
|
||||||
|
|
||||||
func setupSignals() {}
|
func setupSignals() {}
|
Loading…
Reference in a new issue