mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
remove buffering
This commit is contained in:
@@ -1,79 +0,0 @@
|
|||||||
package torrstor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type buffer struct {
|
|
||||||
pieceId int
|
|
||||||
buf []byte
|
|
||||||
used bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type BufferPool struct {
|
|
||||||
buffs map[int]*buffer
|
|
||||||
frees int
|
|
||||||
size int64
|
|
||||||
mu sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBufferPool(bufferLength int64, capacity int64) *BufferPool {
|
|
||||||
bp := new(BufferPool)
|
|
||||||
buffsSize := int(capacity/bufferLength) + 4
|
|
||||||
bp.frees = buffsSize
|
|
||||||
bp.size = bufferLength
|
|
||||||
return bp
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BufferPool) mkBuffs() {
|
|
||||||
if b.buffs != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b.buffs = make(map[int]*buffer, b.frees)
|
|
||||||
fmt.Println("Create", b.frees, "buffers")
|
|
||||||
for i := 0; i < b.frees; i++ {
|
|
||||||
buf := buffer{
|
|
||||||
-1,
|
|
||||||
make([]byte, b.size),
|
|
||||||
false,
|
|
||||||
}
|
|
||||||
b.buffs[i] = &buf
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BufferPool) GetBuffer(p *Piece) (buff []byte, index int) {
|
|
||||||
b.mu.Lock()
|
|
||||||
defer b.mu.Unlock()
|
|
||||||
b.mkBuffs()
|
|
||||||
for id, buf := range b.buffs {
|
|
||||||
if !buf.used {
|
|
||||||
buf.used = true
|
|
||||||
buf.pieceId = p.Id
|
|
||||||
buff = buf.buf
|
|
||||||
index = id
|
|
||||||
b.frees--
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Println("Create slow buffer")
|
|
||||||
return make([]byte, b.size), -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BufferPool) ReleaseBuffer(index int) {
|
|
||||||
if index == -1 || b == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b.mu.Lock()
|
|
||||||
defer b.mu.Unlock()
|
|
||||||
b.mkBuffs()
|
|
||||||
if buff, ok := b.buffs[index]; ok {
|
|
||||||
buff.used = false
|
|
||||||
buff.pieceId = -1
|
|
||||||
b.frees++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BufferPool) Len() int {
|
|
||||||
return b.frees
|
|
||||||
}
|
|
||||||
@@ -25,8 +25,7 @@ type Cache struct {
|
|||||||
pieceLength int64
|
pieceLength int64
|
||||||
pieceCount int
|
pieceCount int
|
||||||
|
|
||||||
pieces map[int]*Piece
|
pieces map[int]*Piece
|
||||||
bufferPull *BufferPool
|
|
||||||
|
|
||||||
readers map[*Reader]struct{}
|
readers map[*Reader]struct{}
|
||||||
muReaders sync.Mutex
|
muReaders sync.Mutex
|
||||||
@@ -57,7 +56,6 @@ func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
|||||||
c.pieceLength = info.PieceLength
|
c.pieceLength = info.PieceLength
|
||||||
c.pieceCount = info.NumPieces()
|
c.pieceCount = info.NumPieces()
|
||||||
c.hash = hash
|
c.hash = hash
|
||||||
c.bufferPull = NewBufferPool(c.pieceLength, c.capacity)
|
|
||||||
|
|
||||||
for i := 0; i < c.pieceCount; i++ {
|
for i := 0; i < c.pieceCount; i++ {
|
||||||
c.pieces[i] = &Piece{
|
c.pieces[i] = &Piece{
|
||||||
@@ -86,7 +84,6 @@ func (c *Cache) Close() error {
|
|||||||
delete(c.storage.caches, c.hash)
|
delete(c.storage.caches, c.hash)
|
||||||
}
|
}
|
||||||
c.pieces = nil
|
c.pieces = nil
|
||||||
c.bufferPull = nil
|
|
||||||
|
|
||||||
c.muReaders.Lock()
|
c.muReaders.Lock()
|
||||||
c.readers = nil
|
c.readers = nil
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ type Piece struct {
|
|||||||
readed bool
|
readed bool
|
||||||
accessed int64
|
accessed int64
|
||||||
buffer []byte
|
buffer []byte
|
||||||
bufIndex int
|
|
||||||
|
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
cache *Cache
|
cache *Cache
|
||||||
@@ -34,10 +33,7 @@ func (p *Piece) WriteAt(b []byte, off int64) (n int, err error) {
|
|||||||
|
|
||||||
if p.buffer == nil {
|
if p.buffer == nil {
|
||||||
go p.cache.cleanPieces()
|
go p.cache.cleanPieces()
|
||||||
p.buffer, p.bufIndex = p.cache.bufferPull.GetBuffer(p)
|
p.buffer = make([]byte, p.cache.pieceLength)
|
||||||
if p.buffer == nil {
|
|
||||||
return 0, errors.New("Can't get buffer write")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
n = copy(p.buffer[off:], b[:])
|
n = copy(p.buffer[off:], b[:])
|
||||||
p.Size += int64(n)
|
p.Size += int64(n)
|
||||||
@@ -98,8 +94,6 @@ func (p *Piece) Release() {
|
|||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
if p.buffer != nil {
|
if p.buffer != nil {
|
||||||
p.buffer = nil
|
p.buffer = nil
|
||||||
p.cache.bufferPull.ReleaseBuffer(p.bufIndex)
|
|
||||||
p.bufIndex = -1
|
|
||||||
}
|
}
|
||||||
p.Size = 0
|
p.Size = 0
|
||||||
p.complete = false
|
p.complete = false
|
||||||
@@ -110,11 +104,3 @@ func (p *Piece) Release() {
|
|||||||
pce.UpdateCompletion()
|
pce.UpdateCompletion()
|
||||||
pce.SetPriority(torrent.PiecePriorityNone)
|
pce.SetPriority(torrent.PiecePriorityNone)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteToDisk(b []byte, off int64) (n int, err error) {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadFromDisk(b []byte, off int64) (n int, err error) {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user