浏览代码

expire old updates

raylu 14 年之前
父节点
当前提交
c37572b1dc
共有 1 个文件被更改,包括 10 次插入0 次删除
  1. 10 0
      updates.go

+ 10 - 0
updates.go

@@ -42,6 +42,7 @@ func addUpdate(pid int, action uint, song *Song) {
 	updateLock.Lock()
 	defer updateLock.Unlock()
 
+	// write new update
 	pup := tailUpdates[pid]
 	if pup != nil {
 		pup.Next = update
@@ -50,6 +51,15 @@ func addUpdate(pid int, action uint, song *Song) {
 	}
 	tailUpdates[pid] = update
 
+	// expire old updates
+	const expiryTime = 1e9 * 3 * 60 // 3 minutes
+	pup = headUpdates[pid]
+	for pup != nil && pup.Timestamp < update.Timestamp - expiryTime {
+		pup = pup.Next
+		headUpdates[pid] = pup
+	}
+
+	// notify listeners
 	listener := listeners[pid]
 	for listener != nil {
 		listener.L <- true