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

master
DataHoarder 6 months ago
parent 6ea83ecce7
commit 0e5c9c69c7
Signed by: DataHoarder
SSH Key Fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

@ -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"
@ -32,18 +31,12 @@ func main() {
defer wg.Done()
server := http.Server{
Addr: fmt.Sprintf("%s:%d", conf.Radio.Host, conf.Radio.Port),
Handler: http.HandlerFunc(queueInstance.HandleRadioRequest),
Addr: fmt.Sprintf("%s:%d", conf.Radio.Host, conf.Radio.Port),
Handler: http.HandlerFunc(queueInstance.HandleRadioRequest),
ReadTimeout: time.Second * 5,
}
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)
}
if err = server.Serve(timeoutListener); err != nil {
if err = server.ListenAndServe(); err != nil {
log.Panic(err)
}
}()

@ -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
}
Loading…
Cancel
Save