mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-05 07:45:27 -04:00
Simplify node lock and various small changes
Co-authored-by: Fabian Kammel <fabian@kammel.dev> Co-authored-by: Daniel Weiße <66256922+daniel-weisse@users.noreply.github.com>
This commit is contained in:
parent
2bcf001d52
commit
cce2611e2a
31 changed files with 530 additions and 229 deletions
|
@ -14,39 +14,23 @@ import (
|
|||
// There is no way to unlock, so the state changes only once from unlock to
|
||||
// locked.
|
||||
type Lock struct {
|
||||
tpm vtpm.TPMOpenFunc
|
||||
locked bool
|
||||
state *sync.Mutex
|
||||
mux *sync.RWMutex
|
||||
tpm vtpm.TPMOpenFunc
|
||||
mux *sync.Mutex
|
||||
}
|
||||
|
||||
// New creates a new NodeLock, which is unlocked.
|
||||
func New(tpm vtpm.TPMOpenFunc) *Lock {
|
||||
return &Lock{
|
||||
tpm: tpm,
|
||||
state: &sync.Mutex{},
|
||||
mux: &sync.RWMutex{},
|
||||
tpm: tpm,
|
||||
mux: &sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
// TryLockOnce tries to lock the node. If the node is already locked, it
|
||||
// returns false. If the node is unlocked, it locks it and returns true.
|
||||
func (l *Lock) TryLockOnce(ownerID, clusterID []byte) (bool, error) {
|
||||
success := l.state.TryLock()
|
||||
if success {
|
||||
l.mux.Lock()
|
||||
defer l.mux.Unlock()
|
||||
l.locked = true
|
||||
if err := vtpm.MarkNodeAsBootstrapped(l.tpm, ownerID, clusterID); err != nil {
|
||||
return success, err
|
||||
}
|
||||
if !l.mux.TryLock() {
|
||||
return false, nil
|
||||
}
|
||||
return success, nil
|
||||
}
|
||||
|
||||
// Locked returns true if the node is locked.
|
||||
func (l *Lock) Locked() bool {
|
||||
l.mux.RLock()
|
||||
defer l.mux.RUnlock()
|
||||
return l.locked
|
||||
return true, vtpm.MarkNodeAsBootstrapped(l.tpm, ownerID, clusterID)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue