-
Notifications
You must be signed in to change notification settings - Fork 24
Make DNSSD queries cancellable #34
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
610463d
a6299e5
de2fd16
8be4b7d
472d5c0
6ad9bd9
8102fb4
792d8e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,10 @@ struct DNSSD { | |
let serviceRefPtr = UnsafeMutablePointer<DNSServiceRef?>.allocate(capacity: 1) | ||
defer { serviceRefPtr.deallocate() } | ||
|
||
continuation.onTermination = { _ in | ||
DNSServiceRefDeallocate(serviceRefPtr.pointee) | ||
} | ||
|
||
// Run the query | ||
let _code = DNSServiceQueryRecord( | ||
serviceRefPtr, | ||
|
@@ -158,9 +162,32 @@ struct DNSSD { | |
return continuation.finish(throwing: AsyncDNSResolver.Error(dnssdCode: _code)) | ||
} | ||
|
||
let serviceSockFD = DNSServiceRefSockFD(serviceRefPtr.pointee) | ||
guard serviceSockFD != -1 else { | ||
return continuation.finish(throwing: AsyncDNSResolver.Error(code: .internalError)) | ||
} | ||
|
||
var pollFDs = [pollfd(fd: serviceSockFD, events: Int16(POLLIN), revents: 0)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We did work to remove the SwiftNIO dependency earlier (#20), so I don't think we want to add it back. The intention here is to allow checking for task cancellation, because calling The docs for
That's why we went with this approach here. It doesn't sound like we must do async I/O to see improvement, but I could be wrong of course. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
^^ that's exactly what NIO can do for you. What's the goal with removing the NIO dependency? Every adopter will have a NIO dependency anyway, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Only a couple of methods on
Not necessarily although I'm sure many will. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We are passing
I wonder if it should sleep a bit before calling
Is there an example that we can follow? I found these threads:
But I didn't find anything obvious. I don't doubt that SwiftNIO can do this more efficiently, but at the end of the day we need to look at the trade-offs. Does doing it without SwiftNIO give us a "good enough" solution? Is it worthwhile to add SwiftNIO, which is a heavier dependency, for this? (I don't have problem with adding the SwiftNIO dependency FWIW.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hot-looping on poll is not the right choice: it's not meaningfully different than just sleeping the thread, as we'll never yield it. Doing this with NIO directly is gonna be a little painful as you'd have to write a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yes, if you don't actually need to do anything with the data in there, then DispacthSource is good. It's pretty bad if you need to actually read the data but if you just need to be told that there is something to read, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your feedback, I'm going to try implementing this with DispatchSource then, if it's ok for everyone. |
||
while true { | ||
guard !Task.isCancelled else { | ||
return continuation.finish(throwing: CancellationError()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would be more useful to throw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I thought that CancellationError was the standard way to throw when a task is cancelled, but apparently it's not a guarantee and an AsyncDNSResolver.Error might be more specific and useful. |
||
} | ||
|
||
let result = poll(&pollFDs, 1, 0) | ||
guard result != -1 else { | ||
return continuation.finish(throwing: AsyncDNSResolver.Error(code: .internalError)) | ||
victorherrerod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if result == 0 { | ||
continue | ||
} | ||
if result == 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it always 1 and not any positive number? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to I can change it to |
||
break | ||
} | ||
} | ||
|
||
// Read reply from the socket (blocking) then call reply handler | ||
DNSServiceProcessResult(serviceRefPtr.pointee) | ||
DNSServiceRefDeallocate(serviceRefPtr.pointee) | ||
yim-lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Streaming done | ||
continuation.finish() | ||
victorherrerod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
Uh oh!
There was an error while loading. Please reload this page.