Srikanth Chekuri 210c5fd7f2
feat: opamp server application (#1787)
* feat: opamp server application

* chore: opamp

* chore: refactor server implementation

* chore: add Stop

* chore: merged opamp updates

* chore: removed all errorf

* chore: added a comment about zero version

* feat: added user context for created by

* chore: changed debugf to debug

* chore: removed lb from opamp + added config parser

* fix: added userid to ConfigNewVersion()

* chore: removed user id from contxt and added config parser

* fix: removed lock inside re-deploy

* chore: added config db fix

* fix: merged app/server.go from develop

* fix: restored extract jwt

* Update pkg/query-service/app/server.go

Co-authored-by: Nityananda Gohain <nityanandagohain@gmail.com>

* fix: dependency version fix and import added

---------

Co-authored-by: Pranay Prateek <pranay@signoz.io>
Co-authored-by: Palash Gupta <palashgdev@gmail.com>
Co-authored-by: mindhash <mindhash@mindhashs-MacBook-Pro.local>
Co-authored-by: Nityananda Gohain <nityanandagohain@gmail.com>
2023-03-15 15:09:15 +05:30

67 lines
1.7 KiB
Go

package model
import (
"fmt"
"sync"
)
// communicates with calling apis when config is applied or fails
var coordinator *Coordinator
func init() {
subscribers := make(map[string][]OnChangeCallback, 0)
coordinator = &Coordinator{
subscribers: subscribers,
}
}
type OnChangeCallback func(agentId string, hash string, err error)
// responsible for managing subscribers on config change
type Coordinator struct {
mutex sync.Mutex
// hash wise list of subscribers
subscribers map[string][]OnChangeCallback
}
func onConfigSuccess(agentId string, hash string) {
notifySubscribers(agentId, hash, nil)
}
func onConfigFailure(agentId string, hash string, errorMessage string) {
notifySubscribers(agentId, hash, fmt.Errorf(errorMessage))
}
// OnSuccess listens to config changes and notifies subscribers
func notifySubscribers(agentId string, hash string, err error) {
// this method currently does not handle multi-agent scenario.
// as soon as a message is delivered, we release all the subscribers
// for a given hash
subs, ok := coordinator.subscribers[hash]
if !ok {
return
}
for _, s := range subs {
s(agentId, hash, err)
}
// delete all subscribers for this hash, assume future
// notifies will be disabled. the first response is processed
delete(coordinator.subscribers, hash)
}
// callers subscribe to this function to listen on config change requests
func ListenToConfigUpdate(agentId string, hash string, ss OnChangeCallback) {
coordinator.mutex.Lock()
defer coordinator.mutex.Unlock()
if subs, ok := coordinator.subscribers[hash]; ok {
subs = append(subs, ss)
coordinator.subscribers[hash] = subs
} else {
coordinator.subscribers[hash] = []OnChangeCallback{ss}
}
}