mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-30 21:58:39 +00:00
refactor
This commit is contained in:
@@ -91,6 +91,7 @@ func (b *Bridge) monitor() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bridge) Start() error {
|
func (b *Bridge) Start() error {
|
||||||
|
b.monitor()
|
||||||
return b.monitorTask.Start()
|
return b.monitorTask.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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():
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package websocket
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@@ -33,14 +32,8 @@ func NewConnection(conn *websocket.Conn, remoteAddr net.Addr, extraReader io.Rea
|
|||||||
reader: extraReader,
|
reader: extraReader,
|
||||||
}
|
}
|
||||||
if heartbeatPeriod != 0 {
|
if heartbeatPeriod != 0 {
|
||||||
var checked atomic.Bool
|
|
||||||
keepalive := task.Periodic{
|
keepalive := task.Periodic{
|
||||||
Execute: func() error {
|
Execute: func() error {
|
||||||
// task.Periodic will try to execute this function immediately, drop it
|
|
||||||
if !checked.Load() {
|
|
||||||
checked.Store(true)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return conn.WriteControl(websocket.PingMessage, []byte{}, time.Time{})
|
return conn.WriteControl(websocket.PingMessage, []byte{}, time.Time{})
|
||||||
},
|
},
|
||||||
Interval: time.Duration(heartbeatPeriod) * time.Second,
|
Interval: time.Duration(heartbeatPeriod) * time.Second,
|
||||||
|
|||||||
Reference in New Issue
Block a user