64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package link
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
)
|
|
|
|
// FindingsBucket is the node-engine's state `findings`: the findings this engine writes in its own bucket, one key each,
|
|
// which the condition keeper raises as conditions and clears when the key goes (novox/hq ADR 0564 §2, ADR 0636 §1).
|
|
const FindingsBucket = "mesh-host_findings"
|
|
|
|
func (l *natsLink) findingsKV() (nats.KeyValue, error) {
|
|
kv, err := l.js.KeyValue(FindingsBucket)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the node-engine's state %s cannot be bound (it is created from the node-engine's manifest; "+
|
|
"written again once it is): %w", FindingsBucket, err)
|
|
}
|
|
return kv, nil
|
|
}
|
|
|
|
func (q *Session) findings() (nats.KeyValue, error) {
|
|
q.init()
|
|
q.mu.Lock()
|
|
bus := q.bus
|
|
q.mu.Unlock()
|
|
l, ok := bus.(interface{ findingsKV() (nats.KeyValue, error) })
|
|
if bus == nil || !ok {
|
|
return nil, ErrNoLink
|
|
}
|
|
return l.findingsKV()
|
|
}
|
|
|
|
// PutFinding writes one finding of this engine over the link open now.
|
|
func (q *Session) PutFinding(key string, value []byte) error {
|
|
kv, err := q.findings()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = kv.Put(key, value)
|
|
return err
|
|
}
|
|
|
|
// DeleteFinding clears one finding of this engine over the link open now; a finding already gone is cleared.
|
|
func (q *Session) DeleteFinding(key string) error {
|
|
kv, err := q.findings()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = kv.Delete(key); err != nil && !errors.Is(err, nats.ErrKeyNotFound) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (o OverNATS) findingsKV() (nats.KeyValue, error) {
|
|
kv, err := o.JS.KeyValue(FindingsBucket)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the node-engine's state %s cannot be bound: %w", FindingsBucket, err)
|
|
}
|
|
return kv, nil
|
|
}
|