Skip to content
Snippets Groups Projects

Allow to reset key store and stop key fetching

Merged Malte Bauch requested to merge qkdnm-set-key-store into master
2 files
+ 20
17
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -31,7 +31,7 @@ type ETSI014HTTPQuantumModule struct {
keyFetchInterval int
keyFetchAmount int64
maxKeyFillLevel uint64
stopFetch chan struct{}
stopFetch context.CancelFunc
}
func NewETSI014HTTPQuantumModule(addr, kmsId, localSAEID, targetSAEID string, tlsConfig config.TLSConfig, master bool, keyFetchInterval int, keyFetchAmount int64, maxKeyFillLevel uint64) (*ETSI014HTTPQuantumModule, error) {
@@ -107,7 +107,8 @@ func (qm *ETSI014HTTPQuantumModule) Client() *etsi14ClientImpl.ClientImpl {
}
func (qm *ETSI014HTTPQuantumModule) Initialize() error {
qm.stopFetch = make(chan struct{}, 0)
var ctx context.Context
ctx, qm.stopFetch = context.WithCancel(context.Background())
// start polling keys
if qm.master {
@@ -117,8 +118,14 @@ func (qm *ETSI014HTTPQuantumModule) Initialize() error {
defer ticker.Stop()
// immediately start with the ticker instead of waiting the defined amount
for ; true; <-ticker.C {
qm.doKeyFetching()
RestartFetchLoop:
for {
select {
case <-ticker.C:
qm.doKeyFetching(ctx)
case <-ctx.Done():
break RestartFetchLoop
}
}
}()
}
@@ -127,7 +134,7 @@ func (qm *ETSI014HTTPQuantumModule) Initialize() error {
func (qm *ETSI014HTTPQuantumModule) StopKeyFetching() {
if qm.master {
close(qm.stopFetch)
qm.stopFetch()
}
}
@@ -185,18 +192,19 @@ func (qm *ETSI014HTTPQuantumModule) GetKeyWithIds(keyIds []etsi14ClientGenerated
return container, nil
}
func (qm *ETSI014HTTPQuantumModule) doKeyFetching() {
func (qm *ETSI014HTTPQuantumModule) doKeyFetching(ctx context.Context) {
ticker := time.NewTicker(time.Duration(qm.keyFetchInterval) * time.Second)
defer ticker.Stop()
failedAttemps := 0
FetchLoop:
for {
select {
case <-ticker.C:
if failedAttemps == maxFailedKeyRequestAttempts {
log.Errorf("stopped trying to fetch keys from qkd module after %d tries", failedAttemps)
break
break FetchLoop
}
if qm.keyStore.Length() < int(qm.maxKeyFillLevel) {
@@ -233,9 +241,8 @@ func (qm *ETSI014HTTPQuantumModule) doKeyFetching() {
failedAttemps = 0
}
case <-qm.stopFetch:
break
case <-ctx.Done():
break FetchLoop
}
}
}
Loading