From c43ee98ec7c5a9d34b32cedf891b4c8707e9c8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Wed, 11 Nov 2026 06:13:21 +0800 Subject: [PATCH] VLESS Multiplex (mux.cool): Replace MutexWriter with buf.BufferedWriter (#7359) Fixes https://github.com/XTLS/Xray-core/pull/7355#issuecomment-6738888441 --------- Co-authored-by: Medium1992 --- common/buf/writer.go | 4 ++++ common/mux/client.go | 4 +++- common/mux/server.go | 4 +++- common/mux/writer.go | 13 ------------- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/common/buf/writer.go b/common/buf/writer.go index f14d83c3d..882a49ad5 100644 --- a/common/buf/writer.go +++ b/common/buf/writer.go @@ -236,6 +236,10 @@ func (w *BufferedWriter) Close() error { return common.Close(w.writer) } +func (w *BufferedWriter) Interrupt() { + common.Interrupt(w.writer) +} + // SequentialWriter is a Writer that writes MultiBuffer sequentially into the underlying io.Writer. type SequentialWriter struct { io.Writer diff --git a/common/mux/client.go b/common/mux/client.go index ab729ef71..65ee73e28 100644 --- a/common/mux/client.go +++ b/common/mux/client.go @@ -188,7 +188,9 @@ var ( // NewClientWorker creates a new mux.Client. func NewClientWorker(stream transport.Link, s ClientStrategy) (*ClientWorker, error) { - stream.Writer = &MutexWriter{Writer: stream.Writer} + writer := buf.NewBufferedWriter(stream.Writer) + writer.SetBuffered(false) + stream.Writer = writer c := &ClientWorker{ sessionManager: NewSessionManager(), link: stream, diff --git a/common/mux/server.go b/common/mux/server.go index 8dd48a131..01a1dac81 100644 --- a/common/mux/server.go +++ b/common/mux/server.go @@ -93,9 +93,11 @@ type ServerWorker struct { } func NewServerWorker(ctx context.Context, d routing.Dispatcher, link *transport.Link) (*ServerWorker, error) { + writer := buf.NewBufferedWriter(link.Writer) + writer.SetBuffered(false) worker := &ServerWorker{ dispatcher: d, - link: &transport.Link{Reader: link.Reader, Writer: &MutexWriter{Writer: link.Writer}}, + link: &transport.Link{Reader: link.Reader, Writer: writer}, sessionManager: NewSessionManager(), done: done.New(), timer: time.NewTicker(60 * time.Second), diff --git a/common/mux/writer.go b/common/mux/writer.go index ac5ec6c4f..0429f4fab 100644 --- a/common/mux/writer.go +++ b/common/mux/writer.go @@ -1,8 +1,6 @@ package mux import ( - "sync" - "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/net" @@ -11,17 +9,6 @@ import ( "github.com/xtls/xray-core/common/session" ) -type MutexWriter struct { - sync.Mutex - buf.Writer -} - -func (w *MutexWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { - w.Lock() - defer w.Unlock() - return w.Writer.WriteMultiBuffer(mb) -} - type Writer struct { dest net.Destination writer buf.Writer