error instead of panic'ing when opening subscription.
This commit is contained in:
8
relay.go
8
relay.go
@@ -377,6 +377,10 @@ func (r *Relay) publish(ctx context.Context, id string, env Envelope) error {
|
|||||||
func (r *Relay) Subscribe(ctx context.Context, filters Filters, opts ...SubscriptionOption) (*Subscription, error) {
|
func (r *Relay) Subscribe(ctx context.Context, filters Filters, opts ...SubscriptionOption) (*Subscription, error) {
|
||||||
sub := r.PrepareSubscription(ctx, filters, opts...)
|
sub := r.PrepareSubscription(ctx, filters, opts...)
|
||||||
|
|
||||||
|
if r.Connection == nil {
|
||||||
|
return nil, fmt.Errorf("not connected to %s", r.URL)
|
||||||
|
}
|
||||||
|
|
||||||
if err := sub.Fire(); err != nil {
|
if err := sub.Fire(); err != nil {
|
||||||
return nil, fmt.Errorf("couldn't subscribe to %v at %s: %w", filters, r.URL, err)
|
return nil, fmt.Errorf("couldn't subscribe to %v at %s: %w", filters, r.URL, err)
|
||||||
}
|
}
|
||||||
@@ -389,10 +393,6 @@ func (r *Relay) Subscribe(ctx context.Context, filters Filters, opts ...Subscrip
|
|||||||
// Remember to cancel subscriptions, either by calling `.Unsub()` on them or ensuring their `context.Context` will be canceled at some point.
|
// Remember to cancel subscriptions, either by calling `.Unsub()` on them or ensuring their `context.Context` will be canceled at some point.
|
||||||
// Failure to do that will result in a huge number of halted goroutines being created.
|
// Failure to do that will result in a huge number of halted goroutines being created.
|
||||||
func (r *Relay) PrepareSubscription(ctx context.Context, filters Filters, opts ...SubscriptionOption) *Subscription {
|
func (r *Relay) PrepareSubscription(ctx context.Context, filters Filters, opts ...SubscriptionOption) *Subscription {
|
||||||
if r.Connection == nil {
|
|
||||||
panic(fmt.Errorf("must call .Connect() first before calling .Subscribe()"))
|
|
||||||
}
|
|
||||||
|
|
||||||
current := subscriptionIDCounter.Add(1)
|
current := subscriptionIDCounter.Add(1)
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user