Files
mesh-host/internal/link/ownname.go
T
jschoubben ac3422f914 H-17: the node-engine's own name is read from its applied record or first composing
Discovery, the finding writer and the vault login names read it; no fixed name.

Builds: design 47 G §527, §634 (A-149)
2026-11-12 04:58:28 +01:00

62 lines
1.5 KiB
Go

package link
import (
"context"
"errors"
"sync"
)
// OwnName is the module that is this node-engine: the module whose catalog entry claims the seat node-engine, which the
// engine learns first from its applied record of the build whose fingerprint is its own and, on a first start with no such
// record, from its first composing (design 47 G §527, §634, A-149). Its discovery, its finding writer and its login names
// in the vault read it here and fix none.
type OwnName struct {
mu sync.Mutex
name string
ready chan struct{}
}
// NewOwnName makes an own name not yet known.
func NewOwnName() *OwnName { return &OwnName{ready: make(chan struct{})} }
// Set keeps the name; an empty one changes nothing. The first name opens Wait for everyone.
func (o *OwnName) Set(name string) {
if o == nil || name == "" {
return
}
o.mu.Lock()
defer o.mu.Unlock()
if o.name == "" {
close(o.ready)
}
o.name = name
}
// Get is the name, "" while it is not known.
func (o *OwnName) Get() string {
if o == nil {
return ""
}
o.mu.Lock()
defer o.mu.Unlock()
return o.name
}
// Wait returns the name once it is known, or ctx's error.
func (o *OwnName) Wait(ctx context.Context) (string, error) {
if o == nil {
return "", errors.New("this session names no own module")
}
select {
case <-o.ready:
return o.Get(), nil
case <-ctx.Done():
return "", ctx.Err()
}
}
// OwnNamed is a link that reads the node-engine's own module from an OwnName when it serves its seat.
type OwnNamed interface {
UseOwnName(*OwnName)
}