package pool
import (
"time"
"github.com/lightningnetwork/lnd/buffer"
)
type Read struct {
workerPool *Worker
bufferPool *ReadBuffer
}
func NewRead(readBufferPool *ReadBuffer, numWorkers int,
workerTimeout time.Duration) *Read {
r := &Read{
bufferPool: readBufferPool,
}
r.workerPool = NewWorker(&WorkerConfig{
NewWorkerState: r.newWorkerState,
NumWorkers: numWorkers,
WorkerTimeout: workerTimeout,
})
return r
}
func (r *Read) Start() error {
return r.workerPool.Start()
}
func (r *Read) Stop() error {
return r.workerPool.Stop()
}
func (r *Read) Submit(inner func(*buffer.Read) error) error {
return r.workerPool.Submit(func(s WorkerState) error {
state := s.(*readWorkerState)
return inner(state.readBuf)
})
}
type readWorkerState struct {
bufferPool *ReadBuffer
readBuf *buffer.Read
}
func (r *Read) newWorkerState() WorkerState {
return &readWorkerState{
bufferPool: r.bufferPool,
readBuf: r.bufferPool.Take(),
}
}
func (r *readWorkerState) Cleanup() {
r.bufferPool.Return(r.readBuf)
r.readBuf = nil
}
func (r *readWorkerState) Reset() {
r.readBuf.Recycle()
}