Remove custom tcp listener, MeteorLight is expected to sit under reverse proxies

This commit is contained in:
DataHoarder 2022-10-02 14:34:13 +02:00
parent 6ea83ecce7
commit 0e5c9c69c7
Signed by: DataHoarder
SSH Key Fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
2 changed files with 4 additions and 107 deletions

View File

@ -6,7 +6,6 @@ import (
"git.gammaspectra.live/S.O.N.G/MeteorLight/api"
"git.gammaspectra.live/S.O.N.G/MeteorLight/config"
"git.gammaspectra.live/S.O.N.G/MeteorLight/queue"
"git.gammaspectra.live/S.O.N.G/MeteorLight/util"
"log"
"net/http"
"sync"
@ -34,16 +33,10 @@ func main() {
server := http.Server{
Addr: fmt.Sprintf("%s:%d", conf.Radio.Host, conf.Radio.Port),
Handler: http.HandlerFunc(queueInstance.HandleRadioRequest),
}
server.SetKeepAlivesEnabled(false)
//setup a timeout to prevent slow clients blocking. See https://github.com/golang/go/issues/16100
timeoutListener, err := util.NewTimeoutListener("tcp", server.Addr, time.Second*15, time.Second*15)
if err != nil {
log.Panic(err)
ReadTimeout: time.Second * 5,
}
if err = server.Serve(timeoutListener); err != nil {
if err = server.ListenAndServe(); err != nil {
log.Panic(err)
}
}()

View File

@ -1,96 +0,0 @@
package util
import (
"log"
"net"
"sync/atomic"
"time"
)
type listener struct {
net.Listener
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func (l *listener) Accept() (net.Conn, error) {
c, err := l.Listener.Accept()
if err != nil {
return nil, err
}
if tcpConnection, ok := c.(*net.TCPConn); ok {
tcpConnection.SetReadBuffer(1024 * 64)
tcpConnection.SetWriteBuffer(1024 * 64)
tcpConnection.SetKeepAlive(true)
tcpConnection.SetKeepAlivePeriod(l.WriteTimeout / 4)
tcpConnection.SetNoDelay(true)
tcpConnection.SetLinger(0)
}
log.Printf("accepted new connection from %s\n", c.RemoteAddr().String())
tc := &Conn{
Conn: c,
ReadTimeout: l.ReadTimeout,
WriteTimeout: l.WriteTimeout,
ReadThreshold: int32((l.ReadTimeout * 1024) / time.Second),
WriteThreshold: int32((l.WriteTimeout * 1024) / time.Second),
}
return tc, nil
}
// Conn wraps a net.Conn, and sets a deadline for every read
// and write operation.
type Conn struct {
net.Conn
ReadTimeout time.Duration
WriteTimeout time.Duration
ReadThreshold int32
WriteThreshold int32
BytesReadFromDeadline atomic.Int32
BytesWrittenFromDeadline atomic.Int32
}
func (c *Conn) Read(b []byte) (n int, err error) {
if c.BytesReadFromDeadline.Load() > c.ReadThreshold {
c.BytesReadFromDeadline.Store(0)
// we set both read and write deadlines here otherwise after the request
// is read writing the response fails with an i/o timeout error
err = c.Conn.SetDeadline(time.Now().Add(c.ReadTimeout))
if err != nil {
return 0, err
}
}
n, err = c.Conn.Read(b)
c.BytesReadFromDeadline.Add(int32(n))
return
}
func (c *Conn) Write(b []byte) (n int, err error) {
if c.BytesWrittenFromDeadline.Load() > c.WriteThreshold {
c.BytesWrittenFromDeadline.Store(0)
// we extend the read deadline too, not sure it's necessary,
// but it doesn't hurt
err = c.Conn.SetDeadline(time.Now().Add(c.WriteTimeout))
if err != nil {
return
}
}
n, err = c.Conn.Write(b)
c.BytesWrittenFromDeadline.Add(int32(n))
return
}
func NewTimeoutListener(network, addr string, readTimeout, writeTimeout time.Duration) (net.Listener, error) {
l, err := net.Listen(network, addr)
if err != nil {
return nil, err
}
tl := &listener{
Listener: l,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
}
return tl, nil
}