snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
87
internal/driver/config/namespace_memory.go
Normal file
87
internal/driver/config/namespace_memory.go
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"github.com/ory/herodot"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/ory/keto/internal/namespace"
|
||||
)
|
||||
|
||||
type (
|
||||
memoryNamespaceManager struct {
|
||||
byName map[string]*namespace.Namespace
|
||||
sync.RWMutex
|
||||
}
|
||||
)
|
||||
|
||||
var _ namespace.Manager = &memoryNamespaceManager{}
|
||||
|
||||
func NewMemoryNamespaceManager(nn ...*namespace.Namespace) *memoryNamespaceManager {
|
||||
s := &memoryNamespaceManager{}
|
||||
s.set(nn)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *memoryNamespaceManager) GetNamespaceByName(_ context.Context, name string) (*namespace.Namespace, error) {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
||||
if n, ok := s.byName[name]; ok {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
return nil, errors.WithStack(herodot.ErrNotFound().WithReasonf("Unknown namespace with name %q.", name))
|
||||
}
|
||||
|
||||
func (s *memoryNamespaceManager) GetNamespaceByConfigID(_ context.Context, id int32) (*namespace.Namespace, error) {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
||||
for _, n := range s.byName {
|
||||
//lint:ignore SA1019 backwards compatibility
|
||||
//nolint:staticcheck
|
||||
if n.ID == id {
|
||||
return n, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.WithStack(herodot.ErrNotFound().WithReasonf("Unknown namespace with id %d.", id))
|
||||
}
|
||||
|
||||
func (s *memoryNamespaceManager) Namespaces(_ context.Context) ([]*namespace.Namespace, error) {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
||||
nn := make([]*namespace.Namespace, 0, len(s.byName))
|
||||
for _, n := range s.byName {
|
||||
nn = append(nn, n)
|
||||
}
|
||||
|
||||
return nn, nil
|
||||
}
|
||||
|
||||
func (s *memoryNamespaceManager) ShouldReload(newValue interface{}) bool {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
||||
nn, _ := s.Namespaces(context.Background())
|
||||
|
||||
return !reflect.DeepEqual(newValue, nn)
|
||||
}
|
||||
|
||||
func (s *memoryNamespaceManager) set(nn []*namespace.Namespace) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
s.byName = make(map[string]*namespace.Namespace, len(nn))
|
||||
for _, n := range nn {
|
||||
s.byName[n.Name] = n
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user