From c78933f1df9b2ef1636c2de101f314478a3bd6ef Mon Sep 17 00:00:00 2001 From: Or Tzabary Date: Fri, 8 Jul 2016 17:05:46 +0300 Subject: [PATCH 1/3] Feature flag --- gor.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/gor.go b/gor.go index 8788a74..c0f06c7 100644 --- a/gor.go +++ b/gor.go @@ -22,6 +22,7 @@ var ( mode string cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") memprofile = flag.String("memprofile", "", "write memory profile to this file") + timeout = flag.Int("timeout", 0, "timeout to stop gor, value in seconds") ) func loggingMiddleware(next http.Handler) http.Handler { @@ -88,7 +89,15 @@ func main() { os.Exit(1) }() - Start(nil) + if *timeout >= 1 { + log.Println("Running gor with timeout of", *timeout, "seconds") + stop := make(chan int) + timeoutGor(stop, *timeout) + + Start(stop) + } else { + Start(nil) + } } func profileCPU(cpuprofile string) { @@ -119,3 +128,10 @@ func profileMEM(memprofile string) { }) } } + +func timeoutGor(stop chan int, seconds int) { + time.AfterFunc(time.Duration(seconds)*time.Second, func() { + log.Println("Stopping gor after", seconds, "seconds") + close(stop) + }) +} From 6620d256264802aa4d67d26ad92437a758be4ff8 Mon Sep 17 00:00:00 2001 From: Or Tzabary Date: Fri, 8 Jul 2016 18:57:42 +0300 Subject: [PATCH 2/3] Change flag name, use of DurationVar instead of int --- gor.go | 13 ++++++------- settings.go | 8 +++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/gor.go b/gor.go index c0f06c7..70f3fa7 100644 --- a/gor.go +++ b/gor.go @@ -22,7 +22,6 @@ var ( mode string cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") memprofile = flag.String("memprofile", "", "write memory profile to this file") - timeout = flag.Int("timeout", 0, "timeout to stop gor, value in seconds") ) func loggingMiddleware(next http.Handler) http.Handler { @@ -89,10 +88,10 @@ func main() { os.Exit(1) }() - if *timeout >= 1 { - log.Println("Running gor with timeout of", *timeout, "seconds") + if Settings.exitAfter >= 1 { + log.Println("Running gor for a duration of", Settings.exitAfter) stop := make(chan int) - timeoutGor(stop, *timeout) + stopAfter(stop, Settings.exitAfter) Start(stop) } else { @@ -129,9 +128,9 @@ func profileMEM(memprofile string) { } } -func timeoutGor(stop chan int, seconds int) { - time.AfterFunc(time.Duration(seconds)*time.Second, func() { - log.Println("Stopping gor after", seconds, "seconds") +func stopAfter(stop chan int, exitAfter time.Duration) { + time.AfterFunc(exitAfter, func() { + log.Println("Stopping gor, duration of", exitAfter, "reached") close(stop) }) } diff --git a/settings.go b/settings.go index dd0675c..1b6104e 100644 --- a/settings.go +++ b/settings.go @@ -25,9 +25,10 @@ func (h *MultiOption) Set(value string) error { // AppSettings is the struct of main configuration type AppSettings struct { - verbose bool - debug bool - stats bool + verbose bool + debug bool + stats bool + exitAfter time.Duration splitOutput bool @@ -74,6 +75,7 @@ func init() { flag.BoolVar(&Settings.verbose, "verbose", false, "Turn on more verbose output") flag.BoolVar(&Settings.debug, "debug", false, "Turn on debug output, shows all intercepted traffic. Works only when with `verbose` flag") flag.BoolVar(&Settings.stats, "stats", false, "Turn on queue stats output") + flag.DurationVar(&Settings.exitAfter, "exit-after", 0, "exit after specified duration") flag.BoolVar(&Settings.splitOutput, "split-output", false, "By default each output gets same traffic. If set to `true` it splits traffic equally among all outputs.") From dae6a8915de74aaa08f6d697d20037cdb353f804 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Fri, 8 Jul 2016 19:27:20 +0300 Subject: [PATCH 3/3] Refactor exit-after --- emitter.go | 8 +------- gor.go | 37 ++++++++++++++++++------------------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/emitter.go b/emitter.go index 9575484..1fc2a83 100644 --- a/emitter.go +++ b/emitter.go @@ -32,13 +32,7 @@ func Start(stop chan int) { for { select { case <-stop: - pluginMu.Lock() - for _, p := range Plugins.All { - if cp, ok := p.(io.Closer); ok { - cp.Close() - } - } - pluginMu.Unlock() + finalize() return case <-time.After(100 * time.Millisecond): } diff --git a/gor.go b/gor.go index 70f3fa7..085eb98 100644 --- a/gor.go +++ b/gor.go @@ -78,27 +78,33 @@ func main() { signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c - - for _, p := range Plugins.All { - if cp, ok := p.(io.Closer); ok { - cp.Close() - } - } - + finalize() os.Exit(1) }() - if Settings.exitAfter >= 1 { + if Settings.exitAfter > 0 { log.Println("Running gor for a duration of", Settings.exitAfter) - stop := make(chan int) - stopAfter(stop, Settings.exitAfter) + closeCh := make(chan int) - Start(stop) + time.AfterFunc(Settings.exitAfter, func() { + log.Println("Stopping gor after", Settings.exitAfter) + close(closeCh) + }) + + Start(closeCh) } else { Start(nil) } } +func finalize() { + for _, p := range Plugins.All { + if cp, ok := p.(io.Closer); ok { + cp.Close() + } + } +} + func profileCPU(cpuprofile string) { if cpuprofile != "" { f, err := os.Create(cpuprofile) @@ -126,11 +132,4 @@ func profileMEM(memprofile string) { f.Close() }) } -} - -func stopAfter(stop chan int, exitAfter time.Duration) { - time.AfterFunc(exitAfter, func() { - log.Println("Stopping gor, duration of", exitAfter, "reached") - close(stop) - }) -} +} \ No newline at end of file