Skip to content

Commit 2225d25

Browse files
authored
create UDP connection only when needed (xtaci#825)
* create udp connection only when needed * misc * some more minor changes * update option description * sync the doc * lets keep original * respect config.Quiet * misc * removed the useless muxes init
1 parent e9316f7 commit 2225d25

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ GLOBAL OPTIONS:
152152
--mode value profiles: fast3, fast2, fast, normal, manual (default: "fast")
153153
--conn value set num of UDP connections to server (default: 1)
154154
--autoexpire value set auto expiration time(in seconds) for a single UDP connection, 0 to disable (default: 0)
155-
--scavengettl value set how long an expired connection can live(in sec), -1 to disable (default: 600)
155+
--scavengettl value set how long an expired connection can live (in seconds) (default: 600)
156156
--mtu value set maximum transmission unit for UDP packets (default: 1350)
157157
--sndwnd value set send window size(num of packets) (default: 128)
158158
--rcvwnd value set receive window size(num of packets) (default: 512)

client/main.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func main() {
129129
cli.IntFlag{
130130
Name: "scavengettl",
131131
Value: 600,
132-
Usage: "set how long an expired connection can live(in sec), -1 to disable",
132+
Usage: "set how long an expired connection can live (in seconds)",
133133
},
134134
cli.IntFlag{
135135
Name: "mtu",
@@ -430,34 +430,30 @@ func main() {
430430
go generic.SnmpLogger(config.SnmpLog, config.SnmpPeriod)
431431

432432
// start scavenger
433-
chScavenger := make(chan *smux.Session, 128)
433+
chScavenger := make(chan timedSession, 128)
434434
go scavenger(chScavenger, &config)
435435

436436
// start listener
437437
numconn := uint16(config.Conn)
438438
muxes := make([]timedSession, numconn)
439-
for k := range muxes {
440-
muxes[k].session = waitConn()
441-
muxes[k].expiryDate = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
442-
if config.AutoExpire > 0 { // only when autoexpire set
443-
chScavenger <- muxes[k].session
444-
}
445-
}
446-
447439
rr := uint16(0)
448440
for {
449441
p1, err := listener.AcceptTCP()
450442
if err != nil {
451443
log.Fatalf("%+v", err)
452444
}
445+
if !config.Quiet {
446+
log.Println("accepted an TCP conn:", p1.RemoteAddr())
447+
}
453448
idx := rr % numconn
454449

455450
// do auto expiration && reconnection
456-
if muxes[idx].session.IsClosed() || (config.AutoExpire > 0 && time.Now().After(muxes[idx].expiryDate)) {
451+
if muxes[idx].session == nil || muxes[idx].session.IsClosed() ||
452+
(config.AutoExpire > 0 && time.Now().After(muxes[idx].expiryDate)) {
457453
muxes[idx].session = waitConn()
458454
muxes[idx].expiryDate = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
459455
if config.AutoExpire > 0 { // only when autoexpire set
460-
chScavenger <- muxes[idx].session
456+
chScavenger <- muxes[idx]
461457
}
462458
}
463459

@@ -468,25 +464,35 @@ func main() {
468464
myApp.Run(os.Args)
469465
}
470466

471-
func scavenger(ch chan *smux.Session, config *Config) {
467+
func scavenger(ch chan timedSession, config *Config) {
468+
// When AutoExpire is set to 0 (default), sessionList will keep empty.
469+
// Then this routine won't need to do anything; thus just terminate it.
470+
if config.AutoExpire <= 0 {
471+
return
472+
}
473+
472474
ticker := time.NewTicker(time.Second)
473475
defer ticker.Stop()
474476
var sessionList []timedSession
475477
for {
476478
select {
477-
case sess := <-ch:
479+
case item := <-ch:
478480
sessionList = append(sessionList, timedSession{
479-
sess,
480-
time.Now().Add(time.Duration(config.ScavengeTTL+config.AutoExpire) * time.Second)})
481+
item.session,
482+
item.expiryDate.Add(time.Duration(config.ScavengeTTL) * time.Second)})
481483
case <-ticker.C:
484+
if len(sessionList) == 0 {
485+
continue
486+
}
487+
482488
var newList []timedSession
483489
for k := range sessionList {
484490
s := sessionList[k]
485491
if s.session.IsClosed() {
486-
log.Println("session normally closed", s.session.RemoteAddr())
492+
log.Println("scavenger: session normally closed:", s.session.LocalAddr())
487493
} else if time.Now().After(s.expiryDate) {
488-
log.Println("session reached scavenge ttl", s.session.RemoteAddr())
489494
s.session.Close()
495+
log.Println("scavenger: session closed due to ttl:", s.session.LocalAddr())
490496
} else {
491497
newList = append(newList, sessionList[k])
492498
}

0 commit comments

Comments
 (0)