add command to change keep time for reqlog objects
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Feb 15, 2016 at 16:26 UTC
e664fc3a2d1cd3019577688daca19244b65af069
2 files changed
+39
-25
commands/reqlog.go
+19
-24
@@ -44,6 +44,7 @@ type ReqLog struct {
44
Requests []*ReqLogEntry
45
nextID int
46
lock sync.Mutex
47
+ keep time.Duration
48
}
49
50
func (rl *ReqLog) Add(req Request) *ReqLogEntry {
@@ -69,43 +70,37 @@ func (rl *ReqLog) Add(req Request) *ReqLogEntry {
70
func (rl *ReqLog) ClearInactive() {
71
rl.lock.Lock()
72
defer rl.lock.Unlock()
72
- i := 0
73
- for j := 0; j < len(rl.Requests); j++ {
74
- if rl.Requests[j].Active {
75
- rl.Requests[i] = rl.Requests[j]
76
- i++
77
- }
78
- }
79
- rl.Requests = rl.Requests[:i]
73
+ k := rl.keep
74
+ rl.keep = 0
75
+ rl.cleanup()
76
+ rl.keep = k
77
}
78
79
func (rl *ReqLog) maybeCleanup() {
80
// only do it every so often or it might
81
// become a perf issue
85
- if len(rl.Requests) == 0 {
82
+ if len(rl.Requests)%10 == 0 {
83
rl.cleanup()
84
}
85
}
86
87
func (rl *ReqLog) cleanup() {
91
- var i int
92
- // drop all logs at are inactive and more than an hour old
93
- for ; i < len(rl.Requests); i++ {
94
- req := rl.Requests[i]
95
- if req.Active || req.EndTime.Add(time.Hour/2).After(time.Now()) {
96
- break
97
- }
98
- }
99
-
100
- if i > 0 {
101
- var j int
102
- for i < len(rl.Requests) {
103
- rl.Requests[j] = rl.Requests[i]
104
- j++
88
+ i := 0
89
+ now := time.Now()
90
+ for j := 0; j < len(rl.Requests); j++ {
91
+ rj := rl.Requests[j]
92
+ if rj.Active || rl.Requests[j].EndTime.Add(rl.keep).After(now) {
93
+ rl.Requests[i] = rl.Requests[j]
94
i++
95
}
107
- rl.Requests = rl.Requests[:len(rl.Requests)-i]
96
}
97
+ rl.Requests = rl.Requests[:i]
98
+}
99
+
100
+func (rl *ReqLog) SetKeepTime(t time.Duration) {
101
+ rl.lock.Lock()
102
+ defer rl.lock.Unlock()
103
+ rl.keep = t
104
}
105
106
// Report generates a copy of all the entries in the requestlog
core/commands/active.go
+20
-1
@@ -25,7 +25,8 @@ Lists running and recently run commands.
25
cmds.BoolOption("v", "verbose", "print more verbose output"),
26
},
27
Subcommands: map[string]*cmds.Command{
28
- "clear": clearInactiveCmd,
28
+ "clear": clearInactiveCmd,
29
+ "set-time": setRequestClearCmd,
30
},
31
Marshalers: map[cmds.EncodingType]cmds.Marshaler{
32
cmds.Text: func(res cmds.Response) (io.Reader, error) {
@@ -92,3 +93,21 @@ var clearInactiveCmd = &cmds.Command{
93
req.InvocContext().ReqLog.ClearInactive()
94
},
95
}
96
+
97
+var setRequestClearCmd = &cmds.Command{
98
+ Helptext: cmds.HelpText{
99
+ Tagline: "Set how long to keep inactive requests in the log",
100
+ },
101
+ Arguments: []cmds.Argument{
102
+ cmds.StringArg("time", true, false, "time to keep inactive requests in log"),
103
+ },
104
+ Run: func(req cmds.Request, res cmds.Response) {
105
+ tval, err := time.ParseDuration(req.Arguments()[0])
106
+ if err != nil {
107
+ res.SetError(err, cmds.ErrNormal)
108
+ return
109
+ }
110
+
111
+ req.InvocContext().ReqLog.SetKeepTime(tval)
112
+ },
113
+}