autofile

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

README

go-autofile

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAutoFileClosed = errors.New("autofile is closed")

ErrAutoFileClosed is reported when operations attempt to use an autofile after it has been closed.

Functions

func GroupCheckDuration

func GroupCheckDuration(duration time.Duration) func(*Group)

GroupCheckDuration allows you to overwrite default groupCheckDuration.

func GroupHeadSizeLimit

func GroupHeadSizeLimit(limit int64) func(*Group)

GroupHeadSizeLimit allows you to overwrite default head size limit - 10MB.

func GroupTotalSizeLimit

func GroupTotalSizeLimit(limit int64) func(*Group)

GroupTotalSizeLimit allows you to overwrite default total size limit of the group - 1GB.

Types

type AutoFile

type AutoFile struct {
	ID   string
	Path string
	// contains filtered or unexported fields
}

AutoFile automatically closes and re-opens file for writing. The file is automatically setup to close itself every 1s and upon receiving SIGHUP.

This is useful for using a log file with the logrotate tool.

func OpenAutoFile

func OpenAutoFile(ctx context.Context, path string) (*AutoFile, error)

OpenAutoFile creates an AutoFile in the path (with random ID). If there is an error, it will be of type *PathError or *ErrPermissionsChanged (if file's permissions got changed (should be 0600)).

func (*AutoFile) Close

func (af *AutoFile) Close() error

Close shuts down the service goroutine and marks af as invalid. Operations on af after Close will report an error.

func (*AutoFile) Size

func (af *AutoFile) Size() (int64, error)

Size returns the size of the AutoFile. It returns -1 and an error if fails get stats or open file. Opens AutoFile if needed.

func (*AutoFile) Sync

func (af *AutoFile) Sync() error

Sync commits the current contents of the file to stable storage. Typically, this means flushing the file system's in-memory copy of recently written data to disk.

func (*AutoFile) Write

func (af *AutoFile) Write(b []byte) (n int, err error)

Write writes len(b) bytes to the AutoFile. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b). Opens AutoFile if needed.

type Group

type Group struct {
	service.BaseService

	ID   string
	Head *AutoFile // The head AutoFile to write to

	Dir string // Directory that contains .Head
	// contains filtered or unexported fields
}

You can open a Group to keep restrictions on an AutoFile, like the maximum size of each chunk, and/or the total amount of bytes stored in the group.

The first file to be written in the Group.Dir is the head file.

Dir/
- <HeadPath>

Once the Head file reaches the size limit, it will be rotated.

Dir/
- <HeadPath>.000   // First rolled file
- <HeadPath>       // New head path, starts empty.
									 // The implicit index is 001.

As more files are written, the index numbers grow...

Dir/
- <HeadPath>.000   // First rolled file
- <HeadPath>.001   // Second rolled file
- ...
- <HeadPath>       // New head path

The Group can also be used to binary-search for some line, assuming that marker lines are written occasionally.

func OpenGroup

func OpenGroup(ctx context.Context, logger log.Logger, headPath string, groupOptions ...func(*Group)) (*Group, error)

OpenGroup creates a new Group with head at headPath. It returns an error if it fails to open head file.

func (*Group) Buffered

func (g *Group) Buffered() int

Buffered returns the size of the currently buffered data.

func (*Group) Close

func (g *Group) Close()

Close closes the head file. The group must be stopped by this moment.

func (*Group) FlushAndSync

func (g *Group) FlushAndSync() error

FlushAndSync writes any buffered data to the underlying file and commits the current content of the file to stable storage (fsync).

func (*Group) HeadSizeLimit

func (g *Group) HeadSizeLimit() int64

HeadSizeLimit returns the current head size limit.

func (*Group) MaxIndex

func (g *Group) MaxIndex() int

MaxIndex returns index of the last file in the group.

func (*Group) MinIndex

func (g *Group) MinIndex() int

MinIndex returns index of the first file in the group.

func (*Group) NewReader

func (g *Group) NewReader(index int) (*GroupReader, error)

NewReader returns a new group reader. CONTRACT: Caller must close the returned GroupReader.

func (*Group) OnStart

func (g *Group) OnStart(ctx context.Context) error

OnStart implements service.Service by starting the goroutine that checks file and group limits.

func (*Group) OnStop

func (g *Group) OnStop()

OnStop implements service.Service by stopping the goroutine described above. NOTE: g.Head must be closed separately using Close.

func (*Group) ReadGroupInfo

func (g *Group) ReadGroupInfo() GroupInfo

Returns info after scanning all files in g.Head's dir.

func (*Group) TotalSizeLimit

func (g *Group) TotalSizeLimit() int64

TotalSizeLimit returns total size limit of the group.

func (*Group) Wait added in v1.6.0

func (g *Group) Wait()

Wait blocks until the Group service has stopped and its background goroutines have fully exited. It overrides BaseService.Wait — which returns as soon as OnStop() closes the quit channel, before the procCancel-driven processTicks goroutine is joined — so that every direct Group consumer gets the same shutdown guarantee BaseWAL.Wait relies on, not just callers that remember to chain WaitForGoroutines explicitly.

It calls g.BaseService.Wait() (the embedded method), not g.Wait(), so there is no recursion. WaitForGoroutines is idempotent, so chaining it here on top of an explicit caller call is harmless.

func (*Group) WaitForGoroutines added in v1.6.0

func (g *Group) WaitForGoroutines()

WaitForGoroutines blocks until the background goroutines started by this Group (e.g. processTicks) have fully exited. It is safe to call after Stop() because OnStop() cancels the goroutine's context via procCancel.

func (*Group) Write

func (g *Group) Write(p []byte) (nn int, err error)

Write writes the contents of p into the current head of the group. It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the write is short. NOTE: Writes are buffered so they don't write synchronously TODO: Make it halt if space is unavailable

func (*Group) WriteLine

func (g *Group) WriteLine(line string) error

WriteLine writes line into the current head of the group. It also appends "\n". NOTE: Writes are buffered so they don't write synchronously TODO: Make it halt if space is unavailable

type GroupInfo

type GroupInfo struct {
	MinIndex  int   // index of the first file in the group, including head
	MaxIndex  int   // index of the last file in the group, including head
	TotalSize int64 // total size of the group
	HeadSize  int64 // size of the head
}

GroupInfo holds information about the group.

type GroupReader

type GroupReader struct {
	*Group
	// contains filtered or unexported fields
}

GroupReader provides an interface for reading from a Group.

func (*GroupReader) Close

func (gr *GroupReader) Close() error

Close closes the GroupReader by closing the cursor file.

func (*GroupReader) CurIndex

func (gr *GroupReader) CurIndex() int

CurIndex returns cursor's file index.

func (*GroupReader) Read

func (gr *GroupReader) Read(p []byte) (n int, err error)

Read implements io.Reader, reading bytes from the current Reader incrementing index until enough bytes are read.

func (*GroupReader) SetIndex

func (gr *GroupReader) SetIndex(index int) error

SetIndex sets the cursor's file index to index by opening a file at this position.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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