瀏覽代碼

optimize map usage

Darien Raymond 7 年之前
父節點
當前提交
4a46817cf6
共有 2 個文件被更改,包括 6 次插入6 次删除
  1. 2 6
      app/router/condition.go
  2. 4 0
      proxy/vmess/encoding/server.go

+ 2 - 6
app/router/condition.go

@@ -164,18 +164,14 @@ func (m *CachableDomainMatcher) ApplyDomain(domain string) bool {
 
 	now := time.Now()
 	if len(m.cache) > 256 && now.Sub(m.lastScan)/time.Second > 5 {
-		remove := make([]string, 0, 128)
-
 		now := time.Now()
 
 		for k, v := range m.cache {
 			if now.Sub(v.timestamp)/time.Second > 60 {
-				remove = append(remove, k)
+				delete(m.cache, k)
 			}
 		}
-		for _, v := range remove {
-			delete(m.cache, v)
-		}
+
 		m.lastScan = now
 	}
 

+ 4 - 0
proxy/vmess/encoding/server.go

@@ -75,6 +75,10 @@ func (h *SessionHistory) removeExpiredEntries() {
 	h.Lock()
 	defer h.Unlock()
 
+	if len(h.cache) == 0 {
+		return
+	}
+
 	for session, expire := range h.cache {
 		if expire.Before(now) {
 			delete(h.cache, session)