From fb982aa1108db41020915ebf59d84ee6f1c51b33 Mon Sep 17 00:00:00 2001 From: yourok <8yourok8@mail.ru> Date: Sat, 24 Feb 2024 11:04:33 +0300 Subject: [PATCH] change to use readers --- server/torr/storage/torrstor/cache.go | 15 +++++++++++++++ server/torr/torrent.go | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/server/torr/storage/torrstor/cache.go b/server/torr/storage/torrstor/cache.go index 5a16480..3019570 100644 --- a/server/torr/storage/torrstor/cache.go +++ b/server/torr/storage/torrstor/cache.go @@ -314,6 +314,21 @@ func (c *Cache) NewReader(file *torrent.File) *Reader { return newReader(file, c) } +func (c *Cache) GetUseReaders() int { + if c == nil { + return 0 + } + c.muReaders.Lock() + defer c.muReaders.Unlock() + readers := 0 + for reader := range c.readers { + if reader.isUse { + readers++ + } + } + return readers +} + func (c *Cache) Readers() int { if c == nil { return 0 diff --git a/server/torr/torrent.go b/server/torr/torrent.go index e01ee59..84a5e43 100644 --- a/server/torr/torrent.go +++ b/server/torr/torrent.go @@ -267,7 +267,7 @@ func (t *Torrent) drop() { } func (t *Torrent) Close() bool { - if t.cache != nil && t.cache.Readers() > 0 { + if t.cache != nil && t.cache.GetUseReaders() > 0 { return false } t.Stat = state.TorrentClosed