copyWithLatency copies data from src to dst, injecting a delay before each write. For zero latency this degrades to a plain io.Copy.
(dst, src net.Conn, latency time.Duration)
| 109 | // copyWithLatency copies data from src to dst, injecting a delay before each |
| 110 | // write. For zero latency this degrades to a plain io.Copy. |
| 111 | func copyWithLatency(dst, src net.Conn, latency time.Duration) { |
| 112 | if latency <= 0 { |
| 113 | _, _ = io.Copy(dst, src) |
| 114 | return |
| 115 | } |
| 116 | buf := make([]byte, 32*1024) |
| 117 | for { |
| 118 | n, readErr := src.Read(buf) |
| 119 | if n > 0 { |
| 120 | time.Sleep(latency) |
| 121 | _, writeErr := dst.Write(buf[:n]) |
| 122 | if writeErr != nil { |
| 123 | return |
| 124 | } |
| 125 | } |
| 126 | if readErr != nil { |
| 127 | return |
| 128 | } |
| 129 | } |
| 130 | } |
no test coverage detected