dbuf

package module
v0.0.0-...-406bb75 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2016 License: Apache-2.0 Imports: 6 Imported by: 2

README

go double buffering

A simple library to manage resources which can be upgraded gracefully. This blog written in Chinese shows the usage of this library : http://blog.codeg.cn/2016/01/27/double-buffering/

During the continuous running period of a network server, there are some resources need to be updated on real-time. How to do an elegant resource upgrading? Here we propose a method to solve this problem which is called double-buffering technology. Double-buffering is a term that is well known throughout the graphics industry.

When double buffering is enabled, all resource upgrading operations are first initialized to a buffer handler instead of the handler which is being used by the process. After all initializing operations are completed, the buffer handler is swapped directly to the old one associated with it.

Example

There is a simple example located in the example directory. The example provides a simple querying service which require two parameters: id and query. id is used as an identity of the client. query is used as the entity the querying. The service logic uses the query as a key to query a backend database and return the result. Avoiding vicious client to do request we provide an black-id-list which holds all the black ids.

Using the following steps to run this example:

Step 1
$ git clone https://github.com/zieckey/dbuf
$ cd dbuf/example
$ go build
$ ./example ./black_id.txt
Step 2

And in another console, we can do some requests:

$ curl "http://localhost:8091/q?id=123&query=jane"
hello, 123
$ curl "http://localhost:8091/q?id=475e5a499587a52ea14a23031ecce7c9&query=jane"
ERROR

id=123 is OK. But id=475e5a499587a52ea14a23031ecce7c9 is rejected.

Step 3

Then we added 123 to the black-id-list. Now we have updated the black-id-list file and need it to be loaded into the service process. The simplest way is to stop the process and then start the process again. But during the continuous running period of a network server, this method will lose some requests during the stop-and-start period. It is very harmful to the client users.

So we use the double-buffering technology provided by this library. What we need to do is only to send a administration request to have it enabled. Like this:

$ curl "http://localhost:8091/admin/reload?name=black_id&path=./black_id.txt"
OK
$ curl "http://localhost:8091/q?id=123&query=jane"
ERROR

id=123 is rejected now. It works. Simple reload the dict file successfully.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DoubleBuffering

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

func (*DoubleBuffering) Get

func (d *DoubleBuffering) Get() TargetRef

Get returns the target this DoubleBuffering manipulated. You should call TargetRef.Release() function after you have used it.

func (*DoubleBuffering) LatestConfMD5

func (d *DoubleBuffering) LatestConfMD5() string

LatestConfMD5 returns the latest config's md5

func (*DoubleBuffering) ReloadTimestamp

func (d *DoubleBuffering) ReloadTimestamp() int64

ReloadTimestamp returns the latest timestamp when the DoubleBuffering reloaded at the last time

type Manager

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

func NewManager

func NewManager() *Manager

func (*Manager) Add

func (m *Manager) Add(name string, conf string, f TargetCreator) (err error)

func (*Manager) Get

func (m *Manager) Get(name string) *DoubleBuffering

func (*Manager) Reload

func (m *Manager) Reload(name, conf string) error

type Target

type Target interface {
	Initialize(conf string) bool
	Close()
}

DoubleBufferingTarget is the interface that wraps the basic operations on a resource.

Initialize does some initialization operations on the resource. When Initialize encounters an error implementations must return false.

Close does some resource recycling works which cannot be done by GC of Golang.

type TargetCreator

type TargetCreator func() Target

type TargetRef

type TargetRef struct {
	Target Target
	// contains filtered or unexported fields
}

func (TargetRef) GetRef

func (d TargetRef) GetRef() int32

GetRef returns the reference count of the resource.

func (TargetRef) Release

func (d TargetRef) Release()

Release decrease one reference count.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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