Skip to content

Commit e2701be

Browse files
committed
Add maxReuseTimes
1 parent 1b25b84 commit e2701be

7 files changed

Lines changed: 35 additions & 11 deletions

File tree

app/proxyman/config.pb.go

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/proxyman/config.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,6 @@ message MultiplexingConfig {
6868
int32 xudpConcurrency = 3;
6969
// "reject" (default), "allow" or "skip".
7070
string xudpProxyUDP443 = 4;
71+
// MaxReuseTimes for an connection
72+
int32 maxReuseTimes = 5;
7173
}

app/proxyman/outbound/handler.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbou
121121

122122
if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil {
123123
if config := h.senderSettings.MultiplexSettings; config.Enabled {
124+
// MaxReuseTimes use 60000 as default, and it also means the upper limit of MaxReuseTimes
125+
// In mux cool spec, connection ID is 2 bytes, so physical limit is 65535, bu we reserve some IDs for future use
126+
MaxReuseTimes := uint32(60000)
127+
if config.MaxReuseTimes != 0 && config.MaxReuseTimes < 60000 {
128+
MaxReuseTimes = uint32(config.MaxReuseTimes)
129+
}
124130
if config.Concurrency < 0 {
125131
h.mux = &mux.ClientManager{Enabled: false}
126132
}
@@ -136,7 +142,7 @@ func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbou
136142
Dialer: h,
137143
Strategy: mux.ClientStrategy{
138144
MaxConcurrency: uint32(config.Concurrency),
139-
MaxConnection: 128,
145+
MaxReuseTimes: MaxReuseTimes,
140146
},
141147
},
142148
},
@@ -157,7 +163,7 @@ func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbou
157163
Dialer: h,
158164
Strategy: mux.ClientStrategy{
159165
MaxConcurrency: uint32(config.XudpConcurrency),
160-
MaxConnection: 128,
166+
MaxReuseTimes: 128,
161167
},
162168
},
163169
},

common/mux/client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (f *DialingWorkerFactory) Create() (*ClientWorker, error) {
170170

171171
type ClientStrategy struct {
172172
MaxConcurrency uint32
173-
MaxConnection uint32
173+
MaxReuseTimes uint32
174174
}
175175

176176
type ClientWorker struct {
@@ -179,6 +179,7 @@ type ClientWorker struct {
179179
done *done.Instance
180180
timer *time.Ticker
181181
strategy ClientStrategy
182+
timeCretaed time.Time
182183
}
183184

184185
var (
@@ -194,6 +195,7 @@ func NewClientWorker(stream transport.Link, s ClientStrategy) (*ClientWorker, er
194195
done: done.New(),
195196
timer: time.NewTicker(time.Second * 16),
196197
strategy: s,
198+
timeCretaed: time.Now(),
197199
}
198200

199201
go c.fetchOutput()
@@ -288,7 +290,7 @@ func fetchInput(ctx context.Context, s *Session, output buf.Writer) {
288290

289291
func (m *ClientWorker) IsClosing() bool {
290292
sm := m.sessionManager
291-
if m.strategy.MaxConnection > 0 && sm.Count() >= int(m.strategy.MaxConnection) {
293+
if m.strategy.MaxReuseTimes > 0 && sm.Count() >= int(m.strategy.MaxReuseTimes) {
292294
return true
293295
}
294296
return false
@@ -318,6 +320,8 @@ func (m *ClientWorker) Dispatch(ctx context.Context, link *transport.Link) bool
318320
if s == nil {
319321
return false
320322
}
323+
errors.LogInfo(ctx, "allocated mux.cool subConnection ID: ", s.ID, "/", m.strategy.MaxReuseTimes)
324+
errors.LogInfo(ctx, "living subConnections:", m.ActiveConnections(), "/", m.strategy.MaxConcurrency, ", this mux connection has been created for ", time.Since(m.timeCretaed).Truncate(time.Second))
321325
s.input = link.Reader
322326
s.output = link.Writer
323327
go fetchInput(ctx, s, m.link.Writer)

common/mux/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestClientWorkerClose(t *testing.T) {
5858
Writer: w1,
5959
}, mux.ClientStrategy{
6060
MaxConcurrency: 4,
61-
MaxConnection: 4,
61+
MaxReuseTimes: 4,
6262
})
6363
common.Must(err)
6464

@@ -68,7 +68,7 @@ func TestClientWorkerClose(t *testing.T) {
6868
Writer: w2,
6969
}, mux.ClientStrategy{
7070
MaxConcurrency: 4,
71-
MaxConnection: 4,
71+
MaxReuseTimes: 4,
7272
})
7373
common.Must(err)
7474

common/mux/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (m *SessionManager) Allocate(Strategy *ClientStrategy) *Session {
5656
defer m.Unlock()
5757

5858
MaxConcurrency := int(Strategy.MaxConcurrency)
59-
MaxConnection := uint16(Strategy.MaxConnection)
59+
MaxConnection := uint16(Strategy.MaxReuseTimes)
6060

6161
if m.closed || (MaxConcurrency > 0 && len(m.sessions) >= MaxConcurrency) || (MaxConnection > 0 && m.count >= MaxConnection) {
6262
return nil

infra/conf/xray.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func (c *SniffingConfig) Build() (*proxyman.SniffingConfig, error) {
101101
type MuxConfig struct {
102102
Enabled bool `json:"enabled"`
103103
Concurrency int16 `json:"concurrency"`
104+
MaxReuseTimes int32 `json:"maxReuseTimes"`
104105
XudpConcurrency int16 `json:"xudpConcurrency"`
105106
XudpProxyUDP443 string `json:"xudpProxyUDP443"`
106107
}
@@ -117,6 +118,7 @@ func (m *MuxConfig) Build() (*proxyman.MultiplexingConfig, error) {
117118
return &proxyman.MultiplexingConfig{
118119
Enabled: m.Enabled,
119120
Concurrency: int32(m.Concurrency),
121+
MaxReuseTimes: m.MaxReuseTimes,
120122
XudpConcurrency: int32(m.XudpConcurrency),
121123
XudpProxyUDP443: m.XudpProxyUDP443,
122124
}, nil

0 commit comments

Comments
 (0)