Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem when using sp.WithRetry(true) when connected to Home Assistant Spotify Connect #238

Open
jerblack opened this issue Sep 14, 2023 · 0 comments

Comments

@jerblack
Copy link

jerblack commented Sep 14, 2023

I am seeing some weird behavior when I use your package to control one particular Spotify Connect device, specifically an instance of Home Assistant on a Raspberry Pi 4 (w/ HifiBerry DAC2 Pro attached) running the Spotify Connect addon
When I create a client using sp.WithRetry(true) and call a control method like client.Next() while connected to the Home Assistant device, the call never returns and nothing is getting logged, but the client is quite obviously sending the command to the device over and over again because I can hear the tracks skipping and the skipping stops as soon as I kill the process.
When I use the same client with sp.WithRetry(true) but connected to the desktop Spotify app or an Echo device. It works as expected and returns immediately.
When I use another client without sp.WithRetry(true) (option omitted completely) it has no problem controlling the Home Assistant device. It works as expected and returns immediately.

The WithRetry option is described as only retrying on rate limiting errors, but it appears to be doing more than that. Do you know why that might be? Is there any way to get a client to log more about why it's retrying? Thanks.

Here's the sample code I was using to test this.

// send next command to spotify
func main() {
	client := getClient()
	e := client.Next(context.Background())
	if e != nil {
		panic(e)
	}
}
func getClient() *sp.Client {
	auth := spAuth.New(
		spAuth.WithRedirectURL(redirectURI),
		spAuth.WithClientID(clientId),
		spAuth.WithClientSecret(clientSecret),
		spAuth.WithScopes(
			spAuth.ScopeImageUpload, spAuth.ScopePlaylistReadPrivate, spAuth.ScopePlaylistModifyPublic,
			spAuth.ScopePlaylistModifyPrivate, spAuth.ScopePlaylistReadCollaborative, spAuth.ScopeUserFollowModify,
			spAuth.ScopeUserFollowRead, spAuth.ScopeUserLibraryModify, spAuth.ScopeUserLibraryRead,
			spAuth.ScopeUserReadPrivate, spAuth.ScopeUserReadCurrentlyPlaying, spAuth.ScopeUserReadPlaybackState,
			spAuth.ScopeUserModifyPlaybackState, spAuth.ScopeUserReadRecentlyPlayed, spAuth.ScopeUserTopRead,
			spAuth.ScopeStreaming,
		),
	)
	tkn := oauth2.Token{
		TokenType:    "bearer",
		AccessToken:  accessToken,
		RefreshToken: refreshToken,
	}
	expiry, _ := time.Parse(time.UnixDate, tokenExpiration)
	tkn.Expiry = expiry
	// WithRetry says it is for retrying on rate limit errors
	return sp.New(auth.Client(context.Background(), &tkn), sp.WithRetry(true))
	// return sp.New(auth.Client(context.Background(), &tkn))
}

Update: I was able to drop some print statements into your spotify.go to make it print out status codes, and when I am connected to the desktop app or an echo device, the Spotify Web API returns an HTTP 204, but when connected to the Home Assistant device, the API returns an HTTP 202. When I look at your shouldRetry function, I can see that you are marking 202 as a status that should be retried. This doesn't really make sense to me given that HTTP status codes that begin with 2 indicate some variant of success, so I am not sure why you are marking it as needing to be retried. The Spotify documentation indicates that the request was successfully received but is not yet completed.
When I remove that check for http.StatusAccepted from your code, I am able to use a client with sp.WithRetry(true) while connected to the Home Assistant device.

func shouldRetry(status int) bool {
	return status == http.StatusTooManyRequests
	// return status == http.StatusAccepted || status == http.StatusTooManyRequests
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant