...

Source file src/edge-infra.dev/pkg/f8n/devinfra/ghappman/session.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/ghappman

     1  package ghappman
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  type sessionStore struct {
     9  	sync.Mutex
    10  	vals    map[string]*session
    11  	expires map[string]*time.Timer
    12  	timeout time.Duration
    13  }
    14  
    15  type session struct {
    16  	KeyHash []byte
    17  	App     *AppConfig
    18  }
    19  
    20  func newSessionStore(timeout time.Duration) *sessionStore {
    21  	return &sessionStore{
    22  		vals:    make(map[string]*session),
    23  		expires: make(map[string]*time.Timer),
    24  		timeout: timeout,
    25  	}
    26  }
    27  
    28  func (s *sessionStore) startSession(key string, hash []byte) {
    29  	s.Lock()
    30  	s.vals[key] = &session{
    31  		KeyHash: hash,
    32  	}
    33  	t := time.AfterFunc(s.timeout, func() {
    34  		s.endSession(key)
    35  	})
    36  	s.expires[key] = t
    37  	s.Unlock()
    38  }
    39  
    40  func (s *sessionStore) updateSession(key string, app *AppConfig) {
    41  	s.Lock()
    42  	s.vals[key].App = app
    43  	s.Unlock()
    44  }
    45  
    46  func (s *sessionStore) endSession(key string) {
    47  	s.Lock()
    48  	if t := s.expires[key]; t != nil {
    49  		// if the session timed out, t.Stop() will be a nop because
    50  		// the timeout timer already reached its end. repeated calls to endSession
    51  		// with the same key or non-existent key will produce a nil timer
    52  		_ = t.Stop()
    53  	}
    54  	delete(s.expires, key)
    55  	delete(s.vals, key)
    56  	s.Unlock()
    57  }
    58  
    59  func (s *sessionStore) getSession(key string) *session {
    60  	s.Lock()
    61  	defer s.Unlock()
    62  	return s.vals[key]
    63  }
    64  
    65  func (s *sessionStore) hasSession(key string) bool {
    66  	s.Lock()
    67  	defer s.Unlock()
    68  	return s.vals[key] != nil
    69  }
    70  

View as plain text