From 370b7f5725979232ba0a21586f967e43cade9527 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 29 May 2025 16:10:32 +1000 Subject: [PATCH] Disable shared file reader for now --- storage/file-handle-cache.go | 47 +++++++++++++++++++++++++----------- storage/file-torrent-io.go | 5 +--- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/storage/file-handle-cache.go b/storage/file-handle-cache.go index 97c15065..dbc3354b 100644 --- a/storage/file-handle-cache.go +++ b/storage/file-handle-cache.go @@ -14,24 +14,29 @@ import ( ) var ( - sharedFiles = sharedFilesType{ - m: make(map[string]*sharedFile), - } + sharedFiles sharedFilesInterface = regularFsSharedFiles{} + + wipSharedFilesPool = sharedFilesType{m: make(map[string]*sharedFile)} ) +type regularFsSharedFiles struct{} + +func (r regularFsSharedFiles) Open(name string) (sharedFileIf, error) { + return os.Open(name) +} + +type sharedFileIf interface { + io.ReaderAt + io.Closer +} + +type sharedFilesInterface interface { + Open(name string) (sharedFileIf, error) +} + func init() { http.HandleFunc("/debug/shared-files", func(w http.ResponseWriter, r *http.Request) { - sharedFiles.mu.Lock() - defer sharedFiles.mu.Unlock() - byRefs := slices.SortedFunc(maps.Keys(sharedFiles.m), func(a, b string) int { - return cmp.Or( - sharedFiles.m[b].refs-sharedFiles.m[a].refs, - cmp.Compare(a, b)) - }) - for _, key := range byRefs { - sf := sharedFiles.m[key] - fmt.Fprintf(w, "%v: refs=%v, name=%v\n", key, sf.refs, sf.f.Name()) - } + wipSharedFilesPool.WriteDebug(w) }) } @@ -40,6 +45,20 @@ type sharedFilesType struct { m map[string]*sharedFile } +func (sharedFiles *sharedFilesType) WriteDebug(w io.Writer) { + sharedFiles.mu.Lock() + defer sharedFiles.mu.Unlock() + byRefs := slices.SortedFunc(maps.Keys(sharedFiles.m), func(a, b string) int { + return cmp.Or( + sharedFiles.m[b].refs-sharedFiles.m[a].refs, + cmp.Compare(a, b)) + }) + for _, key := range byRefs { + sf := sharedFiles.m[key] + fmt.Fprintf(w, "%v: refs=%v, name=%v\n", key, sf.refs, sf.f.Name()) + } +} + // How many opens wouldn't have been needed with singleflight. var sharedFilesWastedOpens = expvar.NewInt("sharedFilesWastedOpens") diff --git a/storage/file-torrent-io.go b/storage/file-torrent-io.go index 96e1acd0..e57690e0 100644 --- a/storage/file-torrent-io.go +++ b/storage/file-torrent-io.go @@ -18,10 +18,7 @@ type fileTorrentImplIO struct { // Returns EOF on short or missing file. func (fst fileTorrentImplIO) readFileAt(file file, b []byte, off int64) (n int, err error) { fst.fts.logger().Debug("readFileAt", "file.safeOsPath", file.safeOsPath) - var f interface { - io.ReaderAt - io.Closer - } + var f sharedFileIf // Fine to open once under each name on a unix system. We could make the shared file keys more // constrained but it shouldn't matter. TODO: Ensure at most one of the names exist. if fst.fts.partFiles() { -- 2.51.0