get rid of mutexes and use a single loop to prevent races.
in the meantime change the API to makes a little less error-prone.
This commit is contained in:
@@ -11,16 +11,22 @@ type Subscription struct {
|
||||
label string
|
||||
counter int
|
||||
conn *Connection
|
||||
mutex sync.Mutex
|
||||
|
||||
Relay *Relay
|
||||
Filters Filters
|
||||
Events chan *Event
|
||||
Relay *Relay
|
||||
Filters Filters
|
||||
|
||||
// the Events channel emits all EVENTs that come in a Subscription
|
||||
// will be closed when the subscription ends
|
||||
Events chan *Event
|
||||
|
||||
// the EndOfStoredEvents channel gets closed when an EOSE comes for that subscription
|
||||
EndOfStoredEvents chan struct{}
|
||||
Context context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
stopped bool
|
||||
// Context will be .Done() when the subscription ends
|
||||
Context context.Context
|
||||
|
||||
live bool
|
||||
cancel context.CancelFunc
|
||||
emitEose sync.Once
|
||||
}
|
||||
|
||||
@@ -29,35 +35,43 @@ type EventMessage struct {
|
||||
Relay string
|
||||
}
|
||||
|
||||
// SetLabel puts a label on the subscription that is prepended to the id that is sent to relays,
|
||||
//
|
||||
// it's only useful for debugging and sanity purposes.
|
||||
func (sub *Subscription) SetLabel(label string) {
|
||||
sub.label = label
|
||||
// When instantiating relay connections, some options may be passed.
|
||||
// SubscriptionOption is the type of the argument passed for that.
|
||||
// Some examples are WithLabel.
|
||||
type SubscriptionOption interface {
|
||||
IsSubscriptionOption()
|
||||
}
|
||||
|
||||
// GetID return the Nostr subscription ID as given to the relay, it will be a sequential number, stringified.
|
||||
// WithLabel puts a label on the subscription (it is prepended to the automatic id) that is sent to relays.
|
||||
type WithLabel string
|
||||
|
||||
func (_ WithLabel) IsSubscriptionOption() {}
|
||||
|
||||
var _ SubscriptionOption = (WithLabel)("")
|
||||
|
||||
// GetID return the Nostr subscription ID as given to the Relay
|
||||
// it is a concatenation of the label and a serial number.
|
||||
func (sub *Subscription) GetID() string {
|
||||
return sub.label + ":" + strconv.Itoa(sub.counter)
|
||||
}
|
||||
|
||||
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
|
||||
// Unsub() also closes the channel sub.Events.
|
||||
// Unsub() also closes the channel sub.Events and makes a new one.
|
||||
func (sub *Subscription) Unsub() {
|
||||
sub.mutex.Lock()
|
||||
defer sub.mutex.Unlock()
|
||||
|
||||
id := sub.GetID()
|
||||
closeMsg := CloseEnvelope(id)
|
||||
closeb, _ := (&closeMsg).MarshalJSON()
|
||||
debugLog("{%s} sending %v", sub.Relay.URL, closeb)
|
||||
sub.conn.WriteMessage(closeb)
|
||||
|
||||
if sub.Relay.IsConnected() {
|
||||
closeMsg := CloseEnvelope(id)
|
||||
closeb, _ := (&closeMsg).MarshalJSON()
|
||||
debugLog("{%s} sending %v", sub.Relay.URL, closeb)
|
||||
sub.Relay.Write(closeb)
|
||||
}
|
||||
|
||||
sub.live = false
|
||||
sub.Relay.Subscriptions.Delete(id)
|
||||
|
||||
if !sub.stopped && sub.Events != nil {
|
||||
close(sub.Events)
|
||||
}
|
||||
sub.stopped = true
|
||||
// do this so we don't have the possibility of closing the Events channel and then trying to send to it
|
||||
sub.Relay.subscriptionChannelCloseQueue <- sub
|
||||
}
|
||||
|
||||
// Sub sets sub.Filters and then calls sub.Fire(ctx).
|
||||
@@ -70,28 +84,15 @@ func (sub *Subscription) Sub(ctx context.Context, filters Filters) {
|
||||
// Fire sends the "REQ" command to the relay.
|
||||
func (sub *Subscription) Fire() error {
|
||||
id := sub.GetID()
|
||||
sub.Relay.Subscriptions.Store(id, sub)
|
||||
|
||||
reqb, _ := ReqEnvelope{id, sub.Filters}.MarshalJSON()
|
||||
debugLog("{%s} sending %v", sub.Relay.URL, reqb)
|
||||
if err := sub.conn.WriteMessage(reqb); err != nil {
|
||||
|
||||
sub.live = true
|
||||
if err := sub.Relay.Write(reqb); err != nil {
|
||||
sub.cancel()
|
||||
return fmt.Errorf("failed to write: %w", err)
|
||||
}
|
||||
|
||||
// the subscription ends once the context is canceled
|
||||
go func() {
|
||||
<-sub.Context.Done()
|
||||
sub.Unsub()
|
||||
}()
|
||||
|
||||
// or when the relay connection is closed
|
||||
go func() {
|
||||
<-sub.Relay.connectionContext.Done()
|
||||
|
||||
// cancel the context -- this will cause the other context cancelation cause above to be called
|
||||
sub.cancel()
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user