create UDP connection only when needed (#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
This commit is contained in:
Hugo Wang
2020-12-20 16:23:32 +08:00
committed by GitHub
parent e9316f7be4
commit 2225d258cb
2 changed files with 25 additions and 19 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ GLOBAL OPTIONS:
--mode value profiles: fast3, fast2, fast, normal, manual (default: "fast")
--conn value set num of UDP connections to server (default: 1)
--autoexpire value set auto expiration time(in seconds) for a single UDP connection, 0 to disable (default: 0)
--scavengettl value set how long an expired connection can live(in sec), -1 to disable (default: 600)
--scavengettl value set how long an expired connection can live (in seconds) (default: 600)
--mtu value set maximum transmission unit for UDP packets (default: 1350)
--sndwnd value set send window size(num of packets) (default: 128)
--rcvwnd value set receive window size(num of packets) (default: 512)
+24 -18
View File
@@ -129,7 +129,7 @@ func main() {
cli.IntFlag{
Name: "scavengettl",
Value: 600,
Usage: "set how long an expired connection can live(in sec), -1 to disable",
Usage: "set how long an expired connection can live (in seconds)",
},
cli.IntFlag{
Name: "mtu",
@@ -430,34 +430,30 @@ func main() {
go generic.SnmpLogger(config.SnmpLog, config.SnmpPeriod)
// start scavenger
chScavenger := make(chan *smux.Session, 128)
chScavenger := make(chan timedSession, 128)
go scavenger(chScavenger, &config)
// start listener
numconn := uint16(config.Conn)
muxes := make([]timedSession, numconn)
for k := range muxes {
muxes[k].session = waitConn()
muxes[k].expiryDate = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
if config.AutoExpire > 0 { // only when autoexpire set
chScavenger <- muxes[k].session
}
}
rr := uint16(0)
for {
p1, err := listener.AcceptTCP()
if err != nil {
log.Fatalf("%+v", err)
}
if !config.Quiet {
log.Println("accepted an TCP conn:", p1.RemoteAddr())
}
idx := rr % numconn
// do auto expiration && reconnection
if muxes[idx].session.IsClosed() || (config.AutoExpire > 0 && time.Now().After(muxes[idx].expiryDate)) {
if muxes[idx].session == nil || muxes[idx].session.IsClosed() ||
(config.AutoExpire > 0 && time.Now().After(muxes[idx].expiryDate)) {
muxes[idx].session = waitConn()
muxes[idx].expiryDate = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
if config.AutoExpire > 0 { // only when autoexpire set
chScavenger <- muxes[idx].session
chScavenger <- muxes[idx]
}
}
@@ -468,25 +464,35 @@ func main() {
myApp.Run(os.Args)
}
func scavenger(ch chan *smux.Session, config *Config) {
func scavenger(ch chan timedSession, config *Config) {
// When AutoExpire is set to 0 (default), sessionList will keep empty.
// Then this routine won't need to do anything; thus just terminate it.
if config.AutoExpire <= 0 {
return
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
var sessionList []timedSession
for {
select {
case sess := <-ch:
case item := <-ch:
sessionList = append(sessionList, timedSession{
sess,
time.Now().Add(time.Duration(config.ScavengeTTL+config.AutoExpire) * time.Second)})
item.session,
item.expiryDate.Add(time.Duration(config.ScavengeTTL) * time.Second)})
case <-ticker.C:
if len(sessionList) == 0 {
continue
}
var newList []timedSession
for k := range sessionList {
s := sessionList[k]
if s.session.IsClosed() {
log.Println("session normally closed", s.session.RemoteAddr())
log.Println("scavenger: session normally closed:", s.session.LocalAddr())
} else if time.Now().After(s.expiryDate) {
log.Println("session reached scavenge ttl", s.session.RemoteAddr())
s.session.Close()
log.Println("scavenger: session closed due to ttl:", s.session.LocalAddr())
} else {
newList = append(newList, sessionList[k])
}