mirror of
https://github.com/rocky-linux/peridot.git
synced 2024-12-18 08:58:30 +00:00
Switch to sync.Map for keywarming
Signed-off-by: Mustafa Gezen <mustafa@ctrliq.com>
This commit is contained in:
parent
70c45775cb
commit
e810946c70
@ -38,8 +38,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"peridot.resf.org/peridot/db/models"
|
"peridot.resf.org/peridot/db/models"
|
||||||
"peridot.resf.org/utils"
|
"peridot.resf.org/utils"
|
||||||
@ -88,13 +86,10 @@ func (s *Server) importGpgKey(armoredKey string) error {
|
|||||||
// WarmGPGKey warms up a specific GPG key
|
// WarmGPGKey warms up a specific GPG key
|
||||||
// This involves shelling out to GPG to import the key
|
// This involves shelling out to GPG to import the key
|
||||||
func (s *Server) WarmGPGKey(key string, armoredKey string, gpgKey *crypto.Key, db *models.Key) (*LoadedKey, error) {
|
func (s *Server) WarmGPGKey(key string, armoredKey string, gpgKey *crypto.Key, db *models.Key) (*LoadedKey, error) {
|
||||||
s.keyImportLock.ReadLock(key)
|
cachedKeyAny, ok := s.keys.Load(key)
|
||||||
defer s.keyImportLock.ReadUnlock(key)
|
|
||||||
|
|
||||||
cachedKey := s.keys[key]
|
|
||||||
// This means that the key is already loaded
|
// This means that the key is already loaded
|
||||||
if cachedKey != nil {
|
if ok {
|
||||||
return cachedKey, nil
|
return cachedKeyAny.(*LoadedKey), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := s.importGpgKey(armoredKey)
|
err := s.importGpgKey(armoredKey)
|
||||||
@ -102,21 +97,20 @@ func (s *Server) WarmGPGKey(key string, armoredKey string, gpgKey *crypto.Key, d
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cachedKey == nil {
|
cachedKey := &LoadedKey{
|
||||||
s.keys[key] = &LoadedKey{
|
keyUuid: db.ID,
|
||||||
keyUuid: db.ID,
|
gpgId: gpgKey.GetHexKeyID(),
|
||||||
gpgId: gpgKey.GetHexKeyID(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
s.keys.Store(key, cachedKey)
|
||||||
|
|
||||||
return s.keys[key], nil
|
return cachedKey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnsureGPGKey ensures that the key is loaded
|
// EnsureGPGKey ensures that the key is loaded
|
||||||
func (s *Server) EnsureGPGKey(key string) (*LoadedKey, error) {
|
func (s *Server) EnsureGPGKey(key string) (*LoadedKey, error) {
|
||||||
cachedKey := s.keys[key]
|
cachedKeyAny, ok := s.keys.Load(key)
|
||||||
if cachedKey != nil {
|
if ok {
|
||||||
return cachedKey, nil
|
return cachedKeyAny.(*LoadedKey), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Key not found in cache, fetch from database
|
// Key not found in cache, fetch from database
|
||||||
|
@ -57,45 +57,17 @@ import (
|
|||||||
|
|
||||||
const TaskQueue = "keykeeper"
|
const TaskQueue = "keykeeper"
|
||||||
|
|
||||||
type MapStringLock struct {
|
|
||||||
*sync.RWMutex
|
|
||||||
m map[string]*sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MapStringLock) ReadLock(key string) {
|
|
||||||
m.RLock()
|
|
||||||
defer m.RUnlock()
|
|
||||||
if m.m[key] == nil {
|
|
||||||
m.Lock()
|
|
||||||
m.m[key] = &sync.Mutex{}
|
|
||||||
m.Unlock()
|
|
||||||
}
|
|
||||||
m.m[key].Lock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MapStringLock) ReadUnlock(key string) {
|
|
||||||
m.RLock()
|
|
||||||
defer m.RUnlock()
|
|
||||||
if m.m[key] == nil {
|
|
||||||
m.Lock()
|
|
||||||
m.m[key] = &sync.Mutex{}
|
|
||||||
m.Unlock()
|
|
||||||
}
|
|
||||||
m.m[key].Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
keykeeperpb.UnimplementedKeykeeperServiceServer
|
keykeeperpb.UnimplementedKeykeeperServiceServer
|
||||||
|
|
||||||
log *logrus.Logger
|
log *logrus.Logger
|
||||||
db peridotdb.Access
|
db peridotdb.Access
|
||||||
storage lookaside.Storage
|
storage lookaside.Storage
|
||||||
worker worker.Worker
|
worker worker.Worker
|
||||||
temporal client.Client
|
temporal client.Client
|
||||||
stores map[string]store.Store
|
stores map[string]store.Store
|
||||||
keys map[string]*LoadedKey
|
keys *sync.Map
|
||||||
keyImportLock *MapStringLock
|
defaultStore string
|
||||||
defaultStore string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(db peridotdb.Access, c client.Client) (*Server, error) {
|
func NewServer(db peridotdb.Access, c client.Client) (*Server, error) {
|
||||||
@ -116,13 +88,9 @@ func NewServer(db peridotdb.Access, c client.Client) (*Server, error) {
|
|||||||
worker: worker.New(c, TaskQueue, worker.Options{
|
worker: worker.New(c, TaskQueue, worker.Options{
|
||||||
DeadlockDetectionTimeout: 15 * time.Minute,
|
DeadlockDetectionTimeout: 15 * time.Minute,
|
||||||
}),
|
}),
|
||||||
temporal: c,
|
temporal: c,
|
||||||
stores: map[string]store.Store{"awssm": sm},
|
stores: map[string]store.Store{"awssm": sm},
|
||||||
keys: map[string]*LoadedKey{},
|
keys: &sync.Map{},
|
||||||
keyImportLock: &MapStringLock{
|
|
||||||
RWMutex: &sync.RWMutex{},
|
|
||||||
m: map[string]*sync.Mutex{},
|
|
||||||
},
|
|
||||||
defaultStore: "awssm",
|
defaultStore: "awssm",
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user