Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f55803c

Browse files
committedFeb 16, 2016
add command to change keep time for reqlog objects
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 6b76e2a commit f55803c

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed
 

‎commands/reqlog.go

+19-24
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type ReqLog struct {
4444
Requests []*ReqLogEntry
4545
nextID int
4646
lock sync.Mutex
47+
keep time.Duration
4748
}
4849

4950
func (rl *ReqLog) Add(req Request) *ReqLogEntry {
@@ -69,43 +70,37 @@ func (rl *ReqLog) Add(req Request) *ReqLogEntry {
6970
func (rl *ReqLog) ClearInactive() {
7071
rl.lock.Lock()
7172
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
8077
}
8178

8279
func (rl *ReqLog) maybeCleanup() {
8380
// only do it every so often or it might
8481
// become a perf issue
85-
if len(rl.Requests) == 0 {
82+
if len(rl.Requests)%10 == 0 {
8683
rl.cleanup()
8784
}
8885
}
8986

9087
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]
10594
i++
10695
}
107-
rl.Requests = rl.Requests[:len(rl.Requests)-i]
10896
}
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
109104
}
110105

111106
// Report generates a copy of all the entries in the requestlog

‎core/commands/active.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Lists running and recently run commands.
2525
cmds.BoolOption("v", "verbose", "print more verbose output"),
2626
},
2727
Subcommands: map[string]*cmds.Command{
28-
"clear": clearInactiveCmd,
28+
"clear": clearInactiveCmd,
29+
"set-time": setRequestClearCmd,
2930
},
3031
Marshalers: map[cmds.EncodingType]cmds.Marshaler{
3132
cmds.Text: func(res cmds.Response) (io.Reader, error) {
@@ -92,3 +93,21 @@ var clearInactiveCmd = &cmds.Command{
9293
req.InvocContext().ReqLog.ClearInactive()
9394
},
9495
}
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.