mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-30 21:58:39 +00:00
WebSocket transport, XHTTP server: Reduce some goroutines by switching to task.Periodic (#7470)
https://github.com/XTLS/Xray-core/pull/7463#issuecomment-7081595080 https://github.com/XTLS/Xray-core/pull/7470#issuecomment-7131388265 https://github.com/XTLS/Xray-core/pull/7470#issuecomment-7163168321 --------- Co-authored-by: StarProxima <34741787+StarProxima@users.noreply.github.com>
This commit is contained in:
@@ -71,7 +71,6 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
|||||||
}
|
}
|
||||||
common.CloseIfExists(t.checkTask)
|
common.CloseIfExists(t.checkTask)
|
||||||
t.checkTask = newCheckTask
|
t.checkTask = newCheckTask
|
||||||
t.Update()
|
|
||||||
common.Must(newCheckTask.Start())
|
common.Must(newCheckTask.Start())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,15 +58,11 @@ func (t *Periodic) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
t.running = true
|
t.running = true
|
||||||
|
t.timer = time.AfterFunc(t.Interval, func() {
|
||||||
|
t.checkedExecute()
|
||||||
|
})
|
||||||
t.access.Unlock()
|
t.access.Unlock()
|
||||||
|
|
||||||
if err := t.checkedExecute(); err != nil {
|
|
||||||
t.access.Lock()
|
|
||||||
t.running = false
|
|
||||||
t.access.Unlock()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,17 +20,17 @@ func TestPeriodicTaskStop(t *testing.T) {
|
|||||||
common.Must(task.Start())
|
common.Must(task.Start())
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
common.Must(task.Close())
|
common.Must(task.Close())
|
||||||
if value != 3 {
|
if value != 2 {
|
||||||
t.Fatal("expected 3, but got ", value)
|
t.Fatal("expected 2, but got ", value)
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second * 4)
|
time.Sleep(time.Second * 4)
|
||||||
if value != 3 {
|
if value != 2 {
|
||||||
t.Fatal("expected 3, but got ", value)
|
t.Fatal("expected 2, but got ", value)
|
||||||
}
|
}
|
||||||
common.Must(task.Start())
|
common.Must(task.Start())
|
||||||
time.Sleep(time.Second * 3)
|
time.Sleep(time.Second * 3)
|
||||||
if value != 5 {
|
if value != 3 {
|
||||||
t.Fatal("Expected 5, but ", value)
|
t.Fatal("Expected 3, but ", value)
|
||||||
}
|
}
|
||||||
common.Must(task.Close())
|
common.Must(task.Close())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
"github.com/xtls/xray-core/common/net"
|
"github.com/xtls/xray-core/common/net"
|
||||||
http_proto "github.com/xtls/xray-core/common/protocol/http"
|
http_proto "github.com/xtls/xray-core/common/protocol/http"
|
||||||
"github.com/xtls/xray-core/common/signal/done"
|
"github.com/xtls/xray-core/common/signal/done"
|
||||||
|
"github.com/xtls/xray-core/common/task"
|
||||||
"github.com/xtls/xray-core/transport/internet"
|
"github.com/xtls/xray-core/transport/internet"
|
||||||
"github.com/xtls/xray-core/transport/internet/hysteria/congestion"
|
"github.com/xtls/xray-core/transport/internet/hysteria/congestion"
|
||||||
"github.com/xtls/xray-core/transport/internet/hysteria/congestion/bbr"
|
"github.com/xtls/xray-core/transport/internet/hysteria/congestion/bbr"
|
||||||
@@ -78,10 +79,9 @@ func (h *requestHandler) upsertSession(sessionId string) *httpSession {
|
|||||||
h.sessions.Store(sessionId, s)
|
h.sessions.Store(sessionId, s)
|
||||||
|
|
||||||
shouldReap := done.New()
|
shouldReap := done.New()
|
||||||
go func() {
|
time.AfterFunc(30*time.Second, func() {
|
||||||
time.Sleep(30 * time.Second)
|
|
||||||
shouldReap.Close()
|
shouldReap.Close()
|
||||||
}()
|
})
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
select {
|
||||||
case <-shouldReap.Wait():
|
case <-shouldReap.Wait():
|
||||||
@@ -222,15 +222,15 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
|||||||
scStreamUpServerSecs := h.config.GetNormalizedScStreamUpServerSecs()
|
scStreamUpServerSecs := h.config.GetNormalizedScStreamUpServerSecs()
|
||||||
hasLegacyRefererCompatMarker := request.Header.Get("Referer") != ""
|
hasLegacyRefererCompatMarker := request.Header.Get("Referer") != ""
|
||||||
if (hasLegacyRefererCompatMarker || obfsPaddingAccepted) && scStreamUpServerSecs.To > 0 {
|
if (hasLegacyRefererCompatMarker || obfsPaddingAccepted) && scStreamUpServerSecs.To > 0 {
|
||||||
go func() {
|
writePadding := &task.Periodic{
|
||||||
for {
|
Execute: func() error {
|
||||||
_, err := httpSC.Write(bytes.Repeat([]byte{'X'}, int(h.config.GetNormalizedXPaddingBytes().rand())))
|
_, err := httpSC.Write(bytes.Repeat([]byte{'X'}, int(h.config.GetNormalizedXPaddingBytes().rand())))
|
||||||
if err != nil {
|
return err
|
||||||
break
|
},
|
||||||
}
|
Interval: time.Duration(scStreamUpServerSecs.rand()) * time.Second,
|
||||||
time.Sleep(time.Duration(scStreamUpServerSecs.rand()) * time.Second)
|
}
|
||||||
}
|
defer writePadding.Close()
|
||||||
}()
|
writePadding.Start()
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case <-request.Context().Done():
|
case <-request.Context().Done():
|
||||||
|
|||||||
@@ -6,9 +6,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/xtls/xray-core/common"
|
||||||
"github.com/xtls/xray-core/common/buf"
|
"github.com/xtls/xray-core/common/buf"
|
||||||
"github.com/xtls/xray-core/common/errors"
|
"github.com/xtls/xray-core/common/errors"
|
||||||
"github.com/xtls/xray-core/common/serial"
|
"github.com/xtls/xray-core/common/serial"
|
||||||
|
"github.com/xtls/xray-core/common/task"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ buf.Writer = (*connection)(nil)
|
var _ buf.Writer = (*connection)(nil)
|
||||||
@@ -20,25 +22,27 @@ type connection struct {
|
|||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
remoteAddr net.Addr
|
remoteAddr net.Addr
|
||||||
|
keepalive *task.Periodic
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnection(conn *websocket.Conn, remoteAddr net.Addr, extraReader io.Reader, heartbeatPeriod uint32) *connection {
|
func NewConnection(conn *websocket.Conn, remoteAddr net.Addr, extraReader io.Reader, heartbeatPeriod uint32) *connection {
|
||||||
if heartbeatPeriod != 0 {
|
wrappedConn := &connection{
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
time.Sleep(time.Duration(heartbeatPeriod) * time.Second)
|
|
||||||
if err := conn.WriteControl(websocket.PingMessage, []byte{}, time.Time{}); err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
return &connection{
|
|
||||||
conn: conn,
|
conn: conn,
|
||||||
remoteAddr: remoteAddr,
|
remoteAddr: remoteAddr,
|
||||||
reader: extraReader,
|
reader: extraReader,
|
||||||
}
|
}
|
||||||
|
if heartbeatPeriod != 0 {
|
||||||
|
keepalive := task.Periodic{
|
||||||
|
Execute: func() error {
|
||||||
|
return conn.WriteControl(websocket.PingMessage, []byte{}, time.Time{})
|
||||||
|
},
|
||||||
|
Interval: time.Duration(heartbeatPeriod) * time.Second,
|
||||||
|
}
|
||||||
|
keepalive.Start()
|
||||||
|
wrappedConn.keepalive = &keepalive
|
||||||
|
}
|
||||||
|
|
||||||
|
return wrappedConn
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read implements net.Conn.Read()
|
// Read implements net.Conn.Read()
|
||||||
@@ -93,6 +97,7 @@ func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *connection) Close() error {
|
func (c *connection) Close() error {
|
||||||
|
common.CloseIfExists(c.keepalive)
|
||||||
var errs []interface{}
|
var errs []interface{}
|
||||||
if err := c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
|
if err := c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user