Skip to content

A Go consumer library for OpLog server

License

Notifications You must be signed in to change notification settings

dailymotion/oplogc

Repository files navigation

OpLog Consumer godoc license build

This repository contains a Go library to connect as a consumer to an OpLog server.

Here is an example of a Go consumer using the provided consumer library:

import (
    "fmt"

    "github.com/dailymotion/oplogc"
)

func main() {
    c := oplogc.Subscribe(myOplogURL, oplogc.Options{})

    ops, errs, done := c.Start()

    for {
        select {
        case op := <-ops:
            // Got the next operation
            switch op.Event {
            case "reset":
                // reset the data store
            case "live":
                // put the service back in production
            default:
                // Do something with the operation
                url := fmt.Sprintf("http://api.domain.com/%s/%s", op.Data.Type, op.Data.ID)
                data := MyAPIGetter(url)
                MyDataSyncer(data)
            }

            // Ack the fact you handled the operation
            op.Done()
        case err := <-errs:
            switch err {
            case oplogc.ErrAccessDenied, oplogc.ErrWritingState:
                c.Stop()
                log.Fatal(err)
            case oplogc.ErrResumeFailed:
                log.Print("Resume failed, forcing full replication")
                c.SetLastID("0")
            default:
                log.Print(err)
            }
        case <-done:
            return
        }
    }
}

In case of a connection failure recovery the ack mechanism allows you to handle operations in parallel without loosing track of which operation has been handled.

See cmd/oplog-tail/ for another usage example.

Licenses

All source code is licensed under the MIT License.

About

A Go consumer library for OpLog server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages