refactor: UdpSocketController to customized UdpSocket#728
refactor: UdpSocketController to customized UdpSocket#728huster-zhangpeng wants to merge 1 commit into
Conversation
010e8a4 to
003583e
Compare
Codecov Report❌ Patch coverage is
... and 7 files with indirect coverage changes 🚀 New features to boost your workflow:
|
| self.lock_guard().remove(waker) | ||
| } | ||
|
|
||
| pub fn together_with(self: &Arc<Self>, waker: &Waker) -> Waker { |
There was a problem hiding this comment.
很容易直接together_with得到Waker然后构造Context传进poll函数
但是后来发现这一个流程是有问题的,在poll操作直接ready而没有pending -> wake -> ready 流程时,会导致有一个waker残留在里面
对于几乎总是直接ready的API(poll send似乎就是此类), 这个问题造成的影响尤为显著
| ) -> Poll<T> { | ||
| self.register(cx.waker()); | ||
| self.add(cx.waker()); | ||
| let result = poll(&mut Context::from_waker(&self.to_waker())); |
There was a problem hiding this comment.
这里的 combine 函数名,不太像是 Wakers 的内在属性,我其实想换成面向过程的,不太喜欢这个函数名,在对比两种写法
| transactions: DashMap<stun::TransactionId, ArcReceiving<stun::Response>>, | ||
| // ── NAT feature ───────────────────────────────────────────────────── | ||
| /// Per-socket NAT type, resolved asynchronously by the NAT probe task. | ||
| nat_features: DashMap<Fd, SetOnce<NetFeature>>, |
There was a problem hiding this comment.
多agent可能给出不一样的nat feautre, 如何处理
There was a problem hiding this comment.
SetOnce而不是Arc, 这会使得异步等待会持有guard, 导致死锁
dashmap的guard虽然Send也Sync, 但是这往往会导致用错,导致死锁
There was a problem hiding this comment.
这个文件,很久之前的,暂时还没在 lib.rs 中引用,这是 qinterface 重构的早期,qinterface 逻辑乱着呢,距离完成早着呢,可以先忽略它
| /// Per-socket NAT type, resolved asynchronously by the NAT probe task. | ||
| nat_features: DashMap<Fd, SetOnce<NetFeature>>, | ||
| /// Externally-visible addresses discovered by agents. | ||
| endpoint_addrs: DashMap<Fd, HashMap<SocketAddr, Arc<SetOnce<SocketAddr>>>>, |
There was a problem hiding this comment.
why not "Vec because the number of agents per socket is typically tiny."
There was a problem hiding this comment.
今天下午 review 出的结构更好,这里确实不妥。整体都是雏形,此文件没参与编译,所以 CI 才过了,别紧张
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub struct Fd(usize); | ||
|
|
||
| static FD_COUNTER: AtomicUsize = AtomicUsize::new(0); |
There was a problem hiding this comment.
一般这种逻辑的唯一ID生成器产出的都是从1开始的,如果某次生成取到了0就说明发生了溢出,生成的ID将不再是唯一的。这个没考虑到
| stun_addr: agent_addr, | ||
| }); | ||
| let mut agents = self.agents.entry(fd).or_default(); | ||
| if agents.iter().any(|(a, _)| *a == agent_addr) { |
There was a problem hiding this comment.
一堆dashmap, 并发问题心智负担太大了,很容易就可以发现到问题
T1: L159
T2: L138...继续往后,ret close
T1: 继续。这时候agents就有了一个不存在的FD作为key
| .nat_features | ||
| .get(&fd) | ||
| .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "unknown fd"))?; | ||
| Ok(*once.wait().await) |
|
|
||
| /// Remove a previously registered STUN agent. | ||
| pub fn del_agent(&self, fd: Fd, agent_addr: SocketAddr) { | ||
| if let Some(mut agents) = self.agents.get_mut(&fd) { |
| .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "unknown agent"))? | ||
| }; | ||
|
|
||
| Ok(*once.wait().await) |
| impl<T: BufMut> WriteStunHeader for T { | ||
| fn put_stun_header(&mut self, stun_header: &StunHeader) { | ||
| self.put_u8(STUN_HEADER_BITS); | ||
| self.put_u32(0); |
| fn put_forward_header(&mut self, forward_header: &ForwardHeader); | ||
| } | ||
|
|
||
| impl<T: BufMut> WriteForwardHeader for T { |
There was a problem hiding this comment.
这是从 qtraversal 哪里拷贝过来的,别紧张,只是在整理,这个 rs文件同样没被激活使用
No description provided.