simple clear

This commit is contained in:
Fangliding
2026-10-18 22:30:54 +08:00
parent 0396b34eff
commit 8d0deebf86
3 changed files with 4 additions and 21 deletions
+1 -6
View File
@@ -397,16 +397,10 @@ type serverSessionState struct {
} }
func (st *serverSessionState) check(packetID uint64) bool { func (st *serverSessionState) check(packetID uint64) bool {
if st.window == nil {
st.window = new(SlidingWindow)
}
return st.window.Check(packetID) return st.window.Check(packetID)
} }
func (st *serverSessionState) add(packetID uint64) { func (st *serverSessionState) add(packetID uint64) {
if st.window == nil {
st.window = new(SlidingWindow)
}
st.window.Add(packetID) st.window.Add(packetID)
} }
@@ -479,6 +473,7 @@ func (s *ClientUDPSession) getServerSession(sessionID uint64, now int64) (*serve
newState := &serverSessionState{ newState := &serverSessionState{
sessionID: sessionID, sessionID: sessionID,
window: new(SlidingWindow),
cipher: bodyAead, cipher: bodyAead,
} }
newState.lastSeen.Store(now) newState.lastSeen.Store(now)
+1 -12
View File
@@ -100,21 +100,12 @@ type ServerUDPSession struct {
func (s *ServerUDPSession) CheckPacketID(packetID uint64) bool { func (s *ServerUDPSession) CheckPacketID(packetID uint64) bool {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
if s.Window == nil {
s.Window = new(SlidingWindow)
}
return s.Window.Check(packetID) return s.Window.Check(packetID)
} }
// CommitPacketID records a packet ID after the packet has been fully validated.
// It re-checks the window under the lock, so when the same packet is validated
// concurrently (e.g. by the workers of two client source addresses), only one is accepted.
func (s *ServerUDPSession) CommitPacketID(packetID uint64) bool { func (s *ServerUDPSession) CommitPacketID(packetID uint64) bool {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
if s.Window == nil {
s.Window = new(SlidingWindow)
}
return s.Window.CheckAndAdd(packetID) return s.Window.CheckAndAdd(packetID)
} }
@@ -127,9 +118,6 @@ func (s *ServerUDPSession) getClientBodyCipher() cipher.AEAD {
func (s *ServerUDPSession) commitClientPacket(packetID uint64, bodyAead cipher.AEAD) bool { func (s *ServerUDPSession) commitClientPacket(packetID uint64, bodyAead cipher.AEAD) bool {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
if s.Window == nil {
s.Window = new(SlidingWindow)
}
if !s.Window.CheckAndAdd(packetID) { if !s.Window.CheckAndAdd(packetID) {
return false return false
} }
@@ -161,6 +149,7 @@ func (m *UDPSessionManager) GetOrCreate(sessionID uint64) *ServerUDPSession {
s := &ServerUDPSession{ s := &ServerUDPSession{
SessionID: sessionID, SessionID: sessionID,
Window: new(SlidingWindow),
manager: m, manager: m,
} }
s.LastActive.Store(now) s.LastActive.Store(now)
+2 -3
View File
@@ -63,9 +63,8 @@ func (s *ServerUDPSession) EnsureLink(
s.currentConn.Store(conn) s.currentConn.Store(conn)
// The session is routed by client session ID and may outlive the inbound // UDP Session might travel across multiple connections
// connection of the source address it started from (SIP022 §3.2.4), so its // don't bind its lifetime to a parent context
// lifetime is governed by its own timer rather than by that connection.
sessCtx, cancel := context.WithCancel(context.WithoutCancel(ctx)) sessCtx, cancel := context.WithCancel(context.WithoutCancel(ctx))
inbound := session.InboundFromContext(sessCtx) inbound := session.InboundFromContext(sessCtx)
if inbound != nil && s.User != nil { if inbound != nil && s.User != nil {