Files
mesh-delivery/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/bound.go
T
jschoubben 103174e44c FB2: the forge bridge says the finding branch-unprotected
A move of a watched branch whose protection was removed writes nothing and is said as the
finding branch-unprotected (scope merge, id the repository and branch as a key, warning) through
mesh-sdk's held finding writer into the bound state findings; a move that finds the protection
back clears it. The findings state is bound by the Binding with performed.

Builds: design 47 G §17, §131, §387, A-251
Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 09:32:36 +01:00

117 lines
3.5 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"sync"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-sdk/go/busretry"
"git.novox.be/novox/mesh-sdk/go/process"
)
// brokerSeat is the seat whose verb `show` answers a `requires` entry's bus names, and whose `recreated` and `dropped`
// say it changed (design 47 G §577): the manifest names `seat:mesh-broker.show` in `invokes` and the two events in
// `consumes`.
const brokerSeat = "mesh-broker"
// performedLocal is the local name of the state performed in the manifest's `requires`.
const performedLocal = "performed"
// keptStore is the part of a key-value bucket the bridge's stores use.
type keptStore interface {
Get(ctx context.Context, key string) (jetstream.KeyValueEntry, error)
Put(ctx context.Context, key string, value []byte) (uint64, error)
PutString(ctx context.Context, key string, value string) (uint64, error)
Delete(ctx context.Context, key string, opts ...jetstream.KVDeleteOpt) error
}
// boundBucket is a `requires` entry's bucket opened by its binding, never by an old name (design 47 G §577): each use
// asks the Binding for the bucket's bus name and opens it, again after `show` answers another name (a recreated entry).
// While `show` has not answered, every use fails with the Binding's error naming the entry, so nothing that needs it is
// done.
type boundBucket struct {
binding *process.Binding
local string
js process.BucketOpener
mu sync.Mutex
name string
kv jetstream.KeyValue
}
func (s *boundBucket) open(ctx context.Context) (jetstream.KeyValue, error) {
names, err := s.binding.Names(s.local)
if err != nil {
return nil, err
}
if names.Bucket == "" {
return nil, fmt.Errorf("the bus entry %q is bound to no bucket", s.local)
}
s.mu.Lock()
defer s.mu.Unlock()
if s.kv != nil && s.name == names.Bucket {
return s.kv, nil
}
kv, err := s.js.KeyValue(ctx, names.Bucket)
if err != nil {
return nil, fmt.Errorf("the bus entry %q, bucket %s: %w", s.local, names.Bucket, err)
}
s.name, s.kv = names.Bucket, kv
return kv, nil
}
func (s *boundBucket) Get(ctx context.Context, key string) (jetstream.KeyValueEntry, error) {
kv, err := s.open(ctx)
if err != nil {
return nil, err
}
return kv.Get(ctx, key)
}
func (s *boundBucket) Put(ctx context.Context, key string, value []byte) (uint64, error) {
kv, err := s.open(ctx)
if err != nil {
return 0, err
}
return kv.Put(ctx, key, value)
}
func (s *boundBucket) PutString(ctx context.Context, key string, value string) (uint64, error) {
return s.Put(ctx, key, []byte(value))
}
func (s *boundBucket) Delete(ctx context.Context, key string, opts ...jetstream.KVDeleteOpt) error {
kv, err := s.open(ctx)
if err != nil {
return err
}
return kv.Delete(ctx, key, opts...)
}
// Keys are the bucket's keys, sorted; none is an empty list.
func (s *boundBucket) Keys(ctx context.Context) ([]string, error) {
kv, err := s.open(ctx)
if err != nil {
return nil, err
}
keys, err := kv.Keys(ctx)
if errors.Is(err, jetstream.ErrNoKeysFound) {
return []string{}, nil
}
sort.Strings(keys)
return keys, err
}
// keptCallOf is call as a kept call (ADR 0668 §1): asked again through a timeout or no responders until answered or
// busretry.Bound has passed.
func keptCallOf(ctx context.Context, call caller) func(key string, body any) (json.RawMessage, error) {
return func(key string, body any) (json.RawMessage, error) {
return busretry.DoRepeatable("the call "+key, func() (json.RawMessage, error) { return call(ctx, key, body) }, nil)
}
}