-
Notifications
You must be signed in to change notification settings - Fork 23
refactor: UdpSocketController to customized UdpSocket #728
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
Open
huster-zhangpeng
wants to merge
1
commit into
main
Choose a base branch
from
refactor/usc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,30 +8,30 @@ use std::{ | |
| use smallvec::SmallVec; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct WakerVec<const N: usize = 4> { | ||
| pub struct WakerGroup<const N: usize = 4> { | ||
| wakers: SmallVec<[Waker; N]>, | ||
| } | ||
|
|
||
| impl<const N: usize> Default for WakerVec<N> { | ||
| impl<const N: usize> Default for WakerGroup<N> { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl<const N: usize> WakerVec<N> { | ||
| impl<const N: usize> WakerGroup<N> { | ||
| pub const fn new() -> Self { | ||
| Self { | ||
| wakers: SmallVec::new_const(), | ||
| } | ||
| } | ||
|
|
||
| pub fn register(&mut self, waker: &Waker) { | ||
| pub fn add(&mut self, waker: &Waker) { | ||
| if !self.wakers.iter().any(|w| w.will_wake(waker)) { | ||
| self.wakers.push(waker.clone()); | ||
| } | ||
| } | ||
|
|
||
| pub fn unregister(&mut self, waker: &Waker) { | ||
| pub fn remove(&mut self, waker: &Waker) { | ||
| self.wakers | ||
| .retain(|registered| !registered.will_wake(waker)); | ||
| } | ||
|
|
@@ -43,15 +43,15 @@ impl<const N: usize> WakerVec<N> { | |
| } | ||
| } | ||
|
|
||
| impl<const N: usize> Drop for WakerVec<N> { | ||
| impl<const N: usize> Drop for WakerGroup<N> { | ||
| fn drop(&mut self) { | ||
| self.wake_all(); | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Wakers<const N: usize = 4> { | ||
| wakers: Mutex<WakerVec<N>>, | ||
| inner: Mutex<WakerGroup<N>>, | ||
| } | ||
|
|
||
| impl<const N: usize> Wake for Wakers<N> { | ||
|
|
@@ -73,24 +73,30 @@ impl<const N: usize> Default for Wakers<N> { | |
| impl<const N: usize> Wakers<N> { | ||
| pub const fn new() -> Self { | ||
| Self { | ||
| wakers: Mutex::new(WakerVec::new()), | ||
| inner: Mutex::new(WakerGroup::new()), | ||
| } | ||
| } | ||
|
|
||
| fn lock(&self) -> MutexGuard<'_, WakerVec<N>> { | ||
| self.wakers.lock().expect("Wakers mutex poisoned") | ||
| fn lock_guard(&self) -> MutexGuard<'_, WakerGroup<N>> { | ||
| self.inner.lock().expect("Wakers mutex poisoned") | ||
| } | ||
|
|
||
| pub fn register(&self, waker: &Waker) { | ||
| self.lock().register(waker) | ||
| pub fn add(&self, waker: &Waker) { | ||
| self.lock_guard().add(waker) | ||
| } | ||
|
|
||
| pub fn unregister(&self, waker: &Waker) { | ||
| self.lock().unregister(waker) | ||
| pub fn remove(&self, waker: &Waker) { | ||
| self.lock_guard().remove(waker) | ||
| } | ||
|
|
||
| pub fn together_with(self: &Arc<Self>, waker: &Waker) -> Waker { | ||
| let mut guard = self.lock_guard(); | ||
| guard.add(waker); | ||
| Waker::from(self.clone()) | ||
| } | ||
|
|
||
| pub fn wake_all(&self) { | ||
| { mem::replace(&mut *self.lock(), WakerVec::new()) }.wake_all() | ||
| { mem::replace(&mut *self.lock_guard(), WakerGroup::new()) }.wake_all() | ||
| } | ||
|
|
||
| pub fn to_waker(self: &Arc<Self>) -> Waker { | ||
|
|
@@ -102,10 +108,10 @@ impl<const N: usize> Wakers<N> { | |
| cx: &mut Context<'_>, | ||
| poll: impl FnOnce(&mut Context<'_>) -> Poll<T>, | ||
| ) -> Poll<T> { | ||
| self.register(cx.waker()); | ||
| self.add(cx.waker()); | ||
| let result = poll(&mut Context::from_waker(&self.to_waker())); | ||
|
Contributor
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. 这里怎么不用together_with
Contributor
Author
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. 这里的 combine 函数名,不太像是 Wakers 的内在属性,我其实想换成面向过程的,不太喜欢这个函数名,在对比两种写法 |
||
| if result.is_ready() { | ||
| self.unregister(cx.waker()); | ||
| self.remove(cx.waker()); | ||
| } | ||
| result | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个API很容易用错
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
很容易直接together_with得到Waker然后构造Context传进poll函数
但是后来发现这一个流程是有问题的,在poll操作直接ready而没有pending -> wake -> ready 流程时,会导致有一个waker残留在里面
对于几乎总是直接ready的API(poll send似乎就是此类), 这个问题造成的影响尤为显著