-
Notifications
You must be signed in to change notification settings - Fork 0
fix: failover for greptimedb #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/killme2008/devtap/internal/store" | ||
| ) | ||
|
|
||
| const defaultLazyCooldown = 30 * time.Second | ||
|
|
||
| // lazyStore wraps a store.Store that is connected on first use and retried | ||
| // with a cooldown on failure. This allows the MCP server to start even when | ||
| // the configured store (e.g. GreptimeDB) is temporarily unavailable, and | ||
| // reconnect once it becomes reachable. | ||
| type lazyStore struct { | ||
| mu sync.Mutex | ||
| inner store.Store | ||
| connectFn func() (store.Store, error) | ||
| lastAttempt time.Time | ||
| lastErr error | ||
| cooldown time.Duration | ||
| closed bool | ||
| } | ||
|
|
||
| func newLazyStore(connectFn func() (store.Store, error)) *lazyStore { | ||
| return &lazyStore{ | ||
| connectFn: connectFn, | ||
| cooldown: defaultLazyCooldown, | ||
| } | ||
| } | ||
|
|
||
| // ensureConnected attempts to establish the inner store if not already connected. | ||
| // Returns nil if connected, or the connection error if unavailable. | ||
| func (ls *lazyStore) ensureConnected() error { | ||
| if ls.closed { | ||
| return errors.New("store closed") | ||
| } | ||
| if ls.inner != nil { | ||
| return nil | ||
| } | ||
| if !ls.lastAttempt.IsZero() && time.Since(ls.lastAttempt) < ls.cooldown { | ||
| return fmt.Errorf("store unavailable (cooldown): %w", ls.lastErr) | ||
| } | ||
| ls.lastAttempt = time.Now() | ||
| s, err := ls.connectFn() | ||
| if err != nil { | ||
| ls.lastErr = err | ||
| return err | ||
| } | ||
| ls.lastErr = nil | ||
| ls.inner = s | ||
| return nil | ||
| } | ||
|
|
||
| // reset closes the inner store and nils it out so the next call retries. | ||
| // Does NOT clear lastAttempt — the cooldown still applies, which prevents | ||
| // hammering if the store accepts connections but fails on queries. For | ||
| // long-running processes (MCP server), the elapsed time since the original | ||
| // connect is typically >cooldown, so the retry is effectively immediate. | ||
| func (ls *lazyStore) reset() { | ||
| if ls.inner != nil { | ||
| _ = ls.inner.Close() | ||
| ls.inner = nil | ||
| } | ||
killme2008 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (ls *lazyStore) Drain(sessionID string, maxLines int) ([]store.LogMessage, error) { | ||
| ls.mu.Lock() | ||
| defer ls.mu.Unlock() | ||
|
|
||
| if err := ls.ensureConnected(); err != nil { | ||
| return nil, err | ||
| } | ||
| msgs, err := ls.inner.Drain(sessionID, maxLines) | ||
| if err != nil { | ||
| ls.reset() | ||
| return nil, err | ||
| } | ||
| return msgs, nil | ||
| } | ||
|
|
||
| func (ls *lazyStore) Status() (map[string]int, error) { | ||
| ls.mu.Lock() | ||
| defer ls.mu.Unlock() | ||
|
|
||
| if err := ls.ensureConnected(); err != nil { | ||
| return nil, err | ||
| } | ||
| counts, err := ls.inner.Status() | ||
| if err != nil { | ||
| ls.reset() | ||
| return nil, err | ||
| } | ||
| return counts, nil | ||
| } | ||
|
|
||
| func (ls *lazyStore) Write(_ string, _ store.LogMessage) error { | ||
| return errors.New("lazyStore: write not supported (read-only)") | ||
| } | ||
|
|
||
| func (ls *lazyStore) Close() error { | ||
| ls.mu.Lock() | ||
| defer ls.mu.Unlock() | ||
|
|
||
| ls.closed = true | ||
| if ls.inner != nil { | ||
| err := ls.inner.Close() | ||
| ls.inner = nil | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.