dbuffer

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: MIT Imports: 3 Imported by: 0

README

DBuffer

Package dbuffer is a Go 1.18+ package using double-buffer to achieve data hot update.

Usage:

package main

import (
    "fmt"
    "log"

    "github.com/ETZhangSX/dbuffer"
)

// implement loader for updating data.
type loader struct {
}

func(l *loader) Load(dest *map[string]string) {
    // update data
    *dest = make(map[string]string)
    *dest["key"] = "value"
}

func main() {
    // alloc func to create obj
    var alloc dbuffer.Alloc[map[string]string] = func() map[string]string {
        obj := map[string]string{}
        return obj
    }
    // update interval
    interval := 30 * time.Second
    buf := dbuffer.New[map[string]string](&loader{}, alloc, dbuffer.WithInterval(interval))

    // get data
    m1 := buf.Data()
    fmt.Println(m1["key"])

    // get data with done, which will block updates to current buffer util all refs done.
    m2, done := buf.DataWithDone()
    defer done()
    fmt.Println(m2["key"])
}

Documentation

Overview

Package dbuffer is a Go 1.18+ package using double-buffer to achieve data hot update.

Usage:

package main

import (
    "fmt"
    "log"

    "github.com/ETZhangSX/dbuffer"
)

// implement loader for updating data.
type loader struct {
}

func(l *loader) Load(dest *map[string]string) {
    // update data
    *dest = make(map[string]string)
    *dest["key"] = "value"
}

func main() {
    // alloc func to create obj
    var alloc dbuffer.Alloc[map[string]string] = func() map[string]string {
        obj := map[string]string{}
        return obj
    }
    // update interval
    interval := 30 * time.Second
    buf := dbuffer.New[map[string]string](&loader{}, alloc, dbuffer.WithInterval(interval))

    // get data
    m1 := buf.Data()
    fmt.Println(m1["key"])

    // get data with done, which will block updates to current buffer util all refs done.
    m2, done := buf.DataWithDone()
    defer done()
    fmt.Println(m2["key"])
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alloc

type Alloc[T any] func() T

Alloc is the func type for allocate the object.

type DBuffer

type DBuffer[T any] interface {
	// Data returns the stored data.
	Data() T
	// DataWithDone returns the stored data and a DoneFunc while increasing
	// the ref counter by one. The ref counter decrease when DoneFunc is called.
	DataWithDone() (data T, done DoneFunc)
	// Load is used to load new data manually. It will be blocked if the writing
	// buffer ref counter is larger than 0.
	Load()
}

DBuffer is the interface for double-buffer that support data hot update.

func New

func New[T any](loader Loader[T], alloc Alloc[T], opts ...Option) DBuffer[T]

New creates a DBuffer instance.

type DoneFunc

type DoneFunc func()

DoneFunc decrements the ref counter of data by one.

type Loader

type Loader[T any] interface {
	// Load data to dest pointer. ok is true if data updated.
	Load(dest *T) (ok bool, err error)
}

Loader wraps the interface

Jump to

Keyboard shortcuts

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