fd

package
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FdCache

type FdCache struct {
	// contains filtered or unexported fields
}

func GetFdCache

func GetFdCache() *FdCache

GetFdCache returns the singleton instance of FdCache.

func NewFdCache

func NewFdCache(capacity int) *FdCache

NewFdCache returns an initialised FdCache that will hold at most `capacity` file descriptors.

func (*FdCache) Acquire

func (fc *FdCache) Acquire(path string) (*FileEntry, error)

Acquire returns a cached *fileEntry for the given path, opening the file in read-only mode if necessary. The entry's reference count is incremented; callers MUST invoke Release when they are done so the underlying descriptor can be closed (and the cache pruned) when no longer needed.

Acquire is race-free against concurrent Release and Remove: if the cached entry has been claimed for closing (refs <= 0), Acquire transparently falls through and opens a fresh descriptor rather than returning a stale FileEntry whose file has been closed.

func (*FdCache) CleanUp

func (fc *FdCache) CleanUp(path string)

CleanUp removes the entry for `path` from the cache and closes the file. This is primarily used when the file itself has been deleted from disk.

func (*FdCache) GetFileLock

func (fc *FdCache) GetFileLock(path string) *sync.RWMutex

GetFileLock returns an RWMutex for the given path, creating it if it doesn't exist. An RWMutex allows multiple concurrent readers while still giving exclusive access to writers (Write/Delete), which is exactly the behaviour we need for raw files. Deprecated: Use GetFileLockManager().GetFileLock(path) instead.

func (*FdCache) Release

func (fc *FdCache) Release(path string, e *FileEntry)

Release decrements the reference count for the given entry. When this is the last holder, Release atomically claims the close via dropRef and invokes closeEntry to finalize cleanup.

func (*FdCache) Remove

func (fc *FdCache) Remove(path string)

Remove forcibly evicts the entry for `path` from the cache (if present). This is primarily used when the file itself has been deleted from disk or when the segment containing it has been compacted away.

Remove never closes a file descriptor that an active holder still owns. The close is always performed by the last Release (which transitions refs 1 → -1 and runs closeEntry). If Remove happens to observe that no holder exists (refs == 0, e.g. a defensive/manual state), it atomically claims the close via CAS(0, refsClosed) and closes the FD itself.

type FileEntry

type FileEntry struct {
	// contains filtered or unexported fields
}

func (*FileEntry) File

func (e *FileEntry) File() *os.File

File returns the underlying os.File descriptor.

func (*FileEntry) Lock

func (e *FileEntry) Lock()

Lock acquires an exclusive lock.

func (*FileEntry) RLock

func (e *FileEntry) RLock()

RLock acquires a shared read lock.

func (*FileEntry) RUnlock

func (e *FileEntry) RUnlock()

RUnlock releases the read lock.

func (*FileEntry) Unlock

func (e *FileEntry) Unlock()

Unlock releases the exclusive lock.

type FileLockManager

type FileLockManager struct {
	// contains filtered or unexported fields
}

FileLockManager manages file-level locks for concurrent access control. It provides RWMutex locks for files, allowing multiple concurrent readers while giving exclusive access to writers (Write/Delete operations).

Thread-safety: All methods are safe for concurrent use.

Usage pattern:

lock := manager.GetFileLock(path)
lock.Lock() // or lock.RLock() for read operations
defer lock.Unlock() // or lock.RUnlock()
// ... perform file operations ...

Note: RemoveFileLock should only be called after the file has been successfully deleted and the lock has been released.

func GetFileLockManager

func GetFileLockManager() *FileLockManager

GetFileLockManager returns the singleton instance of FileLockManager.

func (*FileLockManager) GetFileLock

func (flm *FileLockManager) GetFileLock(path string) *sync.RWMutex

GetFileLock returns an RWMutex for the given path, creating it if it doesn't exist. An RWMutex allows multiple concurrent readers while still giving exclusive access to writers (Write/Delete), which is exactly the behaviour we need for raw files.

func (*FileLockManager) HasLock

func (flm *FileLockManager) HasLock(path string) bool

HasLock returns true if a lock exists in the manager for the given path. Note: This only indicates whether a lock object exists in the manager, not whether the lock is currently held by any goroutine. This method is primarily intended for testing purposes.

func (*FileLockManager) RemoveFileLock

func (flm *FileLockManager) RemoveFileLock(path string)

RemoveFileLock removes the lock for the given path from the manager. This should be called only after a file has been successfully deleted and its lock has been released. Calling this while a lock is still held may lead to unexpected behavior.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL