session.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package mcpproxy
  2. import "sync"
  3. // SessionManager defines the interface for managing session information
  4. type SessionManager interface {
  5. // Set stores a sessionID and its corresponding backend endpoint
  6. Set(sessionID, endpoint string)
  7. // Get retrieves the backend endpoint for a sessionID
  8. Get(sessionID string) (string, bool)
  9. // Delete removes a sessionID from the store
  10. Delete(sessionID string)
  11. }
  12. // MemStore implements the SessionManager interface
  13. type MemStore struct {
  14. mu sync.RWMutex
  15. sessions map[string]string // sessionID -> host+endpoint
  16. }
  17. // NewMemStore creates a new session store
  18. func NewMemStore() *MemStore {
  19. return &MemStore{
  20. sessions: make(map[string]string),
  21. }
  22. }
  23. // Set stores a sessionID and its corresponding backend endpoint
  24. func (s *MemStore) Set(sessionID, endpoint string) {
  25. s.mu.Lock()
  26. defer s.mu.Unlock()
  27. s.sessions[sessionID] = endpoint
  28. }
  29. // Get retrieves the backend endpoint for a sessionID
  30. func (s *MemStore) Get(sessionID string) (string, bool) {
  31. s.mu.RLock()
  32. defer s.mu.RUnlock()
  33. endpoint, ok := s.sessions[sessionID]
  34. return endpoint, ok
  35. }
  36. // Delete removes a sessionID from the store
  37. func (s *MemStore) Delete(sessionID string) {
  38. s.mu.Lock()
  39. defer s.mu.Unlock()
  40. delete(s.sessions, sessionID)
  41. }