Browse Source

Reduce channel allocations

Armon Dadgar 10 years ago
parent
commit
17ed67ccd7
2 changed files with 12 additions and 3 deletions
  1. 6 1
      session.go
  2. 6 2
      stream.go

+ 6 - 1
session.go

@@ -236,9 +236,14 @@ func (s *Session) keepalive() {
 	}
 }
 
-// waitForSend waits to send a header, checking for a potential shutdown
+// waitForSendErr waits to send a header, checking for a potential shutdown
 func (s *Session) waitForSend(hdr header, body io.Reader) error {
 	errCh := make(chan error, 1)
+	return s.waitForSendErr(hdr, body, errCh)
+}
+
+// waitForSendErr waits to send a header, checking for a potential shutdown
+func (s *Session) waitForSendErr(hdr header, body io.Reader, errCh chan error) error {
 	ready := sendReady{Hdr: hdr, Body: body, Err: errCh}
 	select {
 	case s.sendCh <- ready:

+ 6 - 2
stream.go

@@ -37,9 +37,11 @@ type Stream struct {
 	recvLock      sync.Mutex
 
 	controlHdr     header
+	controlErr     chan error
 	controlHdrLock sync.Mutex
 
 	sendHdr  header
+	sendErr  chan error
 	sendLock sync.Mutex
 
 	recvNotifyCh chan struct{}
@@ -62,7 +64,9 @@ func newStream(session *Session, id uint32, state streamState) *Stream {
 		session:      session,
 		state:        state,
 		controlHdr:   header(make([]byte, headerSize)),
+		controlErr:   make(chan error, 1),
 		sendHdr:      header(make([]byte, headerSize)),
+		sendErr:      make(chan error, 1),
 		recvWindow:   initialStreamWindow,
 		sendWindow:   initialStreamWindow,
 		recvNotifyCh: make(chan struct{}, 1),
@@ -185,7 +189,7 @@ START:
 
 	// Send the header
 	s.sendHdr.encode(typeData, flags, s.id, max)
-	if err := s.session.waitForSend(s.sendHdr, body); err != nil {
+	if err := s.session.waitForSendErr(s.sendHdr, body, s.sendErr); err != nil {
 		return 0, err
 	}
 
@@ -247,7 +251,7 @@ func (s *Stream) sendWindowUpdate() error {
 
 	// Send the header
 	s.controlHdr.encode(typeWindowUpdate, flags, s.id, delta)
-	if err := s.session.waitForSend(s.controlHdr, nil); err != nil {
+	if err := s.session.waitForSendErr(s.controlHdr, nil, s.controlErr); err != nil {
 		return err
 	}