Source File
session_cache.go
Belonging Package
github.com/tmc/go-iroh/iroh
package irohimport (tls)// maxTLSTickets is the default upper bound on cached TLS session tickets used// for 0-RTT connection establishment. It matches the Rust iroh constant// DEFAULT_MAX_TLS_TICKETS = 8 * 32 (iroh/src/tls.rs:33): roughly 8 tickets for// each of 32 distinct remote endpoints, an acceptable ~150 KB cache.const maxTLSTickets = 8 * 32// SessionCache stores TLS 1.3 session tickets so a repeat dial to a peer can// resume with 0-RTT early data instead of a fresh handshake. It keeps// at most [maxTLSTickets] entries, evicting the least recently used.//// Entries are bucketed by TLS server name. iroh derives a unique server name// from each peer's endpoint id (see [ServerName]), so tickets for different// peers never collide and resuming always targets the correct identity.//// A SessionCache is safe for concurrent use. The zero value is not usable; call// [NewSessionCache].type SessionCache struct {mu sync.Mutexentries map[string]*list.Element // of *sessionEntrylru list.List // most recently used first}type sessionEntry struct {key stringsession *tls.ClientSessionState}// NewSessionCache returns a [SessionCache] that retains at most [maxTLSTickets]// tickets, evicting the least-recently-used entry when full.func () *SessionCache {return &SessionCache{entries: make(map[string]*list.Element)}}// Get implements [tls.ClientSessionCache]. It returns the cached session for// sessionKey, if any.func ( *SessionCache) ( string) (*tls.ClientSessionState, bool) {.mu.Lock()defer .mu.Unlock(), := .entries[]if ! {return nil, false}.lru.MoveToFront()return .Value.(*sessionEntry).session, true}// Put implements [tls.ClientSessionCache]. The TLS stack calls it when a server// issues a session ticket. A nil session removes the entry, matching the// [tls.ClientSessionCache] contract.func ( *SessionCache) ( string, *tls.ClientSessionState) {.mu.Lock()defer .mu.Unlock()if , := .entries[]; {if == nil {.lru.Remove()delete(.entries, )return}.Value.(*sessionEntry).session =.lru.MoveToFront()return}if == nil {return}if .lru.Len() >= maxTLSTickets {:= .lru.Back().lru.Remove()delete(.entries, .Value.(*sessionEntry).key)}.entries[] = .lru.PushFront(&sessionEntry{, })}// Len reports the number of server names that hold a ticket.// It exists for tests and diagnostics.func ( *SessionCache) () int {.mu.Lock()defer .mu.Unlock()return len(.entries)}
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |