Package dsync

import "github.com/BurntSushi/dsync"
Overview
Index

Overview ▾

func GenerateRandomKey

func GenerateRandomKey(size int) []byte

Generates a cryptographically strong key with `size` bytes. If there is an error generating the key, the program panics.

type DataSource

type DataSource struct {
    // The name of a DataSource, which must be unique for any particular node.
    // e.g., the full path name of a directory/file.
    Name string
    // contains filtered or unexported fields
}

DataSource corresponds to a single collection of data. A DataSource must have a name that is unique in its corresponding node.

In any particular node, real data must belong to at most a single DataSource. e.g., if a directory "/home/andrew" is a data source, then no other data source may correspond to "/home/andrew" or any data contained within.

If the same data is used in two different nodes, the behavior is undefined. (The result will likely be disastrous. Don't do it.)

func (*DataSource) AddKey

func (ds *DataSource) AddKey(key EncKey)

func (*DataSource) SetDatum

func (ds *DataSource) SetDatum(name string, datum Datum)

type Datum

type Datum interface {
    // Size returns the total number of bytes in this datum.
    // If a datum has been removed, this method must return -1.
    Size() int64

    // LastUpdated returns the time that the data was last modified.
    // dsync always uses the most up to date version of a datum.
    LastUpdated() time.Time

    // PieceRead executes the `run` function on the piece numbered `pieceNum`.
    // The reader provided to `run` should only provide bytes of that piece
    // and return EOF when the bytes of that piece have been exhausted.
    PieceRead(pieceNum int, run func(io.Reader))

    // PieceRead executes the `run` function on the piece numbered `pieceNum`.
    // The writer provided to `run` should start writing bytes at the beginning
    // of the specified piece.
    PieceWrite(pieceNum int, run func(io.Writer))

    // PieceSum returns the checksum of the given piece. The checksum may be
    // cached.
    PieceSum(pieceNum int) string

    // RecomputeSums instructs the datum to compute the checksum of each piece
    // from the real data. Any cache that exists should be invalidated.
    // RecomputeSums should block until the checksums for every piece have
    // been computed.
    RecomputeSums()
}

Datum represents a discrete unit of synchronization. Every unit of synchronization must be divisible into pieces where each piece contains exactly `PieceSize` bytes (the last piece may contain fewer bytes).

The onus is on the client to maintain these pieces, although this package provides helper functions for common cases.

Note that all of these functions must be callable from multiple goroutines. It is sufficient to protect each invocation with a simple mutex.

type EncKey

type EncKey struct {
    // The actual AES 256 bit encrpytion key. It must be 32 bytes in length.
    Key string

    // The access privileges of this key. Currently, only read/write and
    // read-only modes are supported.
    Mode KeyMode
}

EncKey represents an encrpytion key that grants access to a data source.

type KeyMode

type KeyMode int
const (
    ReadWrite KeyMode = iota
    ReadOnly
)

type Node

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

Node represents a local grouping of data sources. There is no restriction on the number of nodes for a single host, although typically only one is used.

func NewNode

func NewNode(globalIP string, maxConns, port int) (*Node, error)

func (*Node) AddDataSource

func (nd *Node) AddDataSource(name string) *DataSource

func (*Node) AddPeer

func (nd *Node) AddPeer(host string, port int) error

AddPeer gives knowledge to the given node about a peer. If there is a problem resolving the address or connecting to the peer, an error will be returned.

Upon connecting to a peer, additional peers may be added to your node depending on if they are reachable to you.

func (*Node) Send

func (nd *Node) Send(msg string)