dont start periodic task until necessary

This commit is contained in:
Darien Raymond
2018-08-29 23:00:01 +02:00
parent 5a0a9aa65e
commit eb05a92592
10 changed files with 81 additions and 162 deletions
+8 -8
View File
@@ -42,12 +42,8 @@ func NewSessionHistory() *SessionHistory {
}
h.task = &task.Periodic{
Interval: time.Second * 30,
Execute: func() error {
h.removeExpiredEntries()
return nil
},
Execute: h.removeExpiredEntries,
}
common.Must(h.task.Start())
return h
}
@@ -58,24 +54,26 @@ func (h *SessionHistory) Close() error {
func (h *SessionHistory) addIfNotExits(session sessionId) bool {
h.Lock()
defer h.Unlock()
if expire, found := h.cache[session]; found && expire.After(time.Now()) {
h.Unlock()
return false
}
h.cache[session] = time.Now().Add(time.Minute * 3)
h.Unlock()
common.Must(h.task.Start())
return true
}
func (h *SessionHistory) removeExpiredEntries() {
func (h *SessionHistory) removeExpiredEntries() error {
now := time.Now()
h.Lock()
defer h.Unlock()
if len(h.cache) == 0 {
return
return newError("nothing to do")
}
for session, expire := range h.cache {
@@ -87,6 +85,8 @@ func (h *SessionHistory) removeExpiredEntries() {
if len(h.cache) == 0 {
h.cache = make(map[sessionId]time.Time, 128)
}
return nil
}
// ServerSession keeps information for a session in VMess server.