cleanup session history

pull/861/head
Darien Raymond 2018-02-07 16:35:22 +01:00
parent 8b83bf2283
commit a1ae4aa515
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 19 additions and 31 deletions

View File

@ -46,7 +46,7 @@ func TestRequestSerialization(t *testing.T) {
buffer2.Append(buffer.Bytes()) buffer2.Append(buffer.Bytes())
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
sessionHistory := NewSessionHistory(ctx) sessionHistory := NewSessionHistory()
userValidator := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash) userValidator := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash)
userValidator.Add(user) userValidator.Add(user)

View File

@ -1,7 +1,6 @@
package encoding package encoding
import ( import (
"context"
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/md5" "crypto/md5"
@ -32,26 +31,25 @@ type SessionHistory struct {
sync.RWMutex sync.RWMutex
cache map[sessionId]time.Time cache map[sessionId]time.Time
token *signal.Semaphore token *signal.Semaphore
ctx context.Context timer *time.Timer
} }
func NewSessionHistory(ctx context.Context) *SessionHistory { func NewSessionHistory() *SessionHistory {
h := &SessionHistory{ h := &SessionHistory{
cache: make(map[sessionId]time.Time, 128), cache: make(map[sessionId]time.Time, 128),
token: signal.NewSemaphore(1), token: signal.NewSemaphore(1),
ctx: ctx,
} }
return h return h
} }
func (h *SessionHistory) add(session sessionId) { func (h *SessionHistory) add(session sessionId) {
h.Lock() h.Lock()
h.cache[session] = time.Now().Add(time.Minute * 3) defer h.Unlock()
h.Unlock()
h.cache[session] = time.Now().Add(time.Minute * 3)
select { select {
case <-h.token.Wait(): case <-h.token.Wait():
go h.run() h.timer = time.AfterFunc(time.Minute*3, h.removeExpiredEntries)
default: default:
} }
} }
@ -66,31 +64,21 @@ func (h *SessionHistory) has(session sessionId) bool {
return false return false
} }
func (h *SessionHistory) run() { func (h *SessionHistory) removeExpiredEntries() {
defer h.token.Signal() now := time.Now()
for { h.Lock()
select { defer h.Unlock()
case <-h.ctx.Done():
return for session, expire := range h.cache {
case <-time.After(time.Second * 30): if expire.Before(now) {
}
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)
}
}
for _, session := range session2Remove {
delete(h.cache, session) delete(h.cache, session)
} }
h.Unlock() }
if h.timer != nil {
h.timer.Stop()
h.timer = nil
} }
} }

View File

@ -93,7 +93,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
clients: vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash), clients: vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash),
detours: config.Detour, detours: config.Detour,
usersByEmail: newUserByEmail(config.User, config.GetDefaultValue()), usersByEmail: newUserByEmail(config.User, config.GetDefaultValue()),
sessionHistory: encoding.NewSessionHistory(ctx), sessionHistory: encoding.NewSessionHistory(),
} }
for _, user := range config.User { for _, user := range config.User {