mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
update xray v25.12.2 hysteria2 v2.6.4
This commit is contained in:
@@ -26,30 +26,4 @@ func (c *Conn) Read(b []byte) (n int, err error) {
|
||||
func (c *Conn) Write(b []byte) (n int, err error) {
|
||||
c.limiter.Wait(int64(len(b)))
|
||||
return c.Conn.Write(b)
|
||||
}
|
||||
|
||||
/*
|
||||
type PacketConnCounter struct {
|
||||
network.PacketConn
|
||||
limiter *ratelimit.Bucket
|
||||
}
|
||||
|
||||
func NewPacketConnCounter(conn network.PacketConn, l *ratelimit.Bucket) network.PacketConn {
|
||||
return &PacketConnCounter{
|
||||
PacketConn: conn,
|
||||
limiter: l,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PacketConnCounter) ReadPacket(buff *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||
pLen := buff.Len()
|
||||
destination, err = p.PacketConn.ReadPacket(buff)
|
||||
p.limiter.Wait(int64(buff.Len() - pLen))
|
||||
return destination, err
|
||||
}
|
||||
|
||||
func (p *PacketConnCounter) WritePacket(buff *buf.Buffer, destination M.Socksaddr) (err error) {
|
||||
p.limiter.Wait(int64(buff.Len()))
|
||||
return p.PacketConn.WritePacket(buff, destination)
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -5,49 +5,14 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Task is a task that runs periodically.
|
||||
type Task struct {
|
||||
// Interval of the task being run
|
||||
Interval time.Duration
|
||||
// Execute is the task function
|
||||
Execute func() error
|
||||
|
||||
access sync.Mutex
|
||||
timer *time.Timer
|
||||
running bool
|
||||
Execute func() error
|
||||
access sync.Mutex
|
||||
running bool
|
||||
stop chan struct{}
|
||||
}
|
||||
|
||||
func (t *Task) hasClosed() bool {
|
||||
t.access.Lock()
|
||||
defer t.access.Unlock()
|
||||
|
||||
return !t.running
|
||||
}
|
||||
|
||||
func (t *Task) checkedExecute(first bool) error {
|
||||
if t.hasClosed() {
|
||||
return nil
|
||||
}
|
||||
|
||||
t.access.Lock()
|
||||
defer t.access.Unlock()
|
||||
if first {
|
||||
if err := t.Execute(); err != nil {
|
||||
t.running = false
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !t.running {
|
||||
return nil
|
||||
}
|
||||
t.timer = time.AfterFunc(t.Interval, func() {
|
||||
t.checkedExecute(true)
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start implements common.Runnable.
|
||||
func (t *Task) Start(first bool) error {
|
||||
t.access.Lock()
|
||||
if t.running {
|
||||
@@ -55,24 +20,45 @@ func (t *Task) Start(first bool) error {
|
||||
return nil
|
||||
}
|
||||
t.running = true
|
||||
t.stop = make(chan struct{})
|
||||
t.access.Unlock()
|
||||
if err := t.checkedExecute(first); err != nil {
|
||||
t.access.Lock()
|
||||
t.running = false
|
||||
t.access.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
if first {
|
||||
if err := t.Execute(); err != nil {
|
||||
t.access.Lock()
|
||||
t.running = false
|
||||
close(t.stop)
|
||||
t.access.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-time.After(t.Interval):
|
||||
case <-t.stop:
|
||||
return
|
||||
}
|
||||
|
||||
if err := t.Execute(); err != nil {
|
||||
t.access.Lock()
|
||||
t.running = false
|
||||
close(t.stop)
|
||||
t.access.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close implements common.Closable.
|
||||
func (t *Task) Close() {
|
||||
t.access.Lock()
|
||||
defer t.access.Unlock()
|
||||
|
||||
t.running = false
|
||||
if t.timer != nil {
|
||||
t.timer.Stop()
|
||||
t.timer = nil
|
||||
if t.running {
|
||||
t.running = false
|
||||
close(t.stop)
|
||||
}
|
||||
}
|
||||
t.access.Unlock()
|
||||
}
|
||||
Reference in New Issue
Block a user