Documentation
¶
Overview ¶
Package core provides core infrastructure for TGCP including authentication, HTTP client configuration, caching, service registry, and project management.
HTTP Client Configuration ¶
The HTTP client is configured with three layers of middleware:
- Google Authentication (Application Default Credentials)
- Rate Limiting (Token Bucket Algorithm)
- Retry Logic (Exponential Backoff)
Rate Limiting ¶
The rate limiter uses a token bucket algorithm to prevent API quota exhaustion. Configuration: 10 requests per second with a burst capacity of 20 requests.
How it works:
- Tokens are added to the bucket at a constant rate (10 tokens/second)
- Each request consumes 1 token
- If tokens are available, the request proceeds immediately
- If no tokens are available, the request waits until a token is available
- Burst capacity allows handling traffic spikes up to 20 requests
Example:
client, err := core.NewHTTPClient(ctx, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return err
}
// All requests through this client are automatically rate-limited
Retry Logic ¶
The retry transport implements exponential backoff for transient failures. Configuration: Maximum 3 retries with exponential backoff (100ms, 200ms, 400ms).
Retry conditions:
- Network errors (connection failures, timeouts)
- HTTP 429 (Too Many Requests) - rate limit exceeded
- HTTP 5xx (Server Errors) - temporary server issues
Non-retryable conditions:
- HTTP 4xx (Client Errors) - except 429
- Context cancellation
- Maximum retries exceeded
Backoff calculation: 2^i * 100ms where i is the retry attempt (0, 1, 2)
- Attempt 1: 100ms delay
- Attempt 2: 200ms delay
- Attempt 3: 400ms delay
Example:
// A request that fails with 500 will be retried up to 3 times
// with increasing delays between attempts
resp, err := client.Get("https://compute.googleapis.com/...")
Index ¶
- Constants
- func CheckForUpdates(currentVersion string) tea.Cmd
- func NewHTTPClient(ctx context.Context, scopes ...string) (*http.Client, error)
- type AuthState
- type Cache
- type CacheItem
- type Command
- type GitHubRelease
- type LastUpdatedMsg
- type LoadingMsg
- type NavigationModel
- type Project
- type ProjectManager
- type RateLimitTransport
- type RetryTransport
- type Route
- type ServiceFactory
- type ServiceRegistry
- func (r *ServiceRegistry) GetOrInitializeService(ctx context.Context, name string) (services.Service, error)
- func (r *ServiceRegistry) GetServiceNames() []string
- func (r *ServiceRegistry) InitializeAll(ctx context.Context, projectID string) map[string]services.Service
- func (r *ServiceRegistry) IsRegistered(name string) bool
- func (r *ServiceRegistry) Register(name string, factory ServiceFactory)
- func (r *ServiceRegistry) ReinitializeAll(ctx context.Context, projectID string, svcMap map[string]services.Service)
- type StatusMsg
- type SuggestionMatch
- type SwitchToLogsMsg
- type SwitchToServiceMsg
- type ToastMsg
- type ToastType
- type TokenBucket
- type UpdateCheckedMsg
- type UpdateInfo
- type VersionInfo
- type ViewType
Constants ¶
const ( // GitHubRepo is the repository for version checking GitHubRepo = "yogirk/tgcp" // ReleasesURL is the GitHub API endpoint for latest release ReleasesURL = "https://api.github.com/repos/" + GitHubRepo + "/releases/latest" )
Variables ¶
This section is empty.
Functions ¶
func CheckForUpdates ¶ added in v0.2.0
CheckForUpdates fetches the latest release from GitHub and compares versions
func NewHTTPClient ¶
NewHTTPClient returns an http.Client configured with: 1. Google Authentication (ADC) 2. Rate Limiting (10 RPS, Burst 20) 3. Retry Logic (Exponential Backoff, 3 retries)
The client uses a middleware chain: Client -> RateLimit -> Retry -> Auth(Base) All requests through this client are automatically rate-limited and retried on transient failures.
Example:
ctx := context.Background()
client, err := core.NewHTTPClient(ctx, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return fmt.Errorf("failed to create HTTP client: %w", err)
}
// Use client for GCP API calls - rate limiting and retries are automatic
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache provides a thread-safe in-memory cache
type GitHubRelease ¶ added in v0.2.0
type GitHubRelease struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
HTMLURL string `json:"html_url"`
Body string `json:"body"`
Prerelease bool `json:"prerelease"`
Draft bool `json:"draft"`
PublishedAt string `json:"published_at"`
}
GitHubRelease represents the GitHub API response for a release
type LastUpdatedMsg ¶
LastUpdatedMsg updates the "Last Updated" timestamp
type LoadingMsg ¶
type LoadingMsg struct {
IsLoading bool
Message string // Optional custom message (empty = use playful messages)
}
LoadingMsg signals loading state changes to MainModel Services emit this when they start/stop loading data
type NavigationModel ¶
type NavigationModel struct {
// Palette State
}
NavigationModel manages routing and command palette state
func NewNavigation ¶
func NewNavigation() NavigationModel
func (*NavigationModel) ExecuteSelection ¶
func (m *NavigationModel) ExecuteSelection() *Route
ExecuteSelection returns the route for the selected command
func (*NavigationModel) FilterCommands ¶
func (m *NavigationModel) FilterCommands(query string)
FilterCommands updates suggestions based on input query.
Ranking is a three-tier cascade so that literal matches always beat loose fuzzy hits — critical for short queries like "gce" where a plain fuzzy rank would float "GKE" above "GCE":
- Prefix match on Name (case-insensitive)
- Substring match anywhere in Name
- Fuzzy match on "Name + Description" for everything else
Within each tier, results preserve registration order from defaultCommands.
func (*NavigationModel) RestoreBaseCommands ¶
func (m *NavigationModel) RestoreBaseCommands()
RestoreBaseCommands resets to default commands
func (*NavigationModel) SelectNext ¶
func (m *NavigationModel) SelectNext()
SelectNext moves selection down
func (*NavigationModel) SelectPrev ¶
func (m *NavigationModel) SelectPrev()
SelectPrev moves selection up
func (*NavigationModel) SetCommands ¶
func (m *NavigationModel) SetCommands(cmds []Command)
SetCommands updates the available commands (e.g. for switching context)
type ProjectManager ¶
type ProjectManager struct {
// contains filtered or unexported fields
}
ProjectManager handles project listing and switching
func NewProjectManager ¶
func NewProjectManager(cache *Cache) *ProjectManager
NewProjectManager creates a new manager
func (*ProjectManager) ListProjects ¶
func (pm *ProjectManager) ListProjects(ctx context.Context) ([]Project, error)
ListProjects fetches available projects for the user
func (*ProjectManager) SearchProjects ¶
func (pm *ProjectManager) SearchProjects(query string) []Project
SearchProjects filters projects by query
type RateLimitTransport ¶
type RateLimitTransport struct {
Next http.RoundTripper
Limiter *TokenBucket
}
type RetryTransport ¶
type RetryTransport struct {
Next http.RoundTripper
}
RetryTransport implements automatic retry with exponential backoff for HTTP requests. It wraps an existing http.RoundTripper and retries failed requests up to 3 times.
Retry conditions:
- Network errors (connection failures, timeouts)
- HTTP 429 (Too Many Requests)
- HTTP 5xx (Server Errors)
Non-retryable:
- HTTP 4xx (Client Errors) except 429
- Context cancellation
- Maximum retries exceeded
Backoff: Exponential backoff with formula 2^i * 100ms
- Retry 1: 100ms delay
- Retry 2: 200ms delay
- Retry 3: 400ms delay
func (*RetryTransport) RoundTrip ¶
RoundTrip executes the HTTP request with automatic retry on transient failures. It implements exponential backoff between retry attempts.
The method will retry up to 3 times for:
- Network errors
- HTTP 429 (rate limit)
- HTTP 5xx (server errors)
Returns the last response/error if all retries are exhausted.
type Route ¶
type Route struct {
View ViewType
Service string // e.g., "gce", "sql"
ID string // resource ID or Project ID
}
Route represents a navigational destination
type ServiceFactory ¶
ServiceFactory is a function that creates a new service instance
type ServiceRegistry ¶
type ServiceRegistry struct {
// contains filtered or unexported fields
}
ServiceRegistry manages service registration and lazy initialization
func NewServiceRegistry ¶
func NewServiceRegistry(cache *Cache) *ServiceRegistry
NewServiceRegistry creates a new service registry
func (*ServiceRegistry) GetOrInitializeService ¶
func (r *ServiceRegistry) GetOrInitializeService(ctx context.Context, name string) (services.Service, error)
GetOrInitializeService gets a service from the map, initializing it lazily if needed This is the key method for lazy initialization - services are only initialized when first accessed
func (*ServiceRegistry) GetServiceNames ¶
func (r *ServiceRegistry) GetServiceNames() []string
GetServiceNames returns a list of all registered service names
func (*ServiceRegistry) InitializeAll ¶
func (r *ServiceRegistry) InitializeAll(ctx context.Context, projectID string) map[string]services.Service
InitializeAll creates all registered services but does NOT initialize them Services are initialized lazily on first access via GetOrInitializeService
func (*ServiceRegistry) IsRegistered ¶
func (r *ServiceRegistry) IsRegistered(name string) bool
IsRegistered checks if a service is registered
func (*ServiceRegistry) Register ¶
func (r *ServiceRegistry) Register(name string, factory ServiceFactory)
Register registers a service factory with the given name
func (*ServiceRegistry) ReinitializeAll ¶
func (r *ServiceRegistry) ReinitializeAll(ctx context.Context, projectID string, svcMap map[string]services.Service)
ReinitializeAll reinitializes all initialized services with a new project ID
type SuggestionMatch ¶
type SuggestionMatch struct {
Command
MatchedIndexes []int // Positions of matched characters in Name+Description
}
SuggestionMatch wraps a Command with fuzzy match info for highlighting
type SwitchToLogsMsg ¶
type SwitchToLogsMsg struct {
Filter string
Source string // The short name of the service initiating the switch
Heading string // Optional heading to display (e.g. resource name)
}
SwitchToLogsMsg requests a context switch to the logging service
type SwitchToServiceMsg ¶ added in v0.2.0
type SwitchToServiceMsg struct {
Service string // The short name of the service to switch to
}
SwitchToServiceMsg requests a context switch to a specific service
type ToastMsg ¶
type ToastMsg struct {
Message string
Type ToastType
Duration time.Duration // 0 means use default (3 seconds)
}
ToastMsg triggers a toast notification in the UI
type TokenBucket ¶
type TokenBucket struct {
// contains filtered or unexported fields
}
TokenBucket implements a token bucket rate limiting algorithm. It allows a certain number of requests per second (rate) with a burst capacity.
Algorithm:
- Tokens are added to the bucket at a constant rate
- Each request consumes one token
- If tokens are available, the request proceeds immediately
- If no tokens are available, the request waits until tokens are refilled
Thread-safe: Uses mutex to protect concurrent access to token state.
func NewTokenBucket ¶
func NewTokenBucket(rate, burst float64) *TokenBucket
NewTokenBucket creates a new token bucket rate limiter.
Parameters:
- rate: Tokens added per second (e.g., 10.0 = 10 requests/second)
- burst: Maximum tokens (e.g., 20.0 = can handle 20 requests in quick succession)
Example:
limiter := NewTokenBucket(10.0, 20.0) // 10 req/s, burst of 20
func (*TokenBucket) Wait ¶
func (tb *TokenBucket) Wait(ctx context.Context) error
Wait blocks until a token is available or the context is cancelled. It automatically refills tokens based on elapsed time since last refill.
Returns:
- nil if a token was successfully acquired
- context.Err() if the context was cancelled
Example:
if err := limiter.Wait(ctx); err != nil {
return err // Context cancelled
}
// Token acquired, proceed with request
type UpdateCheckedMsg ¶ added in v0.2.0
type UpdateCheckedMsg struct {
UpdateInfo UpdateInfo
}
UpdateCheckedMsg is sent when version check completes
type UpdateInfo ¶ added in v0.2.0
type UpdateInfo struct {
Available bool
LatestVersion string
CurrentVersion string
ReleaseURL string
ReleaseNotes string
CheckedAt time.Time
Error error
}
UpdateInfo holds information about available updates
type VersionInfo ¶ added in v0.2.0
VersionInfo holds the current application version information
func (VersionInfo) FormatVersion ¶ added in v0.2.0
func (v VersionInfo) FormatVersion() string
FormatVersion returns a formatted version string
func (VersionInfo) String ¶ added in v0.2.0
func (v VersionInfo) String() string
String returns a full version string with commit info