-
Notifications
You must be signed in to change notification settings - Fork 55
[PECOBLR-1147] Implement Client Manager for Per-Host Clients #305
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
Open
samikshya-db
wants to merge
1
commit into
stack/PECOBLR-1146-feature-flag-cache
Choose a base branch
from
stack/PECOBLR-1147-client-manager
base: stack/PECOBLR-1146-feature-flag-cache
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+435
β1
Open
Changes from all commits
Commits
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
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,38 @@ | ||
| package telemetry | ||
|
|
||
| import ( | ||
| "net/http" | ||
| ) | ||
|
|
||
| // telemetryClient represents a client for sending telemetry data to Databricks. | ||
| // This is a minimal stub implementation that will be fully implemented in Phase 4. | ||
| type telemetryClient struct { | ||
| host string | ||
| httpClient *http.Client | ||
| cfg *Config | ||
| started bool | ||
| closed bool | ||
| } | ||
|
|
||
| // newTelemetryClient creates a new telemetry client for the given host. | ||
| func newTelemetryClient(host string, httpClient *http.Client, cfg *Config) *telemetryClient { | ||
| return &telemetryClient{ | ||
| host: host, | ||
| httpClient: httpClient, | ||
| cfg: cfg, | ||
| } | ||
| } | ||
|
|
||
| // start starts the telemetry client's background operations. | ||
| // This is a stub implementation that will be fully implemented in Phase 4. | ||
| func (c *telemetryClient) start() error { | ||
| c.started = true | ||
| return nil | ||
| } | ||
|
|
||
| // close stops the telemetry client and flushes any pending data. | ||
| // This is a stub implementation that will be fully implemented in Phase 4. | ||
| func (c *telemetryClient) close() error { | ||
| c.closed = true | ||
| return nil | ||
| } | ||
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,73 @@ | ||
| package telemetry | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "sync" | ||
| ) | ||
|
|
||
| // clientManager manages one telemetry client per host. | ||
| // Prevents rate limiting by sharing clients across connections. | ||
| type clientManager struct { | ||
| mu sync.RWMutex | ||
| clients map[string]*clientHolder | ||
| } | ||
|
|
||
| // clientHolder holds a telemetry client and its reference count. | ||
| type clientHolder struct { | ||
| client *telemetryClient | ||
| refCount int | ||
| } | ||
|
|
||
| var ( | ||
| managerOnce sync.Once | ||
| managerInstance *clientManager | ||
| ) | ||
|
|
||
| // getClientManager returns the singleton instance. | ||
| func getClientManager() *clientManager { | ||
| managerOnce.Do(func() { | ||
| managerInstance = &clientManager{ | ||
| clients: make(map[string]*clientHolder), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to put a max size or any LRU kind of cache? |
||
| } | ||
| }) | ||
| return managerInstance | ||
| } | ||
|
|
||
| // getOrCreateClient gets or creates a telemetry client for the host. | ||
| // Increments reference count. | ||
| func (m *clientManager) getOrCreateClient(host string, httpClient *http.Client, cfg *Config) *telemetryClient { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
|
|
||
| holder, exists := m.clients[host] | ||
| if !exists { | ||
| holder = &clientHolder{ | ||
| client: newTelemetryClient(host, httpClient, cfg), | ||
| } | ||
| m.clients[host] = holder | ||
| _ = holder.client.start() // Start background flush goroutine | ||
| } | ||
| holder.refCount++ | ||
| return holder.client | ||
| } | ||
|
|
||
| // releaseClient decrements reference count for the host. | ||
| // Closes and removes client when ref count reaches zero. | ||
| func (m *clientManager) releaseClient(host string) error { | ||
| m.mu.Lock() | ||
| holder, exists := m.clients[host] | ||
| if !exists { | ||
| m.mu.Unlock() | ||
| return nil | ||
| } | ||
|
|
||
| holder.refCount-- | ||
| if holder.refCount <= 0 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we log when this becomes negative, that will likely be a bug |
||
| delete(m.clients, host) | ||
| m.mu.Unlock() | ||
| return holder.client.close() // Close and flush | ||
| } | ||
|
|
||
| m.mu.Unlock() | ||
| return nil | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to make this and below method thread safe?