mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 20:57:31 +00:00
refactor(notification): use new queue package (#9360)
# Which Problems Are Solved The recently introduced notification queue have potential race conditions. # How the Problems Are Solved Current code is refactored to use the queue package, which is safe in regards of concurrency. # Additional Changes - the queue is included in startup - improved code quality of queue # Additional Context - closes https://github.com/zitadel/zitadel/issues/9278
This commit is contained in:
45
internal/queue/database.go
Normal file
45
internal/queue/database.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/database/dialect"
|
||||
)
|
||||
|
||||
const (
|
||||
schema = "queue"
|
||||
applicationName = "zitadel_queue"
|
||||
)
|
||||
|
||||
var conns = &sync.Map{}
|
||||
|
||||
type queueKey struct{}
|
||||
|
||||
func WithQueue(parent context.Context) context.Context {
|
||||
return context.WithValue(parent, queueKey{}, struct{}{})
|
||||
}
|
||||
|
||||
func init() {
|
||||
dialect.RegisterBeforeAcquire(func(ctx context.Context, c *pgx.Conn) error {
|
||||
if _, ok := ctx.Value(queueKey{}).(struct{}); !ok {
|
||||
return nil
|
||||
}
|
||||
_, err := c.Exec(ctx, "SET search_path TO "+schema+"; SET application_name TO "+applicationName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conns.Store(c, struct{}{})
|
||||
return nil
|
||||
})
|
||||
dialect.RegisterAfterRelease(func(c *pgx.Conn) error {
|
||||
_, ok := conns.LoadAndDelete(c)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
_, err := c.Exec(context.Background(), "SET search_path TO DEFAULT; SET application_name TO "+dialect.DefaultAppName)
|
||||
return err
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user