goaio

package module
v0.0.0-...-14623b8 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 6 Imported by: 4

README

GoDoc

goaio

Kernel based asynchronous IO library in pure golang

Documentation

Overview

Example
bb := make([]byte, testBuffSize)
a, err := NewAIO(testFile, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
	fmt.Println("Failed to create AIO", err)
}
for i := range bb {
	bb[i] = 0xab
}

var requests []RequestId
//kick off 16 requests to write to the file
//this should generate a file of testBuffSize*16
for i := 0; i < 16; i++ {
	r, err := a.Write(bb)
	if err != nil {
		fmt.Println("Failure to issue write", err)
		return
	}
	requests = append(requests, r)
}

//wait for the first 8 requests to finish in order
for i := 0; i < 8; i++ {
	if _, err := a.WaitFor(requests[i]); err != nil {
		fmt.Println("Failed waiting for write request", err)
		return
	}
}

//wait for the next 8 requests to finish in any order
//this is a batch wait, several can come back
completed := make([]RequestId, 8)
toComplete := 8
for toComplete > 0 {
	n, err := a.WaitAny(completed)
	if err != nil {
		fmt.Println("Failed to wait for any write requests", err)
		return
	}
	toComplete -= n
}

//flush the file handle the AIO for good measure
if err := a.Flush(); err != nil {
	fmt.Println("Failed to sync AIO")
}

if err := a.Close(); err != nil {
	fmt.Println("Failed to close AIO")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInitFail          = errors.New("Failed to initailize AIO context")
	ErrNotInit           = errors.New("Not initialized")
	ErrDestroy           = errors.New("Failed to tear down context")
	ErrIoSubFail         = errors.New("Failed to submit new IO request")
	ErrInvalidBuffer     = errors.New("Invalid buffer")
	ErrWaitAllFailed     = errors.New("Failed to wait for all requests to complete")
	ErrNilEvent          = errors.New("The kernel returned a nil event result.  Fatal error")
	ErrNilCallback       = errors.New("The kernel returned a nil callback structure.  Fatal error")
	ErrUntrackedEventKey = errors.New("The kernel returned an event key we weren't tracking")
	ErrInvalidEventPtr   = errors.New("The kernel returned an invalid callback event pointer")
	ErrCompletion        = errors.New("The kernel failed to process all of the request")
	ErrWhatTheHell       = errors.New("A callback event occurred but no buffer was put into the pool")
	ErrNotFound          = errors.New("ID not found")
	ErrNotDone           = errors.New("Request not finished")
	ErrInvalidQueueDepth = errors.New("Invalid queue depth")
)

Functions

This section is empty.

Types

type AIO

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

func Create

func Create(name string) (*AIO, error)

Create is shorthand for NewAIO(name, O_RDWR|O_CREATE|O_TRUNC, 0660)

func New

func New(fio *os.File, cfg AIOExtConfig) (*AIO, error)

New creates a file aio with the given file handle

func NewAIO

func NewAIO(name string, flag int, perm os.FileMode) (*AIO, error)

NewAIO opens a file with the appropriate flags and permissions and positions the file index at the end of the file

func NewAIOExt

func NewAIOExt(name string, cfg AIOExtConfig, flag int, perm os.FileMode) (*AIO, error)

NewAIOExt opens a file with the appropriate flags and permissions and positions the file index at the end of the file with additional configuration options

func Open

func Open(name string) (*AIO, error)

Open is shorthand for NewAIO(name, O_RDONLY, 0)

func (*AIO) Close

func (a *AIO) Close() error

Close up the aio object, waiting for all requests to finish first

func (*AIO) FD

func (a *AIO) FD() *os.File

FD hands back the underlying *os.File pointer This is NOT A COPY, so do not do close it or do anything crazy with it. This is purely a convienence method, use at your own peril

func (*AIO) Flush

func (a *AIO) Flush() error

Flush will wait for all submitted jobs to finish and then flush the file descriptor. Because the Linux kernel does not actually support Flush via the AIO interface we just issue a plain old flush via userland. No async here. Flush DOES NOT ack outstanding requests

func (*AIO) ReadAt

func (a *AIO) ReadAt(b []byte, offset int64) (RequestId, error)

ReadAt reads data from the file at a specific offset

func (*AIO) Ready

func (a *AIO) Ready() bool

Ready returns whether or not there is a callback buffer ready to go basically a check on whether or not we will block on a read/write attempt

func (*AIO) Truncate

func (a *AIO) Truncate(sz int64) error

Truncate will wait for all submitted jobs to finish and then trunctate the file to the designated size.

func (*AIO) Wait

func (a *AIO) Wait() error

Wait will block until there is an available request slot open

func (*AIO) WaitAny

func (a *AIO) WaitAny(completed []RequestId) (int, error)

WaitAny will block until a request finishes and will populate a

buffer of request IDs with the items that finish, returning the completion
count and a potential error.  If there are no outstanding requests it will
return 0, nil

func (*AIO) WaitFor

func (a *AIO) WaitFor(id RequestId) (int, error)

WaitFor will block until the given RequestId is done

func (*AIO) Write

func (a *AIO) Write(b []byte) (RequestId, error)

Write will submit the bytes for writting at the end of the file, the buffer CANNOT change before the write completes, this is ASYNC!

func (*AIO) WriteAt

func (a *AIO) WriteAt(b []byte, offset int64) (RequestId, error)

WriteAt will write at a specific file offset

type AIOExtConfig

type AIOExtConfig struct {
	QueueDepth int
}

type RequestId

type RequestId uint

Jump to

Keyboard shortcuts

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