mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 11:34:01 +03:00
FILE / ScuroNeko/mtg
mtglib/internal/doppel/conn.go
Исходный файл и его история в репозитории.
- Use sync.Pool for relay buffers instead of stack-allocated arrays. A [16379]byte on the goroutine stack forces Go to grow it to 32KB (next power of two). Pooled buffers keep goroutine stacks small. - Same fix for doppelganger write buffer ([16384]byte in conn.start). - Replace idle goroutines with context.AfterFunc in proxy.ServeConn and relay.Relay. These goroutines existed only to wait on ctx.Done() and close connections. AfterFunc achieves the same without allocating a goroutine until the context is actually cancelled. Net effect: at 3000 concurrent connections on a 1-vCPU/961MB VPS, the unmodified binary drops 246 connections and falls to 10 MB/s. With these changes: zero failures, 63 MB/s, 31% lower RSS. Closes #412
126 lines
2.0 KiB
Go
126 lines
2.0 KiB
Go
package doppel
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/9seconds/mtg/v2/essentials"
|
|
"github.com/9seconds/mtg/v2/mtglib/internal/tls"
|
|
)
|
|
|
|
var doppelBufPool = sync.Pool{
|
|
New: func() any {
|
|
b := make([]byte, tls.MaxRecordSize)
|
|
return &b
|
|
},
|
|
}
|
|
|
|
type Conn struct {
|
|
essentials.Conn
|
|
|
|
p *connPayload
|
|
}
|
|
|
|
type connPayload struct {
|
|
ctx context.Context
|
|
ctxCancel context.CancelCauseFunc
|
|
clock Clock
|
|
wg sync.WaitGroup
|
|
writeStream bytes.Buffer
|
|
writtenCond sync.Cond
|
|
done bool
|
|
}
|
|
|
|
func (c Conn) Write(p []byte) (int, error) {
|
|
if len(p) == 0 {
|
|
return 0, context.Cause(c.p.ctx)
|
|
}
|
|
|
|
c.p.writtenCond.L.Lock()
|
|
c.p.writeStream.Write(p)
|
|
c.p.writtenCond.L.Unlock()
|
|
|
|
c.p.writtenCond.Signal()
|
|
|
|
return len(p), context.Cause(c.p.ctx)
|
|
}
|
|
|
|
func (c Conn) Start() {
|
|
c.p.wg.Go(func() {
|
|
c.start()
|
|
})
|
|
}
|
|
|
|
func (c Conn) start() {
|
|
bp := doppelBufPool.Get().(*[]byte)
|
|
buf := *bp
|
|
defer doppelBufPool.Put(bp)
|
|
|
|
for {
|
|
select {
|
|
case <-c.p.ctx.Done():
|
|
return
|
|
case <-c.p.clock.tick:
|
|
}
|
|
|
|
size := c.p.clock.stats.Size()
|
|
|
|
c.p.writtenCond.L.Lock()
|
|
for c.p.writeStream.Len() == 0 && !c.p.done {
|
|
c.p.writtenCond.Wait()
|
|
}
|
|
n, _ := c.p.writeStream.Read(buf[tls.SizeHeader : tls.SizeHeader+size])
|
|
c.p.writtenCond.L.Unlock()
|
|
|
|
if n == 0 {
|
|
continue
|
|
}
|
|
|
|
if err := tls.WriteRecordInPlace(c.Conn, buf, n); err != nil {
|
|
c.p.ctxCancel(err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c Conn) Stop() {
|
|
c.p.ctxCancel(nil)
|
|
|
|
c.p.writtenCond.L.Lock()
|
|
c.p.done = true
|
|
c.p.writtenCond.L.Unlock()
|
|
c.p.writtenCond.Broadcast()
|
|
|
|
c.p.wg.Wait()
|
|
}
|
|
|
|
func NewConn(ctx context.Context, conn essentials.Conn, stats *Stats) Conn {
|
|
ctx, cancel := context.WithCancelCause(ctx)
|
|
rv := Conn{
|
|
Conn: conn,
|
|
p: &connPayload{
|
|
ctx: ctx,
|
|
ctxCancel: cancel,
|
|
writtenCond: sync.Cond{
|
|
L: &sync.Mutex{},
|
|
},
|
|
clock: Clock{
|
|
stats: stats,
|
|
tick: make(chan struct{}),
|
|
},
|
|
},
|
|
}
|
|
|
|
rv.p.writeStream.Grow(tls.DefaultBufferSize)
|
|
|
|
rv.p.wg.Go(func() {
|
|
rv.p.clock.Start(ctx)
|
|
})
|
|
rv.p.wg.Go(func() {
|
|
rv.start()
|
|
})
|
|
|
|
return rv
|
|
}
|