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