|
|
@@ -26,6 +26,7 @@ func (s *syncPair) Sync() (int64, error) {
|
|
26
|
26
|
func (s *syncPair) Read(p []byte) (int, error) {
|
|
27
|
27
|
n, err := s.readBlocking(p, false)
|
|
28
|
28
|
|
|
|
29
|
+ // nothing has been delivered for readTimeout time. Let's flush.
|
|
29
|
30
|
if errors.Is(err, os.ErrDeadlineExceeded) {
|
|
30
|
31
|
if err := s.Flush(); err != nil {
|
|
31
|
32
|
return 0, fmt.Errorf("cannot flush writer hand-side: %w", err)
|
|
|
@@ -41,7 +42,17 @@ func (s *syncPair) Write(p []byte) (int, error) {
|
|
41
|
42
|
s.mutex.Lock()
|
|
42
|
43
|
defer s.mutex.Unlock()
|
|
43
|
44
|
|
|
44
|
|
- return s.writer.Write(p) // nolint: wrapcheck
|
|
|
45
|
+ n, err := s.writer.Write(p) // nolint: wrapcheck
|
|
|
46
|
+
|
|
|
47
|
+ // optimization for a case when we have a small package and want to avoid a
|
|
|
48
|
+ // delay in readTimeout. In that case, we assume that peer has finished to
|
|
|
49
|
+ // sent a data it wants to send so we can flush without waiting for anything
|
|
|
50
|
+ // else.
|
|
|
51
|
+ if err == nil && n < copyBufferSize {
|
|
|
52
|
+ err = s.writer.Flush()
|
|
|
53
|
+ }
|
|
|
54
|
+
|
|
|
55
|
+ return n, err
|
|
45
|
56
|
}
|
|
46
|
57
|
|
|
47
|
58
|
func (s *syncPair) Flush() error {
|