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 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.
Click to show internal directories.
Click to hide internal directories.