1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package participant
import (
"context"
"strings"
"sync"
"time"
exposurev1alpha1 "github.com/johnandersen777/socialweb-computer-kcp/api/exposure/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
market "github.com/johnandersen777/socialweb-computer-kcp/internal/contract/market"
)
type Inputs struct {
Exposures *Index[exposurev1alpha1.Exposure]
Identities *Index[v1alpha1.MarketIdentity]
mu sync.Mutex
next market.Observer
}
var _ market.Observer = (*Inputs)(nil)
func NewInputs() *Inputs {
return &Inputs{Exposures: NewIndex[exposurev1alpha1.Exposure](), Identities: NewIndex[v1alpha1.MarketIdentity]()}
}
func (in *Inputs) Route(next market.Observer) {
in.mu.Lock()
in.next = next
in.mu.Unlock()
}
func (in *Inputs) Observe(ctx context.Context, change market.Change) (time.Duration, error) {
switch change.Kind {
case market.KindExposure:
if change.Gone || change.Exposure == nil {
in.Exposures.Remove(change.Name)
} else {
in.Exposures.Put(change.Name, *change.Exposure)
}
case market.KindMarketIdentity:
key := change.Workspace + "/" + change.Name
if change.Gone || change.MarketIdentity == nil {
in.Identities.Remove(key)
} else {
in.Identities.Put(key, *change.MarketIdentity)
}
}
in.mu.Lock()
next := in.next
in.mu.Unlock()
if next == nil {
return 0, nil
}
return next.Observe(ctx, change)
}
func (in *Inputs) Address(name string) string {
exposure, ok := in.Exposures.Get(strings.TrimSpace(name))
if !ok {
return ""
}
return strings.TrimRight(strings.TrimSpace(exposure.Status.Address), "/")
}
func (in *Inputs) AwaitAddress(ctx context.Context, name string, timeout time.Duration, usable func(string) bool) string {
if usable == nil {
usable = func(raw string) bool { return raw != "" }
}
waitCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
var found string
_ = in.Exposures.Await(waitCtx, func() bool {
found = in.Address(name)
return usable(found)
})
if !usable(found) {
return ""
}
return found
}
func (in *Inputs) Identity(workspace, name string) (v1alpha1.MarketIdentity, bool) {
return in.Identities.Get(workspace + "/" + strings.TrimSpace(name))
}
func (in *Inputs) AwaitIdentity(ctx context.Context, workspaces []string, name string, timeout time.Duration) (v1alpha1.MarketIdentity, bool) {
waitCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
var found v1alpha1.MarketIdentity
ok := false
_ = in.Identities.Await(waitCtx, func() bool {
for _, ws := range workspaces {
if id, has := in.Identity(ws, name); has {
found, ok = id, true
return true
}
}
return false
})
return found, ok
}