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

[Diagnostics] Refactor diagnostics track methods to handle background work automatically #4270

Open
wants to merge 1 commit into
base: diagnostics-add-apple-products-request-event
Choose a base branch
from

Conversation

tonidero
Copy link
Contributor

Description

Before this, callers of the diagnostics track methods had to handle concurrency and offloading the work to a background thread. This moves the work inside the DiagnosticsTracker, which makes the methods non-async anymore.

}

semaphore.wait()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to do this for this to work as expected and wait for the completion of the block. It's very hacky, but I think it's what we wanted? Would appreciate feedback to make sure this is ok!

Copy link
Contributor

@jamesrb1 jamesrb1 Sep 12, 2024

Choose a reason for hiding this comment

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

Before it would not wait for block() to run before returning, but with this change it does. Is that desired? If so, this can be done by calling await block() without anything else, no need for the semaphore/Task.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe we can't do await block() or await Task because we're inside a non-async function right? We would need to make this the operation dispatcher async, but the whole point of this object is to dispatch tasks in non-async contexts I believe.

Lmk if you want to chat more about this! I could have missed something! Note that this semaphore is only for tests too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh sheesh, I saw the async on the block's signature and misread it as the function's. Semaphore is the way to go!

await self.clearDiagnosticsFileIfTooBig()
await self.diagnosticsFileHandler.appendEvent(diagnosticsEvent: event)
func track(_ event: DiagnosticsEvent) {
self.diagnosticsDispatcher.dispatchOnWorkerThread {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the main change, now dispatching to a background thread happens internally.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need a diagnosticsDispatcher? You could just create a Task here,

func track(_ event: DiagnosticsEvent) {
    Task {
        await self.clearDiagnosticsFileIfTooBig()
        await self.diagnosticsFileHandler.appendEvent(diagnosticsEvent: event)
    }
}

However, with this change, events submitted to track() aren't guaranteed to run sequentially as they are with the OperationDispatcher. But this guarantee won't have existed before, so unless you want to make that change, I think using Task is the way to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are 2 main reasons I decided to go with the OperationDispatcher:

  • First, as you said, this will make the operations run sequentially, which I believe would be safer, since there could be chances of changing contexts between tracking different events, and if we reach the file too big issue, we might be missing extra data. As you said, this problem would be present right now.
  • Second, I wanted to be able to test this class. If I used Task, I don't have a good way to determine when the tracking finished to do the assertions. With OperationDispatcher, we can pass a MockOperationDispatcher in the tests that runs things synchronously (mostly), allowing us to do the assertions immediately.

Lmk if you would like to talk more about it!

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, sounds good!

@tonidero tonidero marked this pull request as ready for review September 12, 2024 12:27
@tonidero tonidero requested a review from a team September 12, 2024 12:27
@tonidero tonidero force-pushed the diagnostics-add-apple-products-request-event branch from 29c87ec to 818c18a Compare September 12, 2024 12:52
@tonidero tonidero requested a review from a team September 16, 2024 08:21
Copy link
Member

@JayShortway JayShortway left a comment

Choose a reason for hiding this comment

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

Nice solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants