Source File
manager.go
Belonging Package
github.com/redis/go-redis/v9/internal/auth/streaming
package streamingimport ()// Manager coordinates streaming credentials and re-authentication for a connection pool.//// The manager is responsible for:// - Creating and managing per-connection credentials listeners// - Providing the pool hook for re-authentication// - Coordinating between credentials updates and pool operations//// When credentials change via a StreamingCredentialsProvider:// 1. The credentials listener (ConnReAuthCredentialsListener) receives the update// 2. It calls MarkForReAuth on the manager// 3. The manager delegates to the pool hook// 4. The pool hook schedules background re-authentication//// The manager maintains a registry of credentials listeners indexed by connection ID,// allowing listener reuse when connections are reinitialized (e.g., after handoff).type Manager struct {// credentialsListeners maps connection ID to credentials listenercredentialsListeners *CredentialsListeners// pool is the connection pool being managedpool pool.Pooler// poolHookRef is the re-authentication pool hookpoolHookRef *ReAuthPoolHook}// NewManager creates a new streaming credentials manager.//// Parameters:// - pl: The connection pool to manage// - reAuthTimeout: Maximum time to wait for acquiring a connection for re-authentication//// The manager creates a ReAuthPoolHook sized to match the pool size, ensuring that// re-auth operations don't exhaust the connection pool.func ( pool.Pooler, time.Duration) *Manager {return NewManagerWithWorkers(, , .Size())}// NewManagerWithWorkers is like NewManager but sizes the re-auth worker// semaphore explicitly. Use it when the returned PoolHook is shared across more// than one pool (e.g. the main pool plus the dedicated pipeline pool): the// semaphore must cover the COMBINED connection ceiling, or after a credential// rotation the extra pool's connections queue behind pl.Size() workers and wait// outside reAuthTimeout.func ( pool.Pooler, time.Duration, int) *Manager {if < 1 {= 1}:= &Manager{pool: ,poolHookRef: NewReAuthPoolHook(, ),credentialsListeners: NewCredentialsListeners(),}.poolHookRef.manager =return}// PoolHook returns the pool hook for re-authentication.//// This hook should be registered with the connection pool to enable// automatic re-authentication when credentials change.func ( *Manager) () pool.PoolHook {return .poolHookRef}// Listener returns or creates a credentials listener for a connection.//// This method is called during connection initialization to set up the// credentials listener. If a listener already exists for the connection ID// (e.g., after a handoff), it is reused.//// Parameters:// - poolCn: The connection to create/get a listener for// - reAuth: Function to re-authenticate the connection with new credentials// - onErr: Function to call when re-authentication fails//// Returns:// - auth.CredentialsListener: The listener to subscribe to the credentials provider// - error: Non-nil if poolCn is nil//// Note: The reAuth and onErr callbacks are captured once when the listener is// created and reused for the connection's lifetime. They should not change.//// Thread-safe: Can be called concurrently during connection initialization.func ( *Manager) (*pool.Conn,func(*pool.Conn, auth.Credentials) error,func(*pool.Conn, error),) (auth.CredentialsListener, error) {if == nil {return nil, errors.New("poolCn cannot be nil")}:= .GetID()// if we reconnect the underlying network connection, the streaming credentials listener will continue to work// so we can get the old listener from the cache and use it.// subscribing the same (an already subscribed) listener for a StreamingCredentialsProvider SHOULD be a no-op, := .credentialsListeners.Get()if ! || == nil {// Create new listener for this connection// Note: Callbacks (reAuth, onErr) are captured once and reused for the connection's lifetime:= &ConnReAuthCredentialsListener{conn: ,reAuth: ,onErr: ,manager: ,}.credentialsListeners.Add(, )=}return , nil}// MarkForReAuth marks a connection for re-authentication.//// This method is called by the credentials listener when new credentials are// received. It delegates to the pool hook to schedule background re-authentication.//// Parameters:// - poolCn: The connection to re-authenticate// - reAuthFn: Function to call for re-authentication, receives error if acquisition fails//// Thread-safe: Called by credentials listeners when credentials change.func ( *Manager) ( *pool.Conn, func(error)) {:= .GetID().poolHookRef.MarkForReAuth(, )}// RemoveListener removes the credentials listener for a connection.//// This method is called by the pool hook's OnRemove to clean up listeners// when connections are removed from the pool.//// Parameters:// - connID: The connection ID whose listener should be removed//// Thread-safe: Called during connection removal.func ( *Manager) ( uint64) {.credentialsListeners.Remove()}
![]() |
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. |