浏览代码

Cleans up timers and de-flakes unit test a bit.

James Phillips 9 年之前
父节点
当前提交
7720b08bf1
共有 2 个文件被更改,包括 8 次插入4 次删除
  1. 7 3
      session.go
  2. 1 1
      session_test.go

+ 7 - 3
session.go

@@ -305,7 +305,10 @@ func (s *Session) waitForSend(hdr header, body io.Reader) error {
 func (s *Session) waitForSendErr(hdr header, body io.Reader, errCh chan error) error {
 	var timeout <- chan time.Time
 	if body == nil {
-		timeout = time.After(s.config.HeaderWriteTimeout)
+		timer := time.NewTimer(s.config.HeaderWriteTimeout)
+		defer timer.Stop()
+
+		timeout = timer.C
 	}
 
 	ready := sendReady{Hdr: hdr, Body: body, Err: errCh}
@@ -331,14 +334,15 @@ func (s *Session) waitForSendErr(hdr header, body io.Reader, errCh chan error) e
 // sendCh itself can be full, we will enforce the configured HeaderWriteTimeout,
 // since this is a small control header.
 func (s *Session) sendNoWait(hdr header) error {
-	timeout := time.After(s.config.HeaderWriteTimeout)
+	timer := time.NewTimer(s.config.HeaderWriteTimeout)
+	defer timer.Stop()
 
 	select {
 	case s.sendCh <- sendReady{Hdr: hdr}:
 		return nil
 	case <-s.shutdownCh:
 		return ErrSessionShutdown
-	case <-timeout:
+	case <-timer.C:
 		return ErrHeaderWriteTimeout
 	}
 }

+ 1 - 1
session_test.go

@@ -44,7 +44,7 @@ func testClientServer() (*Session, *Session) {
 	conf := DefaultConfig()
 	conf.AcceptBacklog = 64
 	conf.KeepAliveInterval = 100 * time.Millisecond
-	conf.HeaderWriteTimeout = 100 * time.Millisecond
+	conf.HeaderWriteTimeout = 250 * time.Millisecond
 	return testClientServerConfig(conf)
 }