From 0d83330ced523f860714c3fc4b200ae9c922d1ec Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 14 Feb 2017 10:13:09 +0100 Subject: [PATCH] condition session history --- proxy/vmess/encoding/server.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/proxy/vmess/encoding/server.go b/proxy/vmess/encoding/server.go index 971fc734..2cbfd625 100644 --- a/proxy/vmess/encoding/server.go +++ b/proxy/vmess/encoding/server.go @@ -17,6 +17,7 @@ import ( "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" "v2ray.com/core/common/serial" + "v2ray.com/core/common/signal" "v2ray.com/core/proxy/vmess" ) @@ -29,19 +30,29 @@ type sessionId struct { type SessionHistory struct { sync.RWMutex cache map[sessionId]time.Time + token *signal.Semaphore + ctx context.Context } func NewSessionHistory(ctx context.Context) *SessionHistory { h := &SessionHistory{ cache: make(map[sessionId]time.Time, 128), + token: signal.NewSemaphore(1), + ctx: ctx, } - go h.run(ctx) return h } func (h *SessionHistory) add(session sessionId) { h.Lock() h.cache[session] = time.Now().Add(time.Minute * 3) + + select { + case <-h.token.Wait(): + go h.run() + default: + } + h.Unlock() } @@ -55,16 +66,22 @@ func (h *SessionHistory) has(session sessionId) bool { return false } -func (h *SessionHistory) run(ctx context.Context) { +func (h *SessionHistory) run() { + defer h.token.Signal() + for { select { - case <-ctx.Done(): + case <-h.ctx.Done(): return case <-time.After(time.Second * 30): } session2Remove := make([]sessionId, 0, 16) now := time.Now() h.Lock() + if len(h.cache) == 0 { + h.Unlock() + return + } for session, expire := range h.cache { if expire.Before(now) { session2Remove = append(session2Remove, session)