Просмотр исходного кода

Add ability to cancel Accept by passing a context

Evan Phoenix 5 лет назад
Родитель
Сommit
79a46c0331
1 измененных файлов с 17 добавлено и 0 удалено
  1. 17 0
      session.go

+ 17 - 0
session.go

@@ -3,6 +3,7 @@ package yamux
 import (
 import (
 	"bufio"
 	"bufio"
 	"bytes"
 	"bytes"
+	"context"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"io/ioutil"
 	"io/ioutil"
@@ -250,6 +251,22 @@ func (s *Session) AcceptStream() (*Stream, error) {
 	}
 	}
 }
 }
 
 
+// AcceptStream is used to block until the next available stream
+// is ready to be accepted.
+func (s *Session) AcceptStreamWithContext(ctx context.Context) (*Stream, error) {
+	select {
+	case <-ctx.Done():
+		return nil, ctx.Err()
+	case stream := <-s.acceptCh:
+		if err := stream.sendWindowUpdate(); err != nil {
+			return nil, err
+		}
+		return stream, nil
+	case <-s.shutdownCh:
+		return nil, s.shutdownErr
+	}
+}
+
 // Close is used to close the session and all streams.
 // Close is used to close the session and all streams.
 // Attempts to send a GoAway before closing the connection.
 // Attempts to send a GoAway before closing the connection.
 func (s *Session) Close() error {
 func (s *Session) Close() error {