Skip to content

Commit 63920cf

Browse files
committed
feat: retry AP connection on key exchange failure
1 parent 7be85c0 commit 63920cf

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

ap/ap.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ func (ap *Accesspoint) init(ctx context.Context) (err error) {
8080
return fmt.Errorf("failed initializing diffiehellman: %w", err)
8181
}
8282

83+
// close previous connection if any
84+
if ap.conn != nil {
85+
_ = ap.conn.Close()
86+
ap.conn = nil
87+
}
88+
8389
// open connection to accesspoint
8490
attempts := 0
8591
for {
@@ -182,7 +188,14 @@ func (ap *Accesspoint) Connect(ctx context.Context, creds *pb.LoginCredentials)
182188
ap.connMu.Lock()
183189
defer ap.connMu.Unlock()
184190

185-
return ap.connect(ctx, creds)
191+
return backoff.Retry(func() error {
192+
err := ap.connect(ctx, creds)
193+
if err != nil {
194+
ap.log.WithError(err).Warnf("failed connecting to accesspoint, retrying")
195+
}
196+
197+
return err
198+
}, backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(500*time.Millisecond), 5), ctx))
186199
}
187200

188201
func (ap *Accesspoint) connect(ctx context.Context, creds *pb.LoginCredentials) error {

session/session.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
"encoding/hex"
66
"fmt"
7+
"net/http"
8+
"time"
9+
710
"github.com/devgianlu/go-librespot/events"
811
"github.com/devgianlu/go-librespot/mercury"
912
"github.com/devgianlu/go-librespot/player"
10-
"net/http"
11-
"time"
1213

1314
librespot "github.com/devgianlu/go-librespot"
1415
"github.com/devgianlu/go-librespot/ap"
@@ -93,9 +94,6 @@ func NewSessionFromOptions(ctx context.Context, opts *Options) (*Session, error)
9394
return nil, fmt.Errorf("failed getting accesspoint from resolver: %w", err)
9495
}
9596
s.ap = ap.NewAccesspoint(opts.Log, apAddr, s.deviceId)
96-
if err != nil {
97-
return nil, fmt.Errorf("failed initializing accesspoint: %w", err)
98-
}
9997

10098
// authenticate with the accesspoint using the proper credentials
10199
switch creds := opts.Credentials.(type) {

0 commit comments

Comments
 (0)