提交 1e1c89c2 作者: Jeromy

command to clear inactive requests from request log

License: MIT
Signed-off-by: 's avatarJeromy <jeromyj@gmail.com>
上级 e8c753d2
...@@ -66,6 +66,19 @@ func (rl *ReqLog) Add(req Request) *ReqLogEntry { ...@@ -66,6 +66,19 @@ func (rl *ReqLog) Add(req Request) *ReqLogEntry {
return rle return rle
} }
func (rl *ReqLog) ClearInactive() {
rl.lock.Lock()
defer rl.lock.Unlock()
i := 0
for j := 0; j < len(rl.Requests); j++ {
if rl.Requests[j].Active {
rl.Requests[i] = rl.Requests[j]
i++
}
}
rl.Requests = rl.Requests[:i]
}
func (rl *ReqLog) maybeCleanup() { func (rl *ReqLog) maybeCleanup() {
// only do it every so often or it might // only do it every so often or it might
// become a perf issue // become a perf issue
...@@ -79,7 +92,7 @@ func (rl *ReqLog) cleanup() { ...@@ -79,7 +92,7 @@ func (rl *ReqLog) cleanup() {
// drop all logs at are inactive and more than an hour old // drop all logs at are inactive and more than an hour old
for ; i < len(rl.Requests); i++ { for ; i < len(rl.Requests); i++ {
req := rl.Requests[i] req := rl.Requests[i]
if req.Active || req.EndTime.Add(time.Hour).After(time.Now()) { if req.Active || req.EndTime.Add(time.Hour/2).After(time.Now()) {
break break
} }
} }
......
...@@ -24,6 +24,9 @@ Lists running and recently run commands. ...@@ -24,6 +24,9 @@ Lists running and recently run commands.
Options: []cmds.Option{ Options: []cmds.Option{
cmds.BoolOption("v", "verbose", "print more verbose output"), cmds.BoolOption("v", "verbose", "print more verbose output"),
}, },
Subcommands: map[string]*cmds.Command{
"clear": clearInactiveCmd,
},
Marshalers: map[cmds.EncodingType]cmds.Marshaler{ Marshalers: map[cmds.EncodingType]cmds.Marshaler{
cmds.Text: func(res cmds.Response) (io.Reader, error) { cmds.Text: func(res cmds.Response) (io.Reader, error) {
out, ok := res.Output().(*[]*cmds.ReqLogEntry) out, ok := res.Output().(*[]*cmds.ReqLogEntry)
...@@ -80,3 +83,12 @@ Lists running and recently run commands. ...@@ -80,3 +83,12 @@ Lists running and recently run commands.
}, },
Type: []*cmds.ReqLogEntry{}, Type: []*cmds.ReqLogEntry{},
} }
var clearInactiveCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Clear inactive requests from the log",
},
Run: func(req cmds.Request, res cmds.Response) {
req.InvocContext().ReqLog.ClearInactive()
},
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论