Add maxReuseTimes

This commit is contained in:
Fangliding
2024-12-31 18:38:03 +08:00
parent dee64ef240
commit a0bf239fc1
7 changed files with 34 additions and 11 deletions
+5 -2
View File
@@ -170,7 +170,7 @@ func (f *DialingWorkerFactory) Create() (*ClientWorker, error) {
type ClientStrategy struct {
MaxConcurrency uint32
MaxConnection uint32
MaxReuseTimes uint32
}
type ClientWorker struct {
@@ -179,6 +179,7 @@ type ClientWorker struct {
done *done.Instance
timer *time.Ticker
strategy ClientStrategy
timeCretaed time.Time
}
var (
@@ -194,6 +195,7 @@ func NewClientWorker(stream transport.Link, s ClientStrategy) (*ClientWorker, er
done: done.New(),
timer: time.NewTicker(time.Second * 16),
strategy: s,
timeCretaed: time.Now(),
}
go c.fetchOutput()
@@ -288,7 +290,7 @@ func fetchInput(ctx context.Context, s *Session, output buf.Writer) {
func (m *ClientWorker) IsClosing() bool {
sm := m.sessionManager
if m.strategy.MaxConnection > 0 && sm.Count() >= int(m.strategy.MaxConnection) {
if m.strategy.MaxReuseTimes > 0 && sm.Count() >= int(m.strategy.MaxReuseTimes) {
return true
}
return false
@@ -318,6 +320,7 @@ func (m *ClientWorker) Dispatch(ctx context.Context, link *transport.Link) bool
if s == nil {
return false
}
errors.LogInfo(ctx, "Allocated mux.cool sub connection ID: ", s.ID, "/", m.strategy.MaxReuseTimes, " living: ", m.ActiveConnections(), "/", m.strategy.MaxConcurrency, " age: ", time.Since(m.timeCretaed).Truncate(time.Second))
s.input = link.Reader
s.output = link.Writer
go fetchInput(ctx, s, m.link.Writer)
+2 -2
View File
@@ -58,7 +58,7 @@ func TestClientWorkerClose(t *testing.T) {
Writer: w1,
}, mux.ClientStrategy{
MaxConcurrency: 4,
MaxConnection: 4,
MaxReuseTimes: 4,
})
common.Must(err)
@@ -68,7 +68,7 @@ func TestClientWorkerClose(t *testing.T) {
Writer: w2,
}, mux.ClientStrategy{
MaxConcurrency: 4,
MaxConnection: 4,
MaxReuseTimes: 4,
})
common.Must(err)
+1 -1
View File
@@ -56,7 +56,7 @@ func (m *SessionManager) Allocate(Strategy *ClientStrategy) *Session {
defer m.Unlock()
MaxConcurrency := int(Strategy.MaxConcurrency)
MaxConnection := uint16(Strategy.MaxConnection)
MaxConnection := uint16(Strategy.MaxReuseTimes)
if m.closed || (MaxConcurrency > 0 && len(m.sessions) >= MaxConcurrency) || (MaxConnection > 0 && m.count >= MaxConnection) {
return nil