You need to enable JavaScript to run this app. Rust服务端如何让客户端无需Mutex即可直接访问其他已连接客户端
优惠活动
大模型
产品
解决方案
定价
更多

Rust服务端如何让客户端无需Mutex即可直接访问其他已连接客户端

问题

我正在开发一个Rust crate,其中封装了用于数据收发的Server和Client结构体。我需要让服务端存储已连接客户端的列表,且客户端处理闭包可以在不同线程中访问该列表,同时希望避免使用Mutex,防止出现性能瓶颈。

我尝试过使用crossbeam的SegQueue,但它仅支持push()和pop()操作,而我需要像操作vector一样能够访问、移除任意客户端。

服务端代码如下:

use std::net::TcpListener;
use std::net::TcpStream;
use std::thread;
use std::time::Duration;
use rayon::ThreadPoolBuilder;

pub enum ReadEvent {
    Disconnected,
    ReceivedError(String),
    InvalidPacket,
}

pub enum SendEvent {
    NotConnected,
}

pub enum ConnectionError {
    FailedToConnect(String),
}

pub struct Client {
    address: Option<String>,
    stream: Option<TcpStream>,
    pub(crate) disconnected: bool,
}

impl Client {

    /// Will connect to an IP and Port and return a new client object
    /// Clients only work with Notitia servers so this method can not be accessed by users
    pub(crate) fn new<S: Into<String>>(ip: S, port: S) -> Self {
        let address = format!("{}:{}", ip.into(), port.into());

        let stream_result = TcpStream::connect(address.clone());
        if stream_result.is_err() {
            panic!("Client failed to connect to address {}", address);
        }

        Self {
            address: Some(address),
            stream: Some(stream_result.unwrap()),
            disconnected: false
        }
    }

    /// Creates a client from an existing connection
    /// /// Clients only work with Notitia servers so this method can not be accessed by users
    pub(crate) fn from<S: Into<String>>(address: S, stream: TcpStream) -> Self {
        Self {
            address: Some(address.into()),
            stream: Some(stream),
            disconnected: false
        }
    }

    pub(crate) fn connect<S: Into<String>>(&mut self, ip: S, port: S) -> Result<(), ConnectionError> {
        self.disconnect();
        self.disconnected = false;
        let address = format!("{}:{}", ip.into(), port.into());
        self.address = Some(address.clone());
        let stream = TcpStream::connect(address);
        if stream.is_err() {
            return Err(ConnectionError::FailedToConnect(stream.unwrap_err().to_string()));
        }
        self.stream = Some(stream.unwrap());
        Ok(())
    }

    pub fn disconnect(&mut self) {
        self.stream = None;
        self.address = None;
        self.disconnected = true;
    }

    fn try_get_stream(&mut self) -> Option<&TcpStream> {
        self.disconnect(); // ensure the Client object is disconnected
        let stream = self.stream.as_ref().unwrap();
        Some(stream)
    }

    /// Sends data to the stream
    pub fn send<S: Into<String>>(&mut self, data: S) -> Result<(), SendEvent> {
        let stream = self.try_get_stream();
        if stream.is_none() {
            return Err(SendEvent::NotConnected);
        }
        // Writes to the stream here
        Ok(())
    }

    /// Sends an error to the client and if it should disconnect
    pub fn send_error<S: Into<String>>(&mut self, error: S, disconnect: bool) -> Result<(), SendEvent> {
        let stream = self.try_get_stream();
        if stream.is_none() {
            return Err(SendEvent::NotConnected);
        }
        // writes an error to the stream
        Ok(())
    }

    /// Waits until a message is received from the stream
    pub fn read(&mut self) -> Result<String, ReadEvent> {
        let stream = self.try_get_stream();
        if stream.is_none() {
            return Err(ReadEvent::Disconnected);
        }
        // Reads data from the stream and processes it into a string output
        Ok(format!(""))
    }
}

impl Clone for Client {
    fn clone(&self) -> Self {
        let address_clone = if self.address.is_none() {
            None
        } else {
            let addr = self.address.as_ref().unwrap();
            Some(addr.clone())
        };

        let stream = if self.stream.is_none() {
            None
        } else {
            let s = self.stream.as_ref().unwrap();
            let c = s.try_clone();
            if c.is_err() {
                None
            } else {
                Some(c.unwrap())
            }
        };

        Self {
            address: address_clone,
            stream,
            disconnected: self.disconnected
        }
    }
}

pub enum ServerError {
    FailedToBindAddr(String),
}

pub struct Server {
    address: String,
    connected: Vec<Client> // <--- The vector in question
}

impl Server {

    pub fn new<IP: Into<String>, PORT: Into<String>>(ip: IP, port: PORT) -> Self {
        Self {
            address: format!("{}:{}", ip.into(), port.into()),
            connected: Vec::new(),
        }
    }
 
    /// Starts the server
    /// max_connections: If Some, will limit the amount of connections to the server at a time
    /// client_handle: A closure that takes a reference to a client and a reference to the vector of clients that are connected
    pub fn start<'a, F: 'a + Fn(&Client, &Vec<Client>) + Send + Sync>(& mut self, max_connections: Option<usize>, client_handle: F) -> Result<(), ServerError> {
        // bind the listener
        let listener_res = TcpListener::bind(self.address.clone());
        if listener_res.is_err() {
            return Err(ServerError::FailedToBindAddr(listener_res.unwrap_err().to_string()));
        }
        let listener = listener_res.unwrap();

        for mut s in listener.incoming() {
            if s.is_err() {
                continue;
            }
            let stream = s.unwrap();

            let sockaddr = stream.peer_addr();
            if sockaddr.is_err() {
                // Write an error to the client
                continue;
            }
            let ip = sockaddr.unwrap().ip().to_string();

            let mut client = Client::from(ip, stream);

            let connections = self.connected.len();

            if max_connections.is_some() && connections >= max_connections.unwrap() {
                client.send_error("Server is full!", true);
                continue;
            }

            self.connected.push(client.clone());

            pool.install(|| { // Uses rayon for the threadpool
                client_handle(&client, &self.connected); // The issue
                client.disconnect();
                // TODO Remove client from self.connected
            });
        }

        Ok(())
    }

}

fn main() {
    thread::spawn(move || { // start the client after 5 seconds to give the server time to start up
        thread::sleep(Duration::from_millis(5000));
        let mut client = Client::new("localhost", "2277");
    });
    let mut server = Server::new("0.0.0.0", "2277");
    server.start(Some(20), |client, connected| {
        println!("Connected!")
    });
}

我还考虑将connected改为哈希表类型,额外存储连接ID。请问有没有无需使用Mutex即可让客户端直接访问其他已连接客户端的可行方案?如果没有的话,有没有非直连的实现方式?

编辑:应要求更新为可运行代码,运行需依赖rayon crate。


解决方案

Rust并发场景下修改共享数据必须通过同步机制保证内存安全,不存在完全无同步的共享可变方案,但你可以选择比Mutex性能更好的无锁同步结构,或者用消息传递的方式避免直接共享数据。

无需Mutex的直接访问方案

你可以使用crossbeam生态的crossbeam-skiplist库提供的SkipMap,这是基于无锁跳表实现的并发有序映射,支持O(log n)复杂度的增、删、随机访问操作,全程使用CAS指令实现同步,没有Mutex的内核态上下文切换开销,高并发场景下性能远高于Mutex<HashMap>,刚好适配你想改成带连接ID哈希表的需求。

具体改造步骤

  1. 新增依赖到Cargo.toml
[dependencies]
rayon = "1.10"
crossbeam-skiplist = "0.2"
  1. 改造Server结构体和启动逻辑
use crossbeam_skiplist::SkipMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering;

pub struct Server {
    address: String,
    // 连接ID自增计数器
    next_conn_id: AtomicU64,
    // 无锁并发映射,键为连接ID,值为Client实例
    connected: Arc<SkipMap<u64, Client>>,
}

impl Server {
    pub fn new<IP: Into<String>, PORT: Into<String>>(ip: IP, port: PORT) -> Self {
        Self {
            address: format!("{}:{}", ip.into(), port.into()),
            next_conn_id: AtomicU64::new(1),
            connected: Arc::new(SkipMap::new()),
        }
    }
 
    // 调整闭包参数类型为SkipMap
    pub fn start<'a, F: 'a + Fn(&Client, &SkipMap<u64, Client>) + Send + Sync>(& mut self, max_connections: Option<usize>, client_handle: F) -> Result<(), ServerError> {
        let listener_res = TcpListener::bind(self.address.clone()).map_err(|e| ServerError::FailedToBindAddr(e.to_string()))?;
        // 补全原代码缺失的线程池初始化逻辑
        let pool = ThreadPoolBuilder::new().build().map_err(|e| ServerError::FailedToBindAddr(e.to_string()))?;

        for s in listener.incoming() {
            let stream = match s {
                Ok(s) => s,
                Err(_) => continue,
            };

            let ip = match stream.peer_addr() {
                Ok(addr) => addr.ip().to_string(),
                Err(_) => continue,
            };

            let mut client = Client::from(ip, stream);
            let connections = self.connected.len();

            if let Some(max) = max_connections {
                if connections >= max {
                    let _ = client.send_error("Server is full!", true);
                    continue;
                }
            }

            // 生成唯一连接ID
            let conn_id = self.next_conn_id.fetch_add(1, Ordering::Relaxed);
            // 插入到无锁映射
            self.connected.insert(conn_id, client.clone());
            // 克隆Arc指针传到线程池任务
            let connected_clone = self.connected.clone();

            pool.install(move || {
                client_handle(&client, &connected_clone);
                client.disconnect();
                // 任务结束直接无锁删除对应连接
                connected_clone.remove(&conn_id);
            });
        }

        Ok(())
    }
}
  1. 闭包使用说明:你可以直接遍历SkipMap访问所有已连接客户端,也可以通过连接ID精准查询指定客户端,所有操作都无需加锁,线程安全。

非直连实现方案

如果你不想用任何共享数据结构,可以选择Actor模型的消息传递方案,完全避免共享:

  • 单独启动一个客户端管理线程,内部维护普通的HashMap<u64, Client>,不需要任何同步原语
  • 给管理线程绑定一个多生产者单消费者通道,接收三种指令:新增客户端、删除客户端、查询客户端列表/指定客户端
  • 所有业务线程需要操作客户端列表时,只需要往通道发送指令,等待管理线程返回结果即可
  • 该方案所有对客户端列表的操作都串行在管理线程,没有并发问题,通道开销极低,足够应对大部分中小规模服务的性能需求。

内容的提问来源于stack exchange,提问作者Skepz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 18:18:03