oops: stop replacing connectionContext with Connect(ctx).

This commit is contained in:
fiatjaf
2023-05-10 09:07:51 -03:00
parent 9dc674bc02
commit 0c06788672

View File

@@ -84,12 +84,13 @@ func (r *Relay) Context() context.Context { return r.connectionContext }
// pass a custom context to the underlying relay connection, use NewRelay() and // pass a custom context to the underlying relay connection, use NewRelay() and
// then Relay.Connect(). // then Relay.Connect().
func (r *Relay) Connect(ctx context.Context) error { func (r *Relay) Connect(ctx context.Context) error {
connectionContext, cancel := context.WithCancel(ctx) if r.connectionContext == nil {
r.connectionContext = connectionContext connectionContext, cancel := context.WithCancel(context.Background())
r.connectionContextCancel = cancel r.connectionContext = connectionContext
r.connectionContextCancel = cancel
}
if r.URL == "" { if r.URL == "" {
cancel()
return fmt.Errorf("invalid relay URL '%s'", r.URL) return fmt.Errorf("invalid relay URL '%s'", r.URL)
} }
@@ -102,7 +103,6 @@ func (r *Relay) Connect(ctx context.Context) error {
conn, err := NewConnection(ctx, r.URL, r.RequestHeader) conn, err := NewConnection(ctx, r.URL, r.RequestHeader)
if err != nil { if err != nil {
cancel()
return fmt.Errorf("error opening websocket to '%s': %w", r.URL, err) return fmt.Errorf("error opening websocket to '%s': %w", r.URL, err)
} }
r.Connection = conn r.Connection = conn
@@ -123,7 +123,6 @@ func (r *Relay) Connect(ctx context.Context) error {
go func() { go func() {
ticker := time.NewTicker(29 * time.Second) ticker := time.NewTicker(29 * time.Second)
defer ticker.Stop() defer ticker.Stop()
defer cancel()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
@@ -138,7 +137,6 @@ func (r *Relay) Connect(ctx context.Context) error {
// handling received messages // handling received messages
go func() { go func() {
defer cancel()
for { for {
message, err := conn.ReadMessage(r.connectionContext) message, err := conn.ReadMessage(r.connectionContext)
if err != nil { if err != nil {