handle remote errors in Stream Write

This commit is contained in:
Marten Seemann
2016-05-03 16:34:46 +07:00
parent ef71186402
commit 6700672440
2 changed files with 21 additions and 0 deletions

View File

@@ -163,6 +163,9 @@ func (s *stream) Write(p []byte) (int, error) {
s.windowUpdateCond.L.Lock()
remainingBytesInWindow := int64(s.flowControlWindow) - int64(s.writeOffset)
for ; remainingBytesInWindow == 0; remainingBytesInWindow = int64(s.flowControlWindow) - int64(s.writeOffset) {
if s.remoteErr != nil {
return 0, s.remoteErr
}
s.windowUpdateCond.Wait()
}
s.windowUpdateCond.L.Unlock()
@@ -206,6 +209,7 @@ func (s *stream) AddStreamFrame(frame *frames.StreamFrame) error {
func (s *stream) RegisterError(err error) {
s.remoteErr = err
s.streamFrames <- nil
s.windowUpdateCond.Broadcast()
}
func (s *stream) finishedReading() bool {

View File

@@ -305,6 +305,23 @@ var _ = Describe("Stream", func() {
Expect(n).To(Equal(2))
Expect(err).ToNot(HaveOccurred())
})
It("immediately returns on remote errors", func() {
var b bool
str.flowControlWindow = 1
testErr := errors.New("test error")
go func() {
time.Sleep(time.Millisecond)
b = true
str.RegisterError(testErr)
}()
_, err := str.Write([]byte{0xDE, 0xCA, 0xFB, 0xAD})
Expect(b).To(BeTrue())
Expect(err).To(Equal(testErr))
})
})
})