mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 13:54:02 +03:00
FILE / ScuroNeko/mtg
mtglib/internal/relay/pools.go
Исходный файл и его история в репозитории.
47 lines
770 B
Go
47 lines
770 B
Go
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)
|
|
}
|