2018-05-01 23:45:06 +00:00
|
|
|
package h2mux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-11-05 20:37:40 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-05-01 23:45:06 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/hpack"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-10-18 22:26:21 +00:00
|
|
|
defaultFrameSize uint32 = 1 << 14 // Minimum frame size in http2 spec
|
|
|
|
defaultWindowSize uint32 = (1 << 16) - 1 // Minimum window size in http2 spec
|
|
|
|
maxWindowSize uint32 = (1 << 31) - 1 // 2^31-1 = 2147483647, max window size in http2 spec
|
|
|
|
defaultTimeout time.Duration = 5 * time.Second
|
|
|
|
defaultRetries uint64 = 5
|
2019-10-17 22:15:51 +00:00
|
|
|
defaultWriteBufferMaxLen int = 1024 * 1024 // 1mb
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
SettingMuxerMagic http2.SettingID = 0x42db
|
|
|
|
MuxerMagicOrigin uint32 = 0xa2e43c8b
|
|
|
|
MuxerMagicEdge uint32 = 0x1088ebf9
|
|
|
|
)
|
|
|
|
|
|
|
|
type MuxedStreamHandler interface {
|
|
|
|
ServeStream(*MuxedStream) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type MuxedStreamFunc func(stream *MuxedStream) error
|
|
|
|
|
|
|
|
func (f MuxedStreamFunc) ServeStream(stream *MuxedStream) error {
|
|
|
|
return f(stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
type MuxerConfig struct {
|
|
|
|
Timeout time.Duration
|
|
|
|
Handler MuxedStreamHandler
|
|
|
|
IsClient bool
|
|
|
|
// Name is used to identify this muxer instance when logging.
|
|
|
|
Name string
|
|
|
|
// The minimum time this connection can be idle before sending a heartbeat.
|
|
|
|
HeartbeatInterval time.Duration
|
|
|
|
// The minimum number of heartbeats to send before terminating the connection.
|
|
|
|
MaxHeartbeats uint64
|
|
|
|
// Logger to use
|
|
|
|
Logger *log.Entry
|
|
|
|
CompressionQuality CompressionSetting
|
2018-10-18 22:26:21 +00:00
|
|
|
// Initial size for HTTP2 flow control windows
|
|
|
|
DefaultWindowSize uint32
|
|
|
|
// Largest allowable size for HTTP2 flow control windows
|
|
|
|
MaxWindowSize uint32
|
|
|
|
// Largest allowable capacity for the buffer of data to be sent
|
|
|
|
StreamWriteBufferMaxLen int
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Muxer struct {
|
|
|
|
// f is used to read and write HTTP2 frames on the wire.
|
|
|
|
f *http2.Framer
|
|
|
|
// config is the MuxerConfig given in Handshake.
|
|
|
|
config MuxerConfig
|
|
|
|
// w, r are references to the underlying connection used.
|
|
|
|
w io.WriteCloser
|
|
|
|
r io.ReadCloser
|
|
|
|
// muxReader is the read process.
|
|
|
|
muxReader *MuxReader
|
|
|
|
// muxWriter is the write process.
|
|
|
|
muxWriter *MuxWriter
|
|
|
|
// muxMetricsUpdater is the process to update metrics
|
2019-01-16 13:57:30 +00:00
|
|
|
muxMetricsUpdater muxMetricsUpdater
|
2018-05-01 23:45:06 +00:00
|
|
|
// newStreamChan is used to create new streams on the writer thread.
|
|
|
|
// The writer will assign the next available stream ID.
|
|
|
|
newStreamChan chan MuxedStreamRequest
|
|
|
|
// abortChan is used to abort the writer event loop.
|
|
|
|
abortChan chan struct{}
|
|
|
|
// abortOnce is used to ensure abortChan is closed once only.
|
|
|
|
abortOnce sync.Once
|
|
|
|
// readyList is used to signal writable streams.
|
|
|
|
readyList *ReadyList
|
|
|
|
// streams tracks currently-open streams.
|
|
|
|
streams *activeStreamMap
|
|
|
|
// explicitShutdown records whether the Muxer is closing because Shutdown was called, or due to another
|
|
|
|
// error.
|
|
|
|
explicitShutdown *BooleanFuse
|
|
|
|
|
|
|
|
compressionQuality CompressionPreset
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:08:55 +00:00
|
|
|
func RPCHeaders() []Header {
|
|
|
|
return []Header{
|
|
|
|
{Name: ":method", Value: "RPC"},
|
|
|
|
{Name: ":scheme", Value: "capnp"},
|
|
|
|
{Name: ":path", Value: "*"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
// Handshake establishes a muxed connection with the peer.
|
|
|
|
// After the handshake completes, it is possible to open and accept streams.
|
|
|
|
func Handshake(
|
|
|
|
w io.WriteCloser,
|
|
|
|
r io.ReadCloser,
|
|
|
|
config MuxerConfig,
|
2019-11-05 20:37:40 +00:00
|
|
|
activeStreamsMetrics prometheus.Gauge,
|
2018-05-01 23:45:06 +00:00
|
|
|
) (*Muxer, error) {
|
|
|
|
// Set default config values
|
|
|
|
if config.Timeout == 0 {
|
|
|
|
config.Timeout = defaultTimeout
|
|
|
|
}
|
2018-10-18 22:26:21 +00:00
|
|
|
if config.DefaultWindowSize == 0 {
|
|
|
|
config.DefaultWindowSize = defaultWindowSize
|
|
|
|
}
|
|
|
|
if config.MaxWindowSize == 0 {
|
|
|
|
config.MaxWindowSize = maxWindowSize
|
|
|
|
}
|
|
|
|
if config.StreamWriteBufferMaxLen == 0 {
|
|
|
|
config.StreamWriteBufferMaxLen = defaultWriteBufferMaxLen
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
// Initialise connection state fields
|
|
|
|
m := &Muxer{
|
|
|
|
f: http2.NewFramer(w, r), // A framer that writes to w and reads from r
|
|
|
|
config: config,
|
|
|
|
w: w,
|
|
|
|
r: r,
|
|
|
|
newStreamChan: make(chan MuxedStreamRequest),
|
|
|
|
abortChan: make(chan struct{}),
|
|
|
|
readyList: NewReadyList(),
|
2019-11-05 20:37:40 +00:00
|
|
|
streams: newActiveStreamMap(config.IsClient, activeStreamsMetrics),
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.f.ReadMetaHeaders = hpack.NewDecoder(4096, func(hpack.HeaderField) {})
|
|
|
|
// Initialise the settings to identify this connection and confirm the other end is sane.
|
|
|
|
handshakeSetting := http2.Setting{ID: SettingMuxerMagic, Val: MuxerMagicEdge}
|
|
|
|
compressionSetting := http2.Setting{ID: SettingCompression, Val: config.CompressionQuality.toH2Setting()}
|
|
|
|
if CompressionIsSupported() {
|
|
|
|
log.Debug("Compression is supported")
|
|
|
|
m.compressionQuality = config.CompressionQuality.getPreset()
|
|
|
|
} else {
|
|
|
|
log.Debug("Compression is not supported")
|
|
|
|
compressionSetting = http2.Setting{ID: SettingCompression, Val: 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMagic := MuxerMagicOrigin
|
|
|
|
if config.IsClient {
|
|
|
|
handshakeSetting.Val = MuxerMagicOrigin
|
|
|
|
expectedMagic = MuxerMagicEdge
|
|
|
|
}
|
|
|
|
errChan := make(chan error, 2)
|
|
|
|
// Simultaneously send our settings and verify the peer's settings.
|
|
|
|
go func() { errChan <- m.f.WriteSettings(handshakeSetting, compressionSetting) }()
|
|
|
|
go func() { errChan <- m.readPeerSettings(expectedMagic) }()
|
|
|
|
err := joinErrorsWithTimeout(errChan, 2, config.Timeout, ErrHandshakeTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Confirm sanity by ACKing the frame and expecting an ACK for our frame.
|
|
|
|
// Not strictly necessary, but let's pretend to be H2-like.
|
|
|
|
go func() { errChan <- m.f.WriteSettingsAck() }()
|
|
|
|
go func() { errChan <- m.readPeerSettingsAck() }()
|
|
|
|
err = joinErrorsWithTimeout(errChan, 2, config.Timeout, ErrHandshakeTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up reader/writer pair ready for serve
|
|
|
|
streamErrors := NewStreamErrorMap()
|
|
|
|
goAwayChan := make(chan http2.ErrCode, 1)
|
|
|
|
inBoundCounter := NewAtomicCounter(0)
|
|
|
|
outBoundCounter := NewAtomicCounter(0)
|
|
|
|
pingTimestamp := NewPingTimestamp()
|
|
|
|
connActive := NewSignal()
|
|
|
|
idleDuration := config.HeartbeatInterval
|
|
|
|
// Sanity check to enusre idelDuration is sane
|
|
|
|
if idleDuration == 0 || idleDuration < defaultTimeout {
|
|
|
|
idleDuration = defaultTimeout
|
|
|
|
config.Logger.Warn("Minimum idle time has been adjusted to ", defaultTimeout)
|
|
|
|
}
|
|
|
|
maxRetries := config.MaxHeartbeats
|
|
|
|
if maxRetries == 0 {
|
|
|
|
maxRetries = defaultRetries
|
|
|
|
config.Logger.Warn("Minimum number of unacked heartbeats to send before closing the connection has been adjusted to ", maxRetries)
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:57:30 +00:00
|
|
|
compBytesBefore, compBytesAfter := NewAtomicCounter(0), NewAtomicCounter(0)
|
|
|
|
|
|
|
|
m.muxMetricsUpdater = newMuxMetricsUpdater(
|
|
|
|
m.abortChan,
|
|
|
|
compBytesBefore,
|
|
|
|
compBytesAfter,
|
|
|
|
)
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
m.explicitShutdown = NewBooleanFuse()
|
|
|
|
m.muxReader = &MuxReader{
|
|
|
|
f: m.f,
|
|
|
|
handler: m.config.Handler,
|
|
|
|
streams: m.streams,
|
|
|
|
readyList: m.readyList,
|
|
|
|
streamErrors: streamErrors,
|
|
|
|
goAwayChan: goAwayChan,
|
|
|
|
abortChan: m.abortChan,
|
|
|
|
pingTimestamp: pingTimestamp,
|
|
|
|
connActive: connActive,
|
2018-10-18 22:26:21 +00:00
|
|
|
initialStreamWindow: m.config.DefaultWindowSize,
|
|
|
|
streamWindowMax: m.config.MaxWindowSize,
|
|
|
|
streamWriteBufferMaxLen: m.config.StreamWriteBufferMaxLen,
|
2020-05-05 22:56:39 +00:00
|
|
|
r: m.r,
|
|
|
|
metricsUpdater: m.muxMetricsUpdater,
|
|
|
|
bytesRead: inBoundCounter,
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
m.muxWriter = &MuxWriter{
|
2019-01-16 13:57:30 +00:00
|
|
|
f: m.f,
|
|
|
|
streams: m.streams,
|
|
|
|
streamErrors: streamErrors,
|
|
|
|
readyStreamChan: m.readyList.ReadyChannel(),
|
|
|
|
newStreamChan: m.newStreamChan,
|
|
|
|
goAwayChan: goAwayChan,
|
|
|
|
abortChan: m.abortChan,
|
|
|
|
pingTimestamp: pingTimestamp,
|
|
|
|
idleTimer: NewIdleTimer(idleDuration, maxRetries),
|
|
|
|
connActiveChan: connActive.WaitChannel(),
|
|
|
|
maxFrameSize: defaultFrameSize,
|
|
|
|
metricsUpdater: m.muxMetricsUpdater,
|
|
|
|
bytesWrote: outBoundCounter,
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
m.muxWriter.headerEncoder = hpack.NewEncoder(&m.muxWriter.headerBuffer)
|
|
|
|
|
|
|
|
if m.compressionQuality.dictSize > 0 && m.compressionQuality.nDicts > 0 {
|
|
|
|
nd, sz := m.compressionQuality.nDicts, m.compressionQuality.dictSize
|
|
|
|
writeDicts, dictChan := newH2WriteDictionaries(
|
|
|
|
nd,
|
|
|
|
sz,
|
|
|
|
m.compressionQuality.quality,
|
|
|
|
compBytesBefore,
|
|
|
|
compBytesAfter,
|
|
|
|
)
|
|
|
|
readDicts := newH2ReadDictionaries(nd, sz)
|
|
|
|
m.muxReader.dictionaries = h2Dictionaries{read: &readDicts, write: writeDicts}
|
|
|
|
m.muxWriter.useDictChan = dictChan
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Muxer) readPeerSettings(magic uint32) error {
|
|
|
|
frame, err := m.f.ReadFrame()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
settingsFrame, ok := frame.(*http2.SettingsFrame)
|
|
|
|
if !ok {
|
|
|
|
return ErrBadHandshakeNotSettings
|
|
|
|
}
|
|
|
|
if settingsFrame.Header().Flags != 0 {
|
|
|
|
return ErrBadHandshakeUnexpectedAck
|
|
|
|
}
|
|
|
|
peerMagic, ok := settingsFrame.Value(SettingMuxerMagic)
|
|
|
|
if !ok {
|
|
|
|
return ErrBadHandshakeNoMagic
|
|
|
|
}
|
|
|
|
if magic != peerMagic {
|
|
|
|
return ErrBadHandshakeWrongMagic
|
|
|
|
}
|
|
|
|
peerCompression, ok := settingsFrame.Value(SettingCompression)
|
|
|
|
if !ok {
|
|
|
|
m.compressionQuality = compressionPresets[CompressionNone]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ver, fmt, sz, nd := parseCompressionSettingVal(peerCompression)
|
|
|
|
if ver != compressionVersion || fmt != compressionFormat || sz == 0 || nd == 0 {
|
|
|
|
m.compressionQuality = compressionPresets[CompressionNone]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Values used for compression are the mimimum between the two peers
|
|
|
|
if sz < m.compressionQuality.dictSize {
|
|
|
|
m.compressionQuality.dictSize = sz
|
|
|
|
}
|
|
|
|
if nd < m.compressionQuality.nDicts {
|
|
|
|
m.compressionQuality.nDicts = nd
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Muxer) readPeerSettingsAck() error {
|
|
|
|
frame, err := m.f.ReadFrame()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
settingsFrame, ok := frame.(*http2.SettingsFrame)
|
|
|
|
if !ok {
|
|
|
|
return ErrBadHandshakeNotSettingsAck
|
|
|
|
}
|
|
|
|
if settingsFrame.Header().Flags != http2.FlagSettingsAck {
|
|
|
|
return ErrBadHandshakeUnexpectedSettings
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinErrorsWithTimeout(errChan <-chan error, receiveCount int, timeout time.Duration, timeoutError error) error {
|
|
|
|
for i := 0; i < receiveCount; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-time.After(timeout):
|
|
|
|
return timeoutError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:57:30 +00:00
|
|
|
// Serve runs the event loops that comprise h2mux:
|
|
|
|
// - MuxReader.run()
|
|
|
|
// - MuxWriter.run()
|
|
|
|
// - muxMetricsUpdater.run()
|
|
|
|
// In the normal case, Shutdown() is called concurrently with Serve() to stop
|
|
|
|
// these loops.
|
2018-05-01 23:45:06 +00:00
|
|
|
func (m *Muxer) Serve(ctx context.Context) error {
|
|
|
|
errGroup, _ := errgroup.WithContext(ctx)
|
|
|
|
errGroup.Go(func() error {
|
2020-02-20 00:57:29 +00:00
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := m.muxReader.run(m.config.Logger)
|
|
|
|
m.explicitShutdown.Fuse(false)
|
|
|
|
m.r.Close()
|
|
|
|
m.abort()
|
2020-05-05 22:56:39 +00:00
|
|
|
// don't block if parent goroutine quit early
|
|
|
|
select {
|
|
|
|
case ch <- err:
|
|
|
|
default:
|
|
|
|
}
|
2020-02-20 00:57:29 +00:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
errGroup.Go(func() error {
|
2020-02-20 00:57:29 +00:00
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := m.muxWriter.run(m.config.Logger)
|
|
|
|
m.explicitShutdown.Fuse(false)
|
|
|
|
m.w.Close()
|
|
|
|
m.abort()
|
2020-05-05 22:56:39 +00:00
|
|
|
// don't block if parent goroutine quit early
|
|
|
|
select {
|
|
|
|
case ch <- err:
|
|
|
|
default:
|
|
|
|
}
|
2020-02-20 00:57:29 +00:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
errGroup.Go(func() error {
|
2020-02-20 00:57:29 +00:00
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := m.muxMetricsUpdater.run(m.config.Logger)
|
2020-05-05 22:56:39 +00:00
|
|
|
// don't block if parent goroutine quit early
|
|
|
|
select {
|
|
|
|
case ch <- err:
|
|
|
|
default:
|
|
|
|
}
|
2020-02-20 00:57:29 +00:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := errGroup.Wait()
|
|
|
|
if isUnexpectedTunnelError(err, m.explicitShutdown.Value()) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:57:30 +00:00
|
|
|
// Shutdown is called to initiate the "happy path" of muxer termination.
|
2019-12-03 21:01:28 +00:00
|
|
|
// It blocks new streams from being created.
|
|
|
|
// It returns a channel that is closed when the last stream has been closed.
|
|
|
|
func (m *Muxer) Shutdown() <-chan struct{} {
|
2018-05-01 23:45:06 +00:00
|
|
|
m.explicitShutdown.Fuse(true)
|
2019-12-03 21:01:28 +00:00
|
|
|
return m.muxReader.Shutdown()
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsUnexpectedTunnelError identifies errors that are expected when shutting down the h2mux tunnel.
|
|
|
|
// The set of expected errors change depending on whether we initiated shutdown or not.
|
|
|
|
func isUnexpectedTunnelError(err error, expectedShutdown bool) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !expectedShutdown {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return !isConnectionClosedError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isConnectionClosedError(err error) bool {
|
|
|
|
if err == io.EOF {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err.Error() == "tls: use of closed connection" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenStream opens a new data stream with the given headers.
|
|
|
|
// Called by proxy server and tunnel
|
2019-04-02 23:12:09 +00:00
|
|
|
func (m *Muxer) OpenStream(ctx context.Context, headers []Header, body io.Reader) (*MuxedStream, error) {
|
2019-08-26 16:30:27 +00:00
|
|
|
stream := m.NewStream(headers)
|
2019-11-13 20:04:19 +00:00
|
|
|
if err := m.MakeMuxedStreamRequest(ctx, NewMuxedStreamRequest(stream, body)); err != nil {
|
2019-08-26 16:30:27 +00:00
|
|
|
return nil, err
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2019-08-26 16:30:27 +00:00
|
|
|
if err := m.AwaitResponseHeaders(ctx, stream); err != nil {
|
|
|
|
return nil, err
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2019-08-26 16:30:27 +00:00
|
|
|
return stream, nil
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 15:08:55 +00:00
|
|
|
func (m *Muxer) OpenRPCStream(ctx context.Context) (*MuxedStream, error) {
|
2019-08-26 16:30:27 +00:00
|
|
|
stream := m.NewStream(RPCHeaders())
|
2019-11-13 20:04:19 +00:00
|
|
|
if err := m.MakeMuxedStreamRequest(ctx, NewMuxedStreamRequest(stream, nil)); err != nil {
|
2019-08-26 16:30:27 +00:00
|
|
|
return nil, err
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
2019-08-26 16:30:27 +00:00
|
|
|
if err := m.AwaitResponseHeaders(ctx, stream); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stream, nil
|
|
|
|
}
|
2019-06-05 15:08:55 +00:00
|
|
|
|
2019-08-26 16:30:27 +00:00
|
|
|
func (m *Muxer) NewStream(headers []Header) *MuxedStream {
|
|
|
|
return NewStream(m.config, headers, m.readyList, m.muxReader.dictionaries)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Muxer) MakeMuxedStreamRequest(ctx context.Context, request MuxedStreamRequest) error {
|
2019-06-05 15:08:55 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-08-26 16:30:27 +00:00
|
|
|
return ErrStreamRequestTimeout
|
2019-06-05 15:08:55 +00:00
|
|
|
case <-m.abortChan:
|
2019-08-26 16:30:27 +00:00
|
|
|
return ErrStreamRequestConnectionClosed
|
|
|
|
// Will be received by mux writer
|
|
|
|
case m.newStreamChan <- request:
|
|
|
|
return nil
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
2019-08-26 16:30:27 +00:00
|
|
|
}
|
2019-06-05 15:08:55 +00:00
|
|
|
|
2019-11-04 15:20:38 +00:00
|
|
|
func (m *Muxer) CloseStreamRead(stream *MuxedStream) {
|
|
|
|
stream.CloseRead()
|
|
|
|
if stream.WriteClosed() {
|
|
|
|
m.streams.Delete(stream.streamID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 16:30:27 +00:00
|
|
|
func (m *Muxer) AwaitResponseHeaders(ctx context.Context, stream *MuxedStream) error {
|
2019-06-05 15:08:55 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-08-26 16:30:27 +00:00
|
|
|
return ErrResponseHeadersTimeout
|
2019-06-05 15:08:55 +00:00
|
|
|
case <-m.abortChan:
|
2019-08-26 16:30:27 +00:00
|
|
|
return ErrResponseHeadersConnectionClosed
|
2019-06-05 15:08:55 +00:00
|
|
|
case <-stream.responseHeadersReceived:
|
2019-08-26 16:30:27 +00:00
|
|
|
return nil
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
func (m *Muxer) Metrics() *MuxerMetrics {
|
2019-01-16 13:57:30 +00:00
|
|
|
return m.muxMetricsUpdater.metrics()
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Muxer) abort() {
|
|
|
|
m.abortOnce.Do(func() {
|
|
|
|
close(m.abortChan)
|
2019-01-16 13:57:30 +00:00
|
|
|
m.readyList.Close()
|
2018-05-01 23:45:06 +00:00
|
|
|
m.streams.Abort()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return how many retries/ticks since the connection was last marked active
|
|
|
|
func (m *Muxer) TimerRetries() uint64 {
|
|
|
|
return m.muxWriter.idleTimer.RetryCount()
|
|
|
|
}
|