Change algorithm of TCP relaying

This commit is contained in:
9seconds
2021-08-27 17:22:36 +03:00
parent 4b7be8c565
commit 456ed5b051
18 changed files with 300 additions and 434 deletions
+7 -23
View File
@@ -1,35 +1,19 @@
package relay
import (
"context"
"io"
"fmt"
"net"
"time"
)
type conn struct {
io.ReadWriteCloser
ctx context.Context
tickChannel chan struct{}
net.Conn
}
func (c conn) Read(p []byte) (int, error) {
n, err := c.ReadWriteCloser.Read(p)
select {
case <-c.ctx.Done():
case c.tickChannel <- struct{}{}:
if err := c.SetReadDeadline(time.Now().Add(getTimeout())); err != nil {
return 0, fmt.Errorf("cannot set read deadline: %w", err)
}
return n, err // nolint: wrapcheck
}
func (c conn) Write(p []byte) (int, error) {
n, err := c.ReadWriteCloser.Write(p)
select {
case <-c.ctx.Done():
case c.tickChannel <- struct{}{}:
}
return n, err // nolint: wrapcheck
return c.Conn.Read(p) // nolint: wrapcheck
}