globe

package module
v0.0.0-...-22d3cc9 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2014 License: MIT Imports: 6 Imported by: 0

README

go-globe

This was an afternoon hack as an excuse to play with Go. Caveat emptor!

go-globe is a distributed dictionary backed by a replicated log such as etcd.

Good for things like:

  • Managing dynamic configuration
  • Storing data available cluster-wide
  • Data or computational sharding
  • Service discovery
  • Other stuff!

Bad for things that require multiple nodes to write to the same key at a high rate, or really anything that needs super high throughput. This is mostly a convenience layer.

There is an abstraction interface for the log client so that any log can be used(ZooKeeper, consul, etc) but currently only etcd is implemented.

Currently it only supports string -> string key value pairs because of no generics in Go and I haven't explored other options for better value typing.

Interface

NewDict(keyspace string, logClient LogClient) *Dict

Creates a new globe.Dict and returns a pointer to it.

  • keyspace: The keyspace in which to store data in the underlying log.
  • logClient: The log client(see below)
Dict.Get(key string) (string, error)

Returns the local value at the given key.

Dict.Put(key string, value string) error

Updates the local value at the given key, updates the log, and returns any errors

NewEtcdClient(cluster []string) *EtcdClient

EtcdClient implements the LogClient interface.

  • cluster: A list of host:port strings that represent the etcd cluster

Example

client := NewEtcdClient([]string{"http://127.0.0.1:4001"})
myDict, err := NewDict("example", client)

myDict.Put("foo", "bar") // ignoring any error

val := myDict.Get("foo") // val == "bar"

Running tests

Note that the tests assume a running etcd process at localhost:4001(the default) and that they are non-deterministic because of race conditions and sleeps and I just haven't made them better yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dict

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

A Key-Value dictionary backed by an distributed log

func NewDict

func NewDict(keyspace string, logClient LogClient) (*Dict, error)

Constructs and returns a new dictionary. The returned dict will be populated with the latest snapshot of the log and automatically update the local copy of the data as it changes remotely.

func (*Dict) Close

func (d *Dict) Close()

Close the dictionary. It will no longer respond to log updates

func (*Dict) Get

func (d *Dict) Get(key string) (string, error)

Get value for given key. Returns error if key is not found.

func (*Dict) Put

func (d *Dict) Put(key string, value string) error

Put value at given key. Overwrites any existing value.

type EtcdClient

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

Implements LogClient

func NewEtcdClient

func NewEtcdClient(cluster []string) *EtcdClient

Constructor

func (*EtcdClient) Get

func (e *EtcdClient) Get(path string) (*LogNode, error)

LogClient Implementation

func (*EtcdClient) Put

func (e *EtcdClient) Put(path string, value string) error

func (*EtcdClient) PutDir

func (e *EtcdClient) PutDir(path string) error

func (*EtcdClient) Watch

func (e *EtcdClient) Watch(path string, watcher chan *LogNode, closer chan bool)

type LogClient

type LogClient interface {
	Get(path string) (*LogNode, error)
	Put(path string, value string) error
	PutDir(path string) error
	Watch(path string, watcher chan *LogNode, closer chan bool)
}

Generic client interface used as a wrapper for the underyling log client. See etcd_client.go for example implementatino

type LogNode

type LogNode struct {
	Key      string
	Value    string
	Children []LogNode
}

Jump to

Keyboard shortcuts

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