-
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
Changes from 1 commit
264abfc
e778c67
a3920e8
b19da8b
e537239
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
| 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?
Collaborator
Author
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. We can't really evict clients with refCount > 0 (they're actively in use). And we anyway clean this up if refCount reach 0. Let me know if you think otherwise. |
||
| } | ||
| }) | ||
| 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 | ||
samikshya-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ = 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 | ||
|
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. Interesting. This relies on the thread safety of the client and not the manager. I think this is an astute thought because close can be IO expensive because of last minute flushes and manager shouldn't be blocked. |
||
| } | ||
|
|
||
| m.mu.Unlock() | ||
samikshya-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
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?
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.
I was hoping to do it in a later PR, but makes sense to do it right-away! done β
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.
Why do we need to make it thread safe?
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.
Oh so this can be shared by multiple connections because client is at host level. So the entire telemetry client has to fully thread safe. Would be really helpful if we document this upfront.