mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-23 05:57:19 +02:00
cache: support databroker option changes (#1294)
This commit is contained in:
parent
31205c0c29
commit
a1378c81f8
16 changed files with 408 additions and 179 deletions
|
@ -52,6 +52,9 @@ type DB struct {
|
|||
byVersion *btree.BTree
|
||||
deletedIDs []string
|
||||
onchange *signal.Signal
|
||||
|
||||
closeOnce sync.Once
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
// NewDB creates a new in-memory database for the given record type.
|
||||
|
@ -62,6 +65,7 @@ func NewDB(recordType string, btreeDegree int) *DB {
|
|||
byID: btree.New(btreeDegree),
|
||||
byVersion: btree.New(btreeDegree),
|
||||
onchange: s,
|
||||
closed: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,6 +88,14 @@ func (db *DB) ClearDeleted(_ context.Context, cutoff time.Time) {
|
|||
db.deletedIDs = remaining
|
||||
}
|
||||
|
||||
// Close closes the database. Any watchers will be closed.
|
||||
func (db *DB) Close() error {
|
||||
db.closeOnce.Do(func() {
|
||||
close(db.closed)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete marks a record as deleted.
|
||||
func (db *DB) Delete(_ context.Context, id string) error {
|
||||
defer db.onchange.Broadcast()
|
||||
|
@ -140,7 +152,10 @@ func (db *DB) Put(_ context.Context, id string, data *anypb.Any) error {
|
|||
func (db *DB) Watch(ctx context.Context) <-chan struct{} {
|
||||
ch := db.onchange.Bind()
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
select {
|
||||
case <-db.closed:
|
||||
case <-ctx.Done():
|
||||
}
|
||||
close(ch)
|
||||
db.onchange.Unbind(ch)
|
||||
}()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue