2024-10-23 16:44:35 +01:00
|
|
|
package udpproxy
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
// sessionCache tracks connection sessions
|
|
|
|
type sessionCache struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
data map[string]*session
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSessionCache creates a usable sessionCache.
|
|
|
|
func newSessionCache() sessionCache {
|
|
|
|
return sessionCache{
|
|
|
|
data: make(map[string]*session),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 17:22:03 +01:00
|
|
|
// read returns the associated session for an addr
|
|
|
|
func (sc *sessionCache) read(addr string) (*session, bool) {
|
2024-10-23 16:44:35 +01:00
|
|
|
sc.mu.RLock()
|
|
|
|
defer sc.mu.RUnlock()
|
|
|
|
|
|
|
|
v, ok := sc.data[addr]
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2024-10-23 17:22:03 +01:00
|
|
|
// insert adds a session for a given addr.
|
|
|
|
func (sc *sessionCache) insert(addr string, session *session) {
|
2024-10-23 16:44:35 +01:00
|
|
|
sc.mu.Lock()
|
|
|
|
defer sc.mu.Unlock()
|
|
|
|
|
|
|
|
sc.data[addr] = session
|
|
|
|
}
|
|
|
|
|
2024-10-23 17:22:03 +01:00
|
|
|
// delete removes the session for the given addr.
|
|
|
|
func (sc *sessionCache) delete(addr string) {
|
2024-10-23 16:44:35 +01:00
|
|
|
sc.mu.Lock()
|
|
|
|
defer sc.mu.Unlock()
|
|
|
|
|
|
|
|
delete(sc.data, addr)
|
|
|
|
}
|