mirror of
https://github.com/zitadel/zitadel.git
synced 2025-07-02 00:18:32 +00:00

This pull request improves the scalability of the session API by enhancing middleware tracing and refining SQL query behavior for user authentication methods. # Which Problems Are Solved - Eventstore subscriptions locked each other during they wrote the events to the event channels of the subscribers in push. - `ListUserAuthMethodTypesRequired` query used `Bitmap heap scan` to join the tables needed. - The auth and oidc package triggered projections often when data were read. - The session API triggered the user projection each time a user was searched to write the user check command. # How the Problems Are Solved - the `sync.Mutex` was replaced with `sync.RWMutex` to allow parallel read of the map - The query was refactored to use index scans only - if the data should already be up-to-date `shouldTriggerBulk` is set to false - as the user should already exist for some time the trigger was removed. # Additional Changes - refactoring of `tracing#Span.End` calls # Additional Context - part of https://github.com/zitadel/zitadel/issues/9239 --------- Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
107 lines
2.2 KiB
Go
107 lines
2.2 KiB
Go
package eventstore
|
|
|
|
import (
|
|
"slices"
|
|
"sync"
|
|
|
|
"github.com/zitadel/logging"
|
|
)
|
|
|
|
var (
|
|
subscriptions = map[AggregateType][]*Subscription{}
|
|
subsMutex sync.RWMutex
|
|
)
|
|
|
|
type Subscription struct {
|
|
Events chan Event
|
|
types map[AggregateType][]EventType
|
|
}
|
|
|
|
// SubscribeAggregates subscribes for all events on the given aggregates
|
|
func SubscribeAggregates(eventQueue chan Event, aggregates ...AggregateType) *Subscription {
|
|
types := make(map[AggregateType][]EventType, len(aggregates))
|
|
for _, aggregate := range aggregates {
|
|
types[aggregate] = nil
|
|
}
|
|
sub := &Subscription{
|
|
Events: eventQueue,
|
|
types: types,
|
|
}
|
|
|
|
subsMutex.Lock()
|
|
defer subsMutex.Unlock()
|
|
|
|
for _, aggregate := range aggregates {
|
|
subscriptions[aggregate] = append(subscriptions[aggregate], sub)
|
|
}
|
|
|
|
return sub
|
|
}
|
|
|
|
// SubscribeEventTypes subscribes for the given event types
|
|
// if no event types are provided the subscription is for all events of the aggregate
|
|
func SubscribeEventTypes(eventQueue chan Event, types map[AggregateType][]EventType) *Subscription {
|
|
sub := &Subscription{
|
|
Events: eventQueue,
|
|
types: types,
|
|
}
|
|
|
|
subsMutex.Lock()
|
|
defer subsMutex.Unlock()
|
|
|
|
for aggregate := range types {
|
|
subscriptions[aggregate] = append(subscriptions[aggregate], sub)
|
|
}
|
|
|
|
return sub
|
|
}
|
|
|
|
func (es *Eventstore) notify(events []Event) {
|
|
subsMutex.RLock()
|
|
defer subsMutex.RUnlock()
|
|
for _, event := range events {
|
|
subs, ok := subscriptions[event.Aggregate().Type]
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, sub := range subs {
|
|
eventTypes := sub.types[event.Aggregate().Type]
|
|
//subscription for all events
|
|
if len(eventTypes) == 0 {
|
|
sub.Events <- event
|
|
continue
|
|
}
|
|
//subscription for certain events
|
|
if slices.Contains(eventTypes, event.Type()) {
|
|
select {
|
|
case sub.Events <- event:
|
|
default:
|
|
logging.Debug("unable to push event")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Subscription) Unsubscribe() {
|
|
subsMutex.Lock()
|
|
defer subsMutex.Unlock()
|
|
for aggregate := range s.types {
|
|
subs, ok := subscriptions[aggregate]
|
|
if !ok {
|
|
continue
|
|
}
|
|
for i := len(subs) - 1; i >= 0; i-- {
|
|
if subs[i] == s {
|
|
subs[i] = subs[len(subs)-1]
|
|
subs[len(subs)-1] = nil
|
|
subs = subs[:len(subs)-1]
|
|
}
|
|
}
|
|
}
|
|
_, ok := <-s.Events
|
|
if ok {
|
|
close(s.Events)
|
|
}
|
|
}
|