glider-ssh/_examples/ssh-timeouts/timeouts.go
Jeff Lindsay 48c9603bfc server: timeouts and context canceling on closed connections (#46)
Signed-off-by: Jeff Lindsay <progrium@gmail.com>
2017-07-24 16:25:45 -05:00

42 lines
779 B
Go

package main
import (
"log"
"time"
"github.com/gliderlabs/ssh"
)
var (
DeadlineTimeout = 30 * time.Second
IdleTimeout = 10 * time.Second
)
func main() {
ssh.Handle(func(s ssh.Session) {
log.Println("new connection")
i := 0
for {
i += 1
log.Println("active seconds:", i)
select {
case <-time.After(time.Second):
continue
case <-s.Context().Done():
log.Println("connection closed")
return
}
}
})
log.Println("starting ssh server on port 2222...")
log.Printf("connections will only last %s\n", DeadlineTimeout)
log.Printf("and timeout after %s of no activity\n", IdleTimeout)
server := &ssh.Server{
Addr: ":2222",
MaxTimeout: DeadlineTimeout,
IdleTimeout: IdleTimeout,
}
log.Fatal(server.ListenAndServe())
}