Skip to main content

Common

Utility functions for managing Airtable API tokens in a thread-safe manner.

Overview

The Common module provides thread-safe token management functionality for the Airtable package. It maintains a global map of tokens indexed by UUIDs, allowing multiple goroutines to safely access and modify tokens concurrently.

Functions

getToken

Retrieves a token by its ID from the global tokens map.

Parameters:

  • id (string) - The UUID identifier of the token to retrieve

Returns:

  • token (string) - The token value associated with the provided ID, or empty string if not found

Thread Safety: Uses a read lock to ensure safe concurrent access to the tokens map.

addToken

Adds a new token to the global tokens map and returns a unique identifier for it.

Parameters:

  • token (string) - The token value to store

Returns:

  • tokenId (string) - A unique UUID identifier for the stored token

Thread Safety: Uses a write lock to ensure exclusive access while adding the token to prevent race conditions.

Implementation Details:

  1. Generates a new UUID using google/uuid package
  2. Stores the token in the global tokens map with the UUID as key
  3. Returns the generated UUID

removeToken

Removes a token from the global tokens map using its ID.

Parameters:

  • id (string) - The UUID identifier of the token to remove

Returns:

  • None

Thread Safety: Uses a write lock to ensure exclusive access while removing the token to prevent race conditions.

Global Variables

tokens

A global map that stores token values indexed by UUID strings.

Type: map[string]string

cMutex

A read-write mutex that ensures thread-safe access to the tokens map.

Type: sync.RWMutex

Concurrency Handling

The module uses a sync.RWMutex to handle concurrent access:

  • Read operations (getToken) use RLock() for better performance when multiple goroutines only need to read
  • Write operations (addToken, removeToken) use Lock() for exclusive access
  • All locks are properly released using defer statements to prevent deadlocks

Usage Notes

  • Tokens are stored in memory and will be lost when the application restarts
  • Each token is associated with a UUID identifier generated by the addToken function
  • The module is designed for internal use within the Airtable package and should not be directly accessed by users
  • For security reasons, tokens should be handled carefully and removed when no longer needed using removeToken