github.com/MerlinKodo/quic-go@v0.39.2/internal/qtls/client_session_cache.go (about)

     1  //go:build go1.21
     2  
     3  package qtls
     4  
     5  import (
     6  	"crypto/tls"
     7  )
     8  
     9  type clientSessionCache struct {
    10  	getData func() []byte
    11  	setData func([]byte)
    12  	wrapped tls.ClientSessionCache
    13  }
    14  
    15  var _ tls.ClientSessionCache = &clientSessionCache{}
    16  
    17  func (c clientSessionCache) Put(key string, cs *tls.ClientSessionState) {
    18  	if cs == nil {
    19  		c.wrapped.Put(key, nil)
    20  		return
    21  	}
    22  	ticket, state, err := cs.ResumptionState()
    23  	if err != nil || state == nil {
    24  		c.wrapped.Put(key, cs)
    25  		return
    26  	}
    27  	state.Extra = append(state.Extra, addExtraPrefix(c.getData()))
    28  	newCS, err := tls.NewResumptionState(ticket, state)
    29  	if err != nil {
    30  		// It's not clear why this would error. Just save the original state.
    31  		c.wrapped.Put(key, cs)
    32  		return
    33  	}
    34  	c.wrapped.Put(key, newCS)
    35  }
    36  
    37  func (c clientSessionCache) Get(key string) (*tls.ClientSessionState, bool) {
    38  	cs, ok := c.wrapped.Get(key)
    39  	if !ok || cs == nil {
    40  		return cs, ok
    41  	}
    42  	ticket, state, err := cs.ResumptionState()
    43  	if err != nil {
    44  		// It's not clear why this would error.
    45  		// Remove the ticket from the session cache, so we don't run into this error over and over again
    46  		c.wrapped.Put(key, nil)
    47  		return nil, false
    48  	}
    49  	// restore QUIC transport parameters and RTT stored in state.Extra
    50  	if extra := findExtraData(state.Extra); extra != nil {
    51  		c.setData(extra)
    52  	}
    53  	session, err := tls.NewResumptionState(ticket, state)
    54  	if err != nil {
    55  		// It's not clear why this would error.
    56  		// Remove the ticket from the session cache, so we don't run into this error over and over again
    57  		c.wrapped.Put(key, nil)
    58  		return nil, false
    59  	}
    60  	return session, true
    61  }