Cap max time in cloak mode

This commit is contained in:
9seconds
2019-12-03 13:56:37 +03:00
parent d7e565e0f5
commit c5029c706c
+15 -6
View File
@@ -9,7 +9,10 @@ import (
"github.com/9seconds/mtg/wrappers/rwc" "github.com/9seconds/mtg/wrappers/rwc"
) )
const cloakTimeout = 5 * time.Second const (
cloakLastActivityTimeout = 5 * time.Second
cloakMaxTimeout = 30 * time.Second
)
func cloak(one, another io.ReadWriteCloser) { func cloak(one, another io.ReadWriteCloser) {
defer func() { defer func() {
@@ -41,17 +44,23 @@ func cloak(one, another io.ReadWriteCloser) {
}() }()
go func() { go func() {
timer := time.NewTimer(cloakTimeout) lastActivityTimer := time.NewTimer(cloakLastActivityTimeout)
defer timer.Stop() defer lastActivityTimer.Stop()
maxTimer := time.NewTimer(cloakMaxTimeout)
defer maxTimer.Stop()
for { for {
select { select {
case <-channelPing: case <-channelPing:
timer.Stop() lastActivityTimer.Stop()
timer = time.NewTimer(cloakTimeout) lastActivityTimer = time.NewTimer(cloakLastActivityTimeout)
case <-ctx.Done(): case <-ctx.Done():
return return
case <-timer.C: case <-lastActivityTimer.C:
cancel()
return
case <-maxTimer.C:
cancel() cancel()
return return
} }