mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 16:01:55 +03:00
Proxy is working in a simple mode now
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var relayPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &Relay{
|
||||
tickChannel: make(chan struct{}),
|
||||
errorChannel: make(chan error, 1),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func AcquireRelay(ctx context.Context, logger Logger, bufferSize int, idleTimeout time.Duration) *Relay {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
r := relayPool.Get().(*Relay)
|
||||
r.ctx = ctx
|
||||
r.ctxCancel = cancel
|
||||
r.logger = logger
|
||||
r.tickTimeout = idleTimeout
|
||||
|
||||
if len(r.eastBuffer) != bufferSize {
|
||||
r.eastBuffer = make([]byte, bufferSize)
|
||||
}
|
||||
|
||||
if len(r.westBuffer) != bufferSize {
|
||||
r.westBuffer = make([]byte, bufferSize)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func ReleaseRelay(r *Relay) {
|
||||
r.ctxCancel()
|
||||
|
||||
r.ctx = nil
|
||||
r.ctxCancel = nil
|
||||
r.logger = nil
|
||||
|
||||
relayPool.Put(r)
|
||||
}
|
||||
Reference in New Issue
Block a user