From e664fc3a2d1cd3019577688daca19244b65af069 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 15 Feb 2016 16:26:33 -0800 Subject: [PATCH] add command to change keep time for reqlog objects License: MIT Signed-off-by: Jeromy --- commands/reqlog.go | 51 +++++++++++++++++++---------------------- core/commands/active.go | 21 ++++++++++++++++- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/commands/reqlog.go b/commands/reqlog.go index 8cf81dfa3..08701a8d5 100644 --- a/commands/reqlog.go +++ b/commands/reqlog.go @@ -44,6 +44,7 @@ type ReqLog struct { Requests []*ReqLogEntry nextID int lock sync.Mutex + keep time.Duration } func (rl *ReqLog) Add(req Request) *ReqLogEntry { @@ -69,9 +70,26 @@ func (rl *ReqLog) Add(req Request) *ReqLogEntry { func (rl *ReqLog) ClearInactive() { rl.lock.Lock() defer rl.lock.Unlock() + k := rl.keep + rl.keep = 0 + rl.cleanup() + rl.keep = k +} + +func (rl *ReqLog) maybeCleanup() { + // only do it every so often or it might + // become a perf issue + if len(rl.Requests)%10 == 0 { + rl.cleanup() + } +} + +func (rl *ReqLog) cleanup() { i := 0 + now := time.Now() for j := 0; j < len(rl.Requests); j++ { - if rl.Requests[j].Active { + rj := rl.Requests[j] + if rj.Active || rl.Requests[j].EndTime.Add(rl.keep).After(now) { rl.Requests[i] = rl.Requests[j] i++ } @@ -79,33 +97,10 @@ func (rl *ReqLog) ClearInactive() { rl.Requests = rl.Requests[:i] } -func (rl *ReqLog) maybeCleanup() { - // only do it every so often or it might - // become a perf issue - if len(rl.Requests) == 0 { - rl.cleanup() - } -} - -func (rl *ReqLog) cleanup() { - var i int - // drop all logs at are inactive and more than an hour old - for ; i < len(rl.Requests); i++ { - req := rl.Requests[i] - if req.Active || req.EndTime.Add(time.Hour/2).After(time.Now()) { - break - } - } - - if i > 0 { - var j int - for i < len(rl.Requests) { - rl.Requests[j] = rl.Requests[i] - j++ - i++ - } - rl.Requests = rl.Requests[:len(rl.Requests)-i] - } +func (rl *ReqLog) SetKeepTime(t time.Duration) { + rl.lock.Lock() + defer rl.lock.Unlock() + rl.keep = t } // Report generates a copy of all the entries in the requestlog diff --git a/core/commands/active.go b/core/commands/active.go index 5fcca36d8..8f6876b62 100644 --- a/core/commands/active.go +++ b/core/commands/active.go @@ -25,7 +25,8 @@ Lists running and recently run commands. cmds.BoolOption("v", "verbose", "print more verbose output"), }, Subcommands: map[string]*cmds.Command{ - "clear": clearInactiveCmd, + "clear": clearInactiveCmd, + "set-time": setRequestClearCmd, }, Marshalers: map[cmds.EncodingType]cmds.Marshaler{ cmds.Text: func(res cmds.Response) (io.Reader, error) { @@ -92,3 +93,21 @@ var clearInactiveCmd = &cmds.Command{ req.InvocContext().ReqLog.ClearInactive() }, } + +var setRequestClearCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Set how long to keep inactive requests in the log", + }, + Arguments: []cmds.Argument{ + cmds.StringArg("time", true, false, "time to keep inactive requests in log"), + }, + Run: func(req cmds.Request, res cmds.Response) { + tval, err := time.ParseDuration(req.Arguments()[0]) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + req.InvocContext().ReqLog.SetKeepTime(tval) + }, +}