add SetWriteDelay

This commit is contained in:
xtaci
2017-03-18 13:06:32 +08:00
parent 962ede4ec7
commit cd98e642cd
2 changed files with 12 additions and 1 deletions
+11 -1
View File
@@ -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()
+1
View File
@@ -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++ {