Documentation ¶
Overview ¶
Package iox contains I/O utilities.
The primary concern of the package is managing and minimizing the use of file descriptors, an operating system resource which is often in short supply in high-concurrency servers.
The two objects that help in this are the Filer and BufferFile.
A filer manages a allotment of file descriptors, blocking on file creation until an old file closes and frees up a descriptor allotment.
A BufferFile keeps a fraction of its contents in memory. If the number of bytes stored in a BufferFile is small, no file descriptor is ever used.
Index ¶
- type BufferFile
- func (bf *BufferFile) Close() (err error)
- func (bf *BufferFile) Read(p []byte) (n int, err error)
- func (bf *BufferFile) ReadAt(p []byte, off int64) (n int, err error)
- func (bf *BufferFile) Seek(offset int64, whence int) (int64, error)
- func (bf *BufferFile) Size() int64
- func (bf *BufferFile) Truncate(size int64) error
- func (bf *BufferFile) Write(p []byte) (n int, err error)
- type File
- type Filer
- func (f *Filer) BufferFile(memSize int) *BufferFile
- func (f *Filer) Open(name string) (*File, error)
- func (f *Filer) OpenFile(name string, flag int, perm os.FileMode) (*File, error)
- func (f *Filer) SetTempdir(tempdir string)
- func (f *Filer) Shutdown(ctx context.Context) error
- func (f *Filer) TempFile(dir, prefix, suffix string) (file *File, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufferFile ¶
type BufferFile struct { io.Reader io.ReaderAt io.Writer io.Seeker io.Closer // contains filtered or unexported fields }
BufferFile is a temporary file that stores its first N bytes in memory.
A BufferFile will not create an underlying temporary file until a Write or a Seek pushes it beyond its memory limit. This allows for typical cases where the contents fit in memory to avoid the disk entirely and not hold a file descriptor.
func (*BufferFile) Close ¶
func (bf *BufferFile) Close() (err error)
Close closes the BufferFile, deleting any underlying temporary file.
func (*BufferFile) Size ¶
func (bf *BufferFile) Size() int64
Size returns the current length of the buffer file. It is equivalent to the position returned by bf.Seek(0, os.SEEK_END).
func (*BufferFile) Truncate ¶
func (bf *BufferFile) Truncate(size int64) error
Truncate changes the file size. It does not move the offset, use Seek for that.
type Filer ¶
type Filer struct { DefaultBufferMemSize int // default value: 64kb Logf func(format string, v ...interface{}) // used to report open files at Shutdown // contains filtered or unexported fields }
A Filer creates files, managing load on file descriptors.
Exported fields can only be modified after NewFiler is called and before any methods are called.
func NewFiler ¶
NewFiler creates a Filer which will open at most fdLimit files simultaneously. If fdLimit is 0, a Filer is limited to 90% of the process's allowed files.
func (*Filer) BufferFile ¶
func (f *Filer) BufferFile(memSize int) *BufferFile
BufferFile creates a buffered file with up to memSize bytes stored in memory.
If memSize is zero, the Filer's default value is used.
The underlying file descriptor should not be handled directly as the fraction of the contents stored in the OS file may change.
func (*Filer) Open ¶
Open opens the named file for reading.
It is similar to os.Open except it will block if Filer has exhasted its file descriptors until one is available.
func (*Filer) OpenFile ¶
OpenFile is a generalized file open method.
It is similar to os.OpenFile except it will block if Filer has exhasted its file descriptors until one is available.
func (*Filer) SetTempdir ¶
SetTempdir sets the default directory used to hold temporary files.