frodo

package module
v0.0.0-...-55a39bb Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: MIT Imports: 7 Imported by: 0

README

frodo

A quick POC to play with io_uring APIs using Go.

Overview

Frodo deliberately keeps the API dead simple. Because he does not want the ring to fall into the wrong hands. It just exposes 2 very simple ReadFile and WriteFile functions which are akin to the ioutil family of functions. These calls just push an entry to the submission queue. To allow the user to control when to submit the queue, a Poll function is provided.

The Poll function will submit the queue and wait for all the entries to appear in the completion queue. Dive into the code to know more :)

For an overall explanation of the subject matter, please read: https://developers.mattermost.com/blog/hands-on-iouring-go.

For a more detailed background, please read: https://kernel.dk/io_uring.pdf.

Pre-requisites
  • Install liburing in your machine from latest master. (https://github.com/axboe/liburing/)
  • You need to have a modern (>=5.3) Linux kernel. It may work on older kernels too. But I have not tested it.
Getting started

See the docs page to get started.

Documentation

Overview

Package frodo implements a high-level Go wrapper to perform file read/write operations using liburing.

It exposes 2 very simple ReadFile and WriteFile functions which are akin to the ioutil family of functions. These calls just push an entry to the submission queue. To allow the user to control when to submit the queue, a Poll function is provided. The Poll function will submit the queue and wait for all the entries to appear in the completion queue.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/agnivade/frodo"
)

func main() {
	err := frodo.Init()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer frodo.Cleanup()

	go func() {
		for err := range frodo.Err() {
			fmt.Println(err)
		}
	}()

	var wg sync.WaitGroup
	// Read a file.
	err = frodo.ReadFile("testdata/ssa.html", func(buf []byte) {
		defer wg.Done()
		// handle buf
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	// Write something
	err = frodo.WriteFile("testdata/dummy.txt", []byte("hello world"), 0644, func(n int) {
		defer wg.Done()
		// handle n
	})

	// Call Poll to let the kernel know to read the entries.
	frodo.Poll()
	// Wait till all callbacks are done.
	wg.Wait()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup()

Cleanup must be called to close the ring.

func Err

func Err() <-chan error

Err is a channel that needs to be read to receive internal errors that can happen during interaction with the ring or during callbacks. It must be read from after calling Init, otherwise the processing might get stuck.

func Init

func Init() error

Init is used to initialize the ring and setup some global state.

func Poll

func Poll()

Poll signals the kernel to read all pending entries from the submission queue and waits until all entries have been read from the completion queue.

func ReadFile

func ReadFile(path string, cb func(buf []byte)) error

ReadFile reads a file from the given path and returns the result as a byte slice in the passed callback function.

func WriteFile

func WriteFile(path string, data []byte, perm os.FileMode, cb func(written int)) error

WriteFile writes data to a file at the given path. After the file is written, it then calls the callback with the number of bytes written.

Types

This section is empty.

Jump to

Keyboard shortcuts

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