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 { ...@@ -56,8 +56,10 @@ func OnNamespaceConnected(c *neffos.NSConn, msg neffos.Message) error {
remoteIP = strings.Split(remoteAddr, ",")[0] remoteIP = strings.Split(remoteAddr, ",")[0]
logger.Infof("Accepted %s connect websocket from %s", user.Username, remoteIP) logger.Infof("Accepted %s connect websocket from %s", user.Username, remoteIP)
go func() { go func() {
tick := time.NewTicker(30 * time.Second)
defer tick.Stop()
for { for {
<-time.After(30 * time.Second) <-tick.C
if c.Conn.IsClosed() { if c.Conn.IsClosed() {
logger.Infof("User %s from %s websocket connect closed", user.Username, remoteIP) logger.Infof("User %s from %s websocket connect closed", user.Username, remoteIP)
return return
......
...@@ -60,6 +60,8 @@ func (c *CommandRecorder) record() { ...@@ -60,6 +60,8 @@ func (c *CommandRecorder) record() {
maxRetry := 0 maxRetry := 0
logger.Infof("Session %s: Command recorder start", c.sessionID) logger.Infof("Session %s: Command recorder start", c.sessionID)
defer logger.Infof("Session %s: Command recorder close", c.sessionID) defer logger.Infof("Session %s: Command recorder close", c.sessionID)
tick := time.NewTicker(time.Second * 10)
defer tick.Stop()
for { for {
select { select {
case <-c.closed: case <-c.closed:
...@@ -74,7 +76,7 @@ func (c *CommandRecorder) record() { ...@@ -74,7 +76,7 @@ func (c *CommandRecorder) record() {
if len(cmdList) < 5 { if len(cmdList) < 5 {
continue continue
} }
case <-time.After(time.Second * 5): case <-tick.C:
if len(cmdList) == 0 { if len(cmdList) == 0 {
continue continue
} }
......
...@@ -152,10 +152,19 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn srvconn.ServerCo ...@@ -152,10 +152,19 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn srvconn.ServerCo
go LoopRead(srvConn, srvInChan) go LoopRead(srvConn, srvInChan)
winCh := userConn.WinCh() winCh := userConn.WinCh()
maxIdleTime := s.MaxIdleTime * time.Minute
lastActiveTime := time.Now()
tick := time.NewTicker(30 * time.Second)
defer tick.Stop()
for { for {
select { 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) 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) logger.Debugf("Session idle more than %d minutes, disconnect: %s", s.MaxIdleTime, s.ID)
msg = utils.WrapperWarn(msg) msg = utils.WrapperWarn(msg)
...@@ -190,6 +199,7 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn srvconn.ServerCo ...@@ -190,6 +199,7 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn srvconn.ServerCo
} }
_, err = srvConn.Write(p) _, err = srvConn.Write(p)
} }
lastActiveTime = time.Now()
} }
} }
......
...@@ -548,17 +548,12 @@ func (u *UserSftp) LoopPushFTPLog() { ...@@ -548,17 +548,12 @@ func (u *UserSftp) LoopPushFTPLog() {
dataChan := make(chan *model.FTPLog) dataChan := make(chan *model.FTPLog)
go u.SendFTPLog(dataChan) go u.SendFTPLog(dataChan)
defer close(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 { select {
case <-time.After(timeoutSecond): case <-tick.C:
case logData, ok := <-u.LogChan: case logData, ok := <-u.LogChan:
if !ok { if !ok {
return 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