forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
relaypool.go
230 lines (189 loc) · 5.45 KB
/
relaypool.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package nostr
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
s "github.com/SaveTheRbtz/generic-sync-map-go"
)
type PublishStatus struct {
Relay string
Status Status
}
type RelayPool struct {
SecretKey *string
Policies s.MapOf[string, RelayPoolPolicy]
Relays s.MapOf[string, *Relay]
subscriptions s.MapOf[string, Filters]
eventStreams s.MapOf[string, chan EventMessage]
Notices chan *NoticeMessage
}
type RelayPoolPolicy interface {
ShouldRead(Filters) bool
ShouldWrite(*Event) bool
}
type SimplePolicy struct {
Read bool
Write bool
}
func (s SimplePolicy) ShouldRead(_ Filters) bool {
return s.Read
}
func (s SimplePolicy) ShouldWrite(_ *Event) bool {
return s.Write
}
type NoticeMessage struct {
Message string
Relay string
}
// New creates a new RelayPool with no relays in it
func NewRelayPool() *RelayPool {
return &RelayPool{
Policies: s.MapOf[string, RelayPoolPolicy]{},
Relays: s.MapOf[string, *Relay]{},
Notices: make(chan *NoticeMessage),
}
}
// Add calls AddContext with background context in a separate goroutine, sending
// any connection error over the returned channel.
//
// The returned channel is closed once the connection is successfully
// established or RelayConnectContext returned an error.
func (r *RelayPool) Add(url string, policy RelayPoolPolicy) <-chan error {
cherr := make(chan error)
go func() {
defer close(cherr)
if err := r.AddContext(context.Background(), url, policy); err != nil {
cherr <- err
}
}()
return cherr
}
// AddContext connects to a relay at a canonical version specified by the url
// and adds it to the pool. The returned error is non-nil only on connection
// errors, including an expired context before the connection is complete.
//
// Once successfully connected, AddContext returns and the context expiration
// has no effect: call r.Remove to close the connection and delete a relay from the pool.
func (r *RelayPool) AddContext(ctx context.Context, url string, policy RelayPoolPolicy) error {
relay, err := RelayConnectContext(ctx, url)
if err != nil {
return fmt.Errorf("failed to connect to %s: %w", url, err)
}
if policy == nil {
policy = SimplePolicy{Read: true, Write: true}
}
r.addConnected(relay, policy)
return nil
}
func (r *RelayPool) addConnected(relay *Relay, policy RelayPoolPolicy) {
r.Policies.Store(relay.URL, policy)
r.Relays.Store(relay.URL, relay)
r.subscriptions.Range(func(id string, filters Filters) bool {
sub := relay.prepareSubscription(id)
sub.Sub(filters)
eventStream, _ := r.eventStreams.Load(id)
go func(sub *Subscription) {
for evt := range sub.Events {
eventStream <- EventMessage{Relay: relay.URL, Event: evt}
eventStream <- EventMessage{Relay: relay.URL, Event: evt}
}
}(sub)
return true
})
}
// Remove removes a relay from the pool.
func (r *RelayPool) Remove(url string) {
nm := NormalizeURL(url)
r.Relays.Delete(nm)
r.Policies.Delete(nm)
if relay, ok := r.Relays.Load(nm); ok {
relay.Close()
}
}
//Sub subscribes to events matching the passed filters and returns the subscription ID,
//a channel which you should pass into Unique to get unique events, and a function which
//you should call to clean up and close your subscription so that the relay doesn't block you.
func (r *RelayPool) Sub(filters Filters) (subID string, events chan EventMessage, unsubscribe func()) {
random := make([]byte, 7)
rand.Read(random)
id := hex.EncodeToString(random)
r.subscriptions.Store(id, filters)
eventStream := make(chan EventMessage)
r.eventStreams.Store(id, eventStream)
unsub := make(chan struct{})
r.Relays.Range(func(_ string, relay *Relay) bool {
sub := relay.prepareSubscription(id)
sub.Sub(filters)
go func(sub *Subscription) {
for evt := range sub.Events {
eventStream <- EventMessage{Relay: relay.URL, Event: evt}
}
}(sub)
go func() {
select {
case <-unsub:
sub.Unsub()
}
}()
return true
})
return id, eventStream, func() { gracefulClose(unsub) }
}
func gracefulClose(c chan struct{}) {
select {
case <-c:
default:
close(c)
}
}
func Unique(all chan EventMessage) chan Event {
uniqueEvents := make(chan Event)
emittedAlready := s.MapOf[string, struct{}]{}
go func() {
for eventMessage := range all {
if _, ok := emittedAlready.LoadOrStore(eventMessage.Event.ID, struct{}{}); !ok {
uniqueEvents <- eventMessage.Event
}
}
}()
return uniqueEvents
}
func (r *RelayPool) PublishEvent(evt *Event) (*Event, chan PublishStatus, error) {
size := 0
r.Relays.Range(func(_ string, _ *Relay) bool {
size++
return true
})
status := make(chan PublishStatus, size)
if r.SecretKey == nil && (evt.PubKey == "" || evt.Sig == "") {
return nil, status, errors.New("PublishEvent needs either a signed event to publish or to have been configured with a .SecretKey.")
}
if evt.PubKey == "" {
sk, err := GetPublicKey(*r.SecretKey)
if err != nil {
return nil, status, fmt.Errorf("The pool's global SecretKey is invalid: %w", err)
}
evt.PubKey = sk
}
if evt.Sig == "" {
err := evt.Sign(*r.SecretKey)
if err != nil {
return nil, status, fmt.Errorf("Error signing event: %w", err)
}
}
r.Relays.Range(func(url string, relay *Relay) bool {
if r, ok := r.Policies.Load(url); !ok || !r.ShouldWrite(evt) {
return true
}
relay.MustPublish(*evt)
//go func(relay *Relay) {
// for resultStatus := range relay.Publish(*evt) {
// status <- PublishStatus{relay.URL, resultStatus}
// }
//}(relay)
return true
})
return evt, status, nil
}