mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 17:34:01 +03:00
Optimize for a fast flush
This commit is contained in:
@@ -3,8 +3,8 @@ package relay
|
||||
import "time"
|
||||
|
||||
const (
|
||||
copyBufferSize = 32 * 1024
|
||||
writerBufferSize = 2 * copyBufferSize
|
||||
copyBufferSize = 64 * 1024
|
||||
writerBufferSize = 128 * 1024
|
||||
readTimeout = 10 * time.Millisecond
|
||||
)
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ func (s *syncPair) Sync() (int64, error) {
|
||||
func (s *syncPair) Read(p []byte) (int, error) {
|
||||
n, err := s.readBlocking(p, false)
|
||||
|
||||
// nothing has been delivered for readTimeout time. Let's flush.
|
||||
if errors.Is(err, os.ErrDeadlineExceeded) {
|
||||
if err := s.Flush(); err != nil {
|
||||
return 0, fmt.Errorf("cannot flush writer hand-side: %w", err)
|
||||
@@ -41,7 +42,17 @@ func (s *syncPair) Write(p []byte) (int, error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
return s.writer.Write(p) // nolint: wrapcheck
|
||||
n, err := s.writer.Write(p) // nolint: wrapcheck
|
||||
|
||||
// optimization for a case when we have a small package and want to avoid a
|
||||
// delay in readTimeout. In that case, we assume that peer has finished to
|
||||
// sent a data it wants to send so we can flush without waiting for anything
|
||||
// else.
|
||||
if err == nil && n < copyBufferSize {
|
||||
err = s.writer.Flush()
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (s *syncPair) Flush() error {
|
||||
|
||||
Reference in New Issue
Block a user