Compare commits

..

No commits in common. "c65fd898873beada54b0b0828aca5f2c7215ef15" and "75d17267d7e597f187b17e113e412e5dea6b91e6" have entirely different histories.

2 changed files with 28 additions and 77 deletions

View file

@ -25,6 +25,9 @@ jobs:
# - linux/riscv64
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
@ -35,9 +38,6 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.PACKAGE_TOKEN }}
- name: Checkout repository
uses: actions/checkout@v3
- name: Docker meta
id: meta
uses: https://github.com/docker/metadata-action@v5

View file

@ -8,7 +8,6 @@ import (
"slices"
"strings"
"sync"
"sync/atomic"
"time"
"git.kmsign.ru/royalcat/tstor/src/host/controller"
@ -26,8 +25,6 @@ type TorrentFs struct {
filesCache map[string]File
lastAccessTimeout atomic.Pointer[time.Time]
resolver *resolver
}
@ -251,104 +248,62 @@ func (fs *TorrentFs) traceAttrs(add ...attribute.KeyValue) trace.SpanStartOption
}
// Stat implements Filesystem.
func (tfs *TorrentFs) Stat(ctx context.Context, filename string) (fs.FileInfo, error) {
func (fs *TorrentFs) Stat(ctx context.Context, filename string) (fs.FileInfo, error) {
ctx, span := tracer.Start(ctx, "Stat",
tfs.traceAttrs(attribute.String("filename", filename)),
fs.traceAttrs(attribute.String("filename", filename)),
)
defer span.End()
if isRoot(filename) {
return tfs, nil
return fs, nil
}
fsPath, nestedFs, nestedFsPath, err := tfs.resolver.resolvePath(ctx, filename, tfs.rawOpen)
fsPath, nestedFs, nestedFsPath, err := fs.resolver.resolvePath(ctx, filename, fs.rawOpen)
if err != nil {
return nil, err
}
if nestedFs != nil {
lastReadTimeout := tfs.lastAccessTimeout.Load()
if lastReadTimeout != nil && time.Since(*lastReadTimeout) < secondaryTimeout { // make short timeout for already faliled files
span.SetAttributes(attribute.Bool("short_timeout", true))
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Millisecond)
defer cancel()
}
defer func() {
if err == context.DeadlineExceeded {
now := time.Now()
tfs.lastAccessTimeout.Store(&now)
}
}()
return nestedFs.Stat(ctx, nestedFsPath)
}
return tfs.rawStat(ctx, fsPath)
return fs.rawStat(ctx, fsPath)
}
func (tfs *TorrentFs) Open(ctx context.Context, filename string) (file File, err error) {
func (fs *TorrentFs) Open(ctx context.Context, filename string) (File, error) {
ctx, span := tracer.Start(ctx, "Open",
tfs.traceAttrs(attribute.String("filename", filename)),
fs.traceAttrs(attribute.String("filename", filename)),
)
defer span.End()
if isRoot(filename) {
return newDirFile(tfs.name), nil
return newDirFile(fs.name), nil
}
fsPath, nestedFs, nestedFsPath, err := tfs.resolver.resolvePath(ctx, filename, tfs.rawOpen)
fsPath, nestedFs, nestedFsPath, err := fs.resolver.resolvePath(ctx, filename, fs.rawOpen)
if err != nil {
return nil, err
}
if nestedFs != nil {
lastReadTimeout := tfs.lastAccessTimeout.Load()
if lastReadTimeout != nil && time.Since(*lastReadTimeout) < secondaryTimeout { // make short timeout for already faliled files
span.SetAttributes(attribute.Bool("short_timeout", true))
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Millisecond)
defer cancel()
}
defer func() {
if err == context.DeadlineExceeded {
now := time.Now()
tfs.lastAccessTimeout.Store(&now)
}
}()
return nestedFs.Open(ctx, nestedFsPath)
}
return tfs.rawOpen(ctx, fsPath)
return fs.rawOpen(ctx, fsPath)
}
func (tfs *TorrentFs) ReadDir(ctx context.Context, name string) ([]fs.DirEntry, error) {
func (fs *TorrentFs) ReadDir(ctx context.Context, name string) ([]fs.DirEntry, error) {
ctx, span := tracer.Start(ctx, "ReadDir",
tfs.traceAttrs(attribute.String("name", name)),
fs.traceAttrs(attribute.String("name", name)),
)
defer span.End()
fsPath, nestedFs, nestedFsPath, err := tfs.resolver.resolvePath(ctx, name, tfs.rawOpen)
fsPath, nestedFs, nestedFsPath, err := fs.resolver.resolvePath(ctx, name, fs.rawOpen)
if err != nil {
return nil, err
}
if nestedFs != nil {
lastReadTimeout := tfs.lastAccessTimeout.Load()
if lastReadTimeout != nil && time.Since(*lastReadTimeout) < secondaryTimeout { // make short timeout for already faliled files
span.SetAttributes(attribute.Bool("short_timeout", true))
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Millisecond)
defer cancel()
}
defer func() {
if err == context.DeadlineExceeded {
now := time.Now()
tfs.lastAccessTimeout.Store(&now)
}
}()
return nestedFs.ReadDir(ctx, nestedFsPath)
}
files, err := tfs.files(ctx)
files, err := fs.files(ctx)
if err != nil {
return nil, err
}
@ -392,16 +347,16 @@ var _ File = (*torrentFile)(nil)
type torrentFile struct {
name string
mu sync.RWMutex
mu sync.Mutex
tr torrent.Reader
lastReadTimeout atomic.Pointer[time.Time]
lastReadTimeout time.Time
file *torrent.File
}
const secondaryTimeout = time.Hour * 24
const secondaryTimeout = time.Hour
func openTorrentFile(ctx context.Context, name string, file *torrent.File) (*torrentFile, error) {
// select {
@ -461,11 +416,10 @@ func (tf *torrentFile) Read(ctx context.Context, p []byte) (n int, err error) {
span.End()
}()
tf.mu.RLock()
defer tf.mu.RUnlock()
tf.mu.Lock()
defer tf.mu.Unlock()
lastReadTimeout := tf.lastReadTimeout.Load()
if lastReadTimeout != nil && time.Since(*lastReadTimeout) < secondaryTimeout { // make short timeout for already faliled files
if time.Since(tf.lastReadTimeout) < secondaryTimeout { // make short timeout for already faliled files
span.SetAttributes(attribute.Bool("short_timeout", true))
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Millisecond)
@ -473,8 +427,7 @@ func (tf *torrentFile) Read(ctx context.Context, p []byte) (n int, err error) {
}
defer func() {
if err == context.DeadlineExceeded {
now := time.Now()
tf.lastReadTimeout.Store(&now)
tf.lastReadTimeout = time.Now()
}
}()
@ -490,11 +443,10 @@ func (tf *torrentFile) ReadAt(ctx context.Context, p []byte, off int64) (n int,
span.End()
}()
tf.mu.RLock()
defer tf.mu.RUnlock()
tf.mu.Lock()
defer tf.mu.Unlock()
lastReadTimeout := tf.lastReadTimeout.Load()
if lastReadTimeout != nil && time.Since(*lastReadTimeout) < secondaryTimeout { /// make short timeout for already faliled files
if time.Since(tf.lastReadTimeout) < secondaryTimeout { // make short timeout for already faliled files
span.SetAttributes(attribute.Bool("short_timeout", true))
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Millisecond)
@ -502,8 +454,7 @@ func (tf *torrentFile) ReadAt(ctx context.Context, p []byte, off int64) (n int,
}
defer func() {
if err == context.DeadlineExceeded {
now := time.Now()
tf.lastReadTimeout.Store(&now)
tf.lastReadTimeout = time.Now()
}
}()