storj/private/server/tlsdebounce.go
JT Olio 0b4b04900a private/server: debounce noise and tls connections
to support TCP_FAST_OPEN, we're considering just using
two TCP connections in parallel per request, one with
and one without. this allows us to safely fire both
concurrently without stressing out the node too much.

see https://review.dev.storj.io/c/storj/storj/+/9933

Change-Id: I9aa8a0252350db5ace04ee125bfe469203e980ec
2023-03-21 16:51:31 +00:00

54 lines
1.1 KiB
Go

// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package server
import (
"net"
"github.com/spacemonkeygo/tlshowdy"
)
type tlsDebouncer struct {
net.Listener
debouncer func(addr net.Addr, message []byte) error
}
func tlsDebounce(l net.Listener, debouncer func(addr net.Addr, message []byte) error) net.Listener {
return &tlsDebouncer{
Listener: l,
debouncer: debouncer,
}
}
type tlsDebouncedConn struct {
net.Conn
prefixed net.Conn
debouncer func(addr net.Addr, message []byte) error
}
func (l *tlsDebouncer) Accept() (net.Conn, error) {
conn, err := l.Listener.Accept()
return &tlsDebouncedConn{
Conn: conn,
debouncer: l.debouncer,
}, err
}
func (l *tlsDebouncedConn) Read(p []byte) (n int, err error) {
if l.debouncer != nil {
rr := tlshowdy.NewRecordingReader(l.Conn)
_, err := tlshowdy.Read(rr)
if err != nil {
return 0, err
}
err = l.debouncer(l.RemoteAddr(), rr.Received)
if err != nil {
return 0, err
}
l.debouncer = nil
l.prefixed = tlshowdy.NewPrefixConn(rr.Received, l.Conn)
}
return l.prefixed.Read(p)
}