From cd98e642cd74dcd3efa3b6a240db6bfcb189a057 Mon Sep 17 00:00:00 2001 From: xtaci Date: Sat, 18 Mar 2017 13:06:32 +0800 Subject: [PATCH] add SetWriteDelay --- sess.go | 12 +++++++++++- sess_test.go | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sess.go b/sess.go index a16b8e2..29458a1 100644 --- a/sess.go +++ b/sess.go @@ -90,6 +90,7 @@ type ( headerSize int // the overall header size added before KCP frame updateInterval time.Duration // interval in seconds to call kcp.flush() ackNoDelay bool // send ack immediately for each incoming packet + writeDelay bool // delay kcp.flush() for Write() for bulk transfer // notifications die chan struct{} // notify session has Closed @@ -256,7 +257,9 @@ func (s *UDPSession) Write(b []byte) (n int, err error) { } } - s.kcp.flush(false) + if !s.writeDelay { + s.kcp.flush(false) + } s.mu.Unlock() atomic.AddUint64(&DefaultSnmp.BytesSent, uint64(n)) return n, nil @@ -336,6 +339,13 @@ func (s *UDPSession) SetWriteDeadline(t time.Time) error { return nil } +// SetWriteDelay delays write for bulk transfer until the next update interval +func (s *UDPSession) SetWriteDelay(delay bool) { + s.mu.Lock() + defer s.mu.Unlock() + s.writeDelay = delay +} + // SetWindowSize set maximum window size func (s *UDPSession) SetWindowSize(sndwnd, rcvwnd int) { s.mu.Lock() diff --git a/sess_test.go b/sess_test.go index fcf7b6f..b27ed7f 100644 --- a/sess_test.go +++ b/sess_test.go @@ -270,6 +270,7 @@ func TestSendRecv(t *testing.T) { if err != nil { panic(err) } + cli.SetWriteDelay(true) const N = 100 buf := make([]byte, 10) for i := 0; i < N; i++ {