From 3f216f12f838f067454c0430326c0c40c468656e Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 10 Nov 2018 18:21:42 +0100 Subject: [PATCH] USR1 on circologd pause/resume --- cmd/circologd/main.go | 8 ++++++-- hub.go | 44 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/cmd/circologd/main.go b/cmd/circologd/main.go index d3dfa10..aed1266 100644 --- a/cmd/circologd/main.go +++ b/cmd/circologd/main.go @@ -31,7 +31,7 @@ func main() { flag.Parse() interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM) + signal.Notify(interrupt, syscall.SIGINT, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGTERM) hub := circolog.NewHub(*bufsize) handler := syslog.NewChannelHandler(hub.LogMessages) @@ -90,7 +90,11 @@ func main() { case sig := <-interrupt: if sig == syscall.SIGHUP { log.Println("Clearing queue") - hub.Clear() + hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear} + } + if sig == syscall.SIGUSR1 { + log.Println("Pause/resume") + hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle} } if sig == syscall.SIGTERM || sig == syscall.SIGINT { log.Println("Quitting because of signal", sig) diff --git a/hub.go b/hub.go index eceb3be..f4c746b 100644 --- a/hub.go +++ b/hub.go @@ -24,10 +24,25 @@ type ClientOptions struct { // The channel "register" and "unregister" can be seen as "command" // keep in mind that "registering" is what you do also to get messages in a one-time fashion. In fact, Client // has "options", such as Nofollow, to explain the Hub what should be given + +// An HubCommand is an "enum" of different commands +type HubCommand int + +const ( + CommandClear = iota + CommandPauseToggle = iota +) + +// An HubFullCommand is a Command, complete with arguments +type HubFullCommand struct { + Command HubCommand +} + type Hub struct { Register chan Client Unregister chan Client LogMessages chan format.LogParts + Commands chan HubFullCommand clients map[Client]bool circbuf *ring.Ring @@ -39,6 +54,7 @@ func NewHub(ringBufSize int) Hub { Register: make(chan Client), Unregister: make(chan Client), LogMessages: make(chan format.LogParts), + Commands: make(chan HubFullCommand), circbuf: ring.New(ringBufSize), } } @@ -77,6 +93,7 @@ func (h *Hub) register(cl Client) { // Run is hub main loop; keeps everything going func (h *Hub) Run() { + active := true for { select { case cl := <-h.Register: @@ -88,22 +105,31 @@ func (h *Hub) Run() { delete(h.clients, cl) } case msg := <-h.LogMessages: - h.circbuf.Value = msg - h.circbuf = h.circbuf.Next() - for client := range h.clients { - select { // send without blocking - case client.Messages <- msg: - break - default: - break + if active == true { + h.circbuf.Value = msg + h.circbuf = h.circbuf.Next() + for client := range h.clients { + select { // send without blocking + case client.Messages <- msg: + break + default: + break + } } } + case cmd := <-h.Commands: + if cmd.Command == CommandClear { + h.clear() + } + if cmd.Command == CommandPauseToggle { + active = !active + } } } } // Clear removes every all elements from the buffer -func (h *Hub) Clear() { +func (h *Hub) clear() { buf := h.circbuf for i := 0; i < buf.Len(); i++ { buf.Value = nil