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

API context propagation #85

Merged
merged 6 commits into from
Jan 14, 2025
Merged

API context propagation #85

merged 6 commits into from
Jan 14, 2025

Conversation

Lenart12
Copy link
Contributor

@Lenart12 Lenart12 commented Jan 13, 2025

This pull request includes several changes to improve context handling and (and little logging). The most important changes include adding context propagation to API responses, introducing a logging function to show request details, and simplifying error handling in BLE control operations.

This PR will most importantly allow stopping command execution quickly if request is canceled. This will speed up queue processingby quite a bit if there is a lot of canceled requests and so prevent the queue from growing.

This change will allow executing commands to be canceled right away if request is canceled.
@Lenart12
Copy link
Contributor Author

Lenart12 commented Jan 13, 2025

Small demo :^)

Screen.Recording.2025-01-13.145900.mp4

Copy link
Owner

@wimaha wimaha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!

What is with following commands reusing the same connection? The cancle would not work for them.

internal/ble/control/control.go Outdated Show resolved Hide resolved
…onse

And not always, even if response is missing
@Lenart12
Copy link
Contributor Author

Context is based for each command on its own. If first command succeedes inside its allowed deadline, it will call operateConnection and then all following commands within it have its own context as it calls ExecuteCommand and inside that function the context is unpacked from apiResponse.

var ctx context.Context
if command.Response != nil && command.Response.Ctx != nil {
ctx = command.Response.Ctx
} else {
log.Debug("no context provided, using default", "command", command.Command, "body", command.Body)
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
}

@wimaha
Copy link
Owner

wimaha commented Jan 14, 2025

Thanks, the last Commit was very important.
I will approve the PR and will push a new version to the dev tag. Since I'm from now on 4 weeks away I don't want to push an update with so many changes because I can't change anything for 4 weeks.

Hope this is fine with you.

@wimaha wimaha merged commit 954dea0 into wimaha:main Jan 14, 2025
2 checks passed
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

Successfully merging this pull request may close these issues.

2 participants