logsync

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

Package logsync synchronizes logs between logbooks across networks

Example
// first some boilerplate setup
ctx, done := context.WithCancel(context.Background())
defer done()

// our example has two authors. Johnathon and Basit are going to sync logbooks
// let's start with two empty logbooks
johnathonsLogbook := makeJohnathonLogbook()
basitsLogbook := makeBasitLogbook()

wait := make(chan struct{}, 1)

// create a logsync from basit's logbook:
basitLogsync := New(basitsLogbook, func(o *Options) {
	// we MUST override the PreCheck function. In this example we're only going
	// to allow pushes from johnathon
	o.PushPreCheck = func(ctx context.Context, author profile.Author, ref dsref.Ref, l *oplog.Log) error {
		if author.AuthorID() != johnathonsLogbook.Author().AuthorID() {
			return fmt.Errorf("rejected for secret reasons")
		}
		return nil
	}

	o.Pushed = func(ctx context.Context, author profile.Author, ref dsref.Ref, l *oplog.Log) error {
		wait <- struct{}{}
		return nil
	}
})

// for this example we're going to do sync over HTTP.
// create an HTTP handler for the remote & wire it up to an example server
handleFunc := HTTPHandler(basitLogsync)
server := httptest.NewServer(handleFunc)
defer server.Close()

// johnathon creates a dataset with a bunch of history:
worldBankDatasetRef := makeWorldBankLogs(ctx, johnathonsLogbook)

items, err := johnathonsLogbook.Items(ctx, worldBankDatasetRef, 0, 100)
if err != nil {
	panic(err)
}
fmt.Printf("johnathon has %d references for %s\n", len(items), worldBankDatasetRef.Human())

// johnathon creates a new push
johnathonLogsync := New(johnathonsLogbook)
push, err := johnathonLogsync.NewPush(worldBankDatasetRef, server.URL)
if err != nil {
	panic(err)
}

// execute the push, sending jonathon's world bank reference to basit
if err = push.Do(ctx); err != nil {
	panic(err)
}

// wait for sync to complete
<-wait
if items, err = basitsLogbook.Items(ctx, worldBankDatasetRef, 0, 100); err != nil {
	panic(err)
}
fmt.Printf("basit has %d references for %s\n", len(items), worldBankDatasetRef.Human())

// this time basit creates a history
nasdaqDatasetRef := makeNasdaqLogs(ctx, basitsLogbook)

if items, err = basitsLogbook.Items(ctx, nasdaqDatasetRef, 0, 100); err != nil {
	panic(err)
}
fmt.Printf("basit has %d references for %s\n", len(items), nasdaqDatasetRef.Human())

// prepare to pull nasdaq refs from basit
pull, err := johnathonLogsync.NewPull(nasdaqDatasetRef, server.URL)
if err != nil {
	panic(err)
}
// setting merge=true will persist logs to the logbook if the pull succeeds
pull.Merge = true

if _, err = pull.Do(ctx); err != nil {
	panic(err)
}

if items, err = johnathonsLogbook.Items(ctx, nasdaqDatasetRef, 0, 100); err != nil {
	panic(err)
}
fmt.Printf("johnathon has %d references for %s\n", len(items), nasdaqDatasetRef.Human())
Output:

johnathon has 3 references for johnathon/world_bank_population
basit has 3 references for johnathon/world_bank_population
basit has 2 references for basit/nasdaq
johnathon has 2 references for basit/nasdaq

Index

Examples

Constants

View Source
const (
	// LogsyncProtocolID is the dsyc p2p Protocol Identifier
	LogsyncProtocolID = protocol.ID("/qri/logsync")
	// LogsyncServiceTag tags the type & version of the dsync service
	LogsyncServiceTag = "qri/logsync/0.1.1-dev"
)

Variables

View Source
var (
	// ErrNoLogsync indicates no logsync pointer has been allocated where one is expected
	ErrNoLogsync = fmt.Errorf("logsync: does not exist")
)

Functions

func HTTPHandler

func HTTPHandler(lsync *Logsync) http.HandlerFunc

HTTPHandler exposes a Dsync remote over HTTP by exposing a HTTP handler that interlocks with methods exposed by httpClient

Types

type Hook

type Hook func(ctx context.Context, author profile.Author, ref dsref.Ref, l *oplog.Log) error

Hook is a function called at specified points in the sync lifecycle

type Logsync

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

Logsync fulfills requests from clients, logsync wraps a logbook.Book, pushing and pulling logs from remote sources to its logbook

func New

func New(book *logbook.Book, opts ...func(*Options)) *Logsync

New creates a remote from a logbook and optional configuration functions

func (*Logsync) Author

func (lsync *Logsync) Author() profile.Author

Author is the local author of lsync's logbook

func (*Logsync) DoRemove

func (lsync *Logsync) DoRemove(ctx context.Context, ref dsref.Ref, remoteAddr string) error

DoRemove asks a remote to remove a log

func (*Logsync) NewPull

func (lsync *Logsync) NewPull(ref dsref.Ref, remoteAddr string) (*Pull, error)

NewPull creates a Pull from the local logsync to a remote destination doing a pull fetches a log from the remote to the local logbook

func (*Logsync) NewPush

func (lsync *Logsync) NewPush(ref dsref.Ref, remoteAddr string) (*Push, error)

NewPush prepares a Push from the local logsync to a remote destination doing a push places a local log on the remote

type Options

type Options struct {
	// to send & push over libp2p connections, provide a libp2p host
	Libp2pHost host.Host

	// called before accepting a log, returning an error cancel receiving
	PushPreCheck Hook
	// called after log data has been received, before it's stored in the logbook
	PushFinalCheck Hook
	// called after a log has been merged into the logbook
	Pushed Hook
	// called before a pull is accepted
	PullPreCheck Hook
	// called after a log is pulled
	Pulled Hook
	// called before removing
	RemovePreCheck Hook
	// called after removing
	Removed Hook
}

Options encapsulates runtime configuration for a remote

type Pull

type Pull struct {

	// set to true to merge these logs into the local store on successful pull
	Merge bool
	// contains filtered or unexported fields
}

Pull is a request to fetch a log

func (*Pull) Do

func (p *Pull) Do(ctx context.Context) (*oplog.Log, error)

Do executes the pull

type Push

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

Push is a request to place a log on a remote

func (*Push) Do

func (p *Push) Do(ctx context.Context) error

Do executes a push

Jump to

Keyboard shortcuts

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