Skip to content

Commit 9ac04cd

Browse files
committed
a smarter scavenger
1 parent 76c4d23 commit 9ac04cd

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

client/main.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func checkError(err error) {
7373
}
7474
}
7575

76+
type timedSession struct {
77+
session *smux.Session
78+
expiryDate time.Time
79+
}
80+
7681
func main() {
7782
rand.Seed(int64(time.Now().Nanosecond()))
7883
if VERSION == "SELFBUILD" {
@@ -421,20 +426,24 @@ func main() {
421426
}
422427
}
423428

424-
numconn := uint16(config.Conn)
425-
muxes := make([]struct {
426-
session *smux.Session
427-
ttl time.Time
428-
}, numconn)
429+
// start snmp logger
430+
go generic.SnmpLogger(config.SnmpLog, config.SnmpPeriod)
429431

432+
// start scavenger
433+
chScavenger := make(chan *smux.Session, 128)
434+
go scavenger(chScavenger, &config)
435+
436+
// start listener
437+
numconn := uint16(config.Conn)
438+
muxes := make([]timedSession, numconn)
430439
for k := range muxes {
431440
muxes[k].session = waitConn()
432-
muxes[k].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
441+
muxes[k].expiryDate = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
442+
if config.AutoExpire > 0 {
443+
chScavenger <- muxes[k].session
444+
}
433445
}
434446

435-
chScavenger := make(chan *smux.Session, 128)
436-
go scavenger(chScavenger, config.ScavengeTTL)
437-
go generic.SnmpLogger(config.SnmpLog, config.SnmpPeriod)
438447
rr := uint16(0)
439448
for {
440449
p1, err := listener.AcceptTCP()
@@ -444,10 +453,12 @@ func main() {
444453
idx := rr % numconn
445454

446455
// do auto expiration && reconnection
447-
if muxes[idx].session.IsClosed() || (config.AutoExpire > 0 && time.Now().After(muxes[idx].ttl)) {
448-
chScavenger <- muxes[idx].session
456+
if muxes[idx].session.IsClosed() || (config.AutoExpire > 0 && time.Now().After(muxes[idx].expiryDate)) {
449457
muxes[idx].session = waitConn()
450-
muxes[idx].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
458+
muxes[idx].expiryDate = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
459+
if config.AutoExpire > 0 {
460+
chScavenger <- muxes[idx].session
461+
}
451462
}
452463

453464
go handleClient(muxes[idx].session, p1, config.Quiet)
@@ -457,28 +468,23 @@ func main() {
457468
myApp.Run(os.Args)
458469
}
459470

460-
type scavengeSession struct {
461-
session *smux.Session
462-
ts time.Time
463-
}
464-
465-
func scavenger(ch chan *smux.Session, ttl int) {
471+
func scavenger(ch chan *smux.Session, config *Config) {
466472
ticker := time.NewTicker(time.Second)
467473
defer ticker.Stop()
468-
var sessionList []scavengeSession
474+
var sessionList []timedSession
469475
for {
470476
select {
471477
case sess := <-ch:
472-
sessionList = append(sessionList, scavengeSession{sess, time.Now()})
473-
log.Println("session marked as expired", sess.RemoteAddr())
478+
sessionList = append(sessionList, timedSession{
479+
sess,
480+
time.Now().Add(time.Duration(config.ScavengeTTL+config.AutoExpire) * time.Second)})
474481
case <-ticker.C:
475-
var newList []scavengeSession
482+
var newList []timedSession
476483
for k := range sessionList {
477484
s := sessionList[k]
478-
if s.session.NumStreams() == 0 || s.session.IsClosed() {
485+
if s.session.IsClosed() {
479486
log.Println("session normally closed", s.session.RemoteAddr())
480-
s.session.Close()
481-
} else if ttl >= 0 && time.Since(s.ts) >= time.Duration(ttl)*time.Second {
487+
} else if config.AutoExpire > 0 && time.Now().After(s.expiryDate) {
482488
log.Println("session reached scavenge ttl", s.session.RemoteAddr())
483489
s.session.Close()
484490
} else {

0 commit comments

Comments
 (0)