Commit 2908acf3 authored by Eric's avatar Eric

[update] change time.After to ticker

parent e758f922
......@@ -56,8 +56,10 @@ func OnNamespaceConnected(c *neffos.NSConn, msg neffos.Message) error {
remoteIP = strings.Split(remoteAddr, ",")[0]
logger.Infof("Accepted %s connect websocket from %s", user.Username, remoteIP)
go func() {
tick := time.NewTicker(30 * time.Second)
defer tick.Stop()
for {
<-time.After(30 * time.Second)
<-tick.C
if c.Conn.IsClosed() {
logger.Infof("User %s from %s websocket connect closed", user.Username, remoteIP)
return
......
......@@ -60,6 +60,8 @@ func (c *CommandRecorder) record() {
maxRetry := 0
logger.Infof("Session %s: Command recorder start", c.sessionID)
defer logger.Infof("Session %s: Command recorder close", c.sessionID)
tick := time.NewTicker(time.Second * 10)
defer tick.Stop()
for {
select {
case <-c.closed:
......@@ -74,7 +76,7 @@ func (c *CommandRecorder) record() {
if len(cmdList) < 5 {
continue
}
case <-time.After(time.Second * 5):
case <-tick.C:
if len(cmdList) == 0 {
continue
}
......
......@@ -152,10 +152,19 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn srvconn.ServerCo
go LoopRead(srvConn, srvInChan)
winCh := userConn.WinCh()
maxIdleTime := s.MaxIdleTime * time.Minute
lastActiveTime := time.Now()
tick := time.NewTicker(30 * time.Second)
defer tick.Stop()
for {
select {
// 检测是否超过最大空闲时间
case <-time.After(s.MaxIdleTime * time.Minute):
case <-tick.C:
now := time.Now()
outTime := lastActiveTime.Add(maxIdleTime)
if !now.After(outTime) {
continue
}
msg := fmt.Sprintf(i18n.T("Connect idle more than %d minutes, disconnect"), s.MaxIdleTime)
logger.Debugf("Session idle more than %d minutes, disconnect: %s", s.MaxIdleTime, s.ID)
msg = utils.WrapperWarn(msg)
......@@ -190,6 +199,7 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn srvconn.ServerCo
}
_, err = srvConn.Write(p)
}
lastActiveTime = time.Now()
}
}
......
......@@ -548,17 +548,12 @@ func (u *UserSftp) LoopPushFTPLog() {
dataChan := make(chan *model.FTPLog)
go u.SendFTPLog(dataChan)
defer close(dataChan)
var timeoutSecond time.Duration
for {
switch len(ftpLogList) {
case 0:
timeoutSecond = time.Second * 60
default:
timeoutSecond = time.Second * 10
}
tick := time.NewTicker(time.Second * 10)
defer tick.Stop()
for {
select {
case <-time.After(timeoutSecond):
case <-tick.C:
case logData, ok := <-u.LogChan:
if !ok {
return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment