Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust dataIn buffer size for the actual allotment of buffers #101

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions multiplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ var ErrInvalidState = errors.New("received an unexpected message from the peer")
var errTimeout = timeout{}

var (
ResetStreamTimeout = 2 * time.Minute

WriteCoalesceDelay = 100 * time.Microsecond
ResetStreamTimeout = 2 * time.Minute
ReadDeadlockTimeout = 2 * time.Minute
)

type timeout struct{}
Expand Down Expand Up @@ -92,7 +91,10 @@ type Multiplex struct {
channels map[streamID]*Stream
chLock sync.Mutex

timerIn *time.Timer
timerInFired bool
bufIn, bufOut chan struct{}
bufMax int
reservedMemory int
}

Expand Down Expand Up @@ -137,8 +139,10 @@ func NewMultiplex(con net.Conn, initiator bool, memoryManager MemoryManager) (*M
return nil, err
}

mp.bufMax = bufs - 1
mp.bufIn = make(chan struct{}, bufs)
mp.bufOut = make(chan struct{}, bufs)
mp.timerIn = time.NewTimer(ReadDeadlockTimeout)

go mp.handleIncoming()
go mp.handleOutgoing()
Expand All @@ -150,7 +154,7 @@ func (mp *Multiplex) newStream(id streamID, name string) (s *Stream) {
s = &Stream{
id: id,
name: name,
dataIn: make(chan []byte, 8),
dataIn: make(chan []byte, mp.bufMax),
rDeadline: makePipeDeadline(),
wDeadline: makePipeDeadline(),
mp: mp,
Expand Down Expand Up @@ -530,8 +534,16 @@ func (mp *Multiplex) readNext() ([]byte, error) {
}

func (mp *Multiplex) getBufferInbound(length int) ([]byte, error) {
if !mp.timerInFired && !mp.timerIn.Stop() {
<-mp.timerIn.C
}
mp.timerIn.Reset(ReadDeadlockTimeout)

select {
case mp.bufIn <- struct{}{}:
case <-mp.timerIn.C:
mp.timerInFired = true
return nil, errTimeout
case <-mp.shutdown:
return nil, ErrShutdown
}
Expand Down