accord

package module
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: Apache-2.0 Imports: 10 Imported by: 1

README

Accord

GoDoc Test License

Fast and efficient coordination of distributed processes. Server and client implementation in Go on top of the gRPC framework.

Use Case

Imagine you are processing files within a directory or bucket, you would want to coordinate your workers to pick a file each, lock it for the duration of the processing and then mark it as done once finished so it's not picked up by another worker again.

Architecture

+------------+         +------------+     +--------------+
|            |         |            |     |              |
|   WORKER   | -gRPC-> |   SERVER   | --> |   BACKEND    |
|   Client   | <------ |            | <-- |  PostgreSQL  |
|            |         |            |     |              |
+------------+         +------------+     +--------------+
  • Clients are connected to (a cluster of) Accord Servers and communicate via gRPC.
  • Servers are using a pluggable backend (currently, PostgreSQL only) to coordinate state.
  • Clients maintain a local state cache to avoid hammering the server/backends.

Documentation

Please see the API documentation for package and API descriptions and examples.

Backends

  • PostgreSQL - requires version >= 9.5.
  • Mock - in-memory backend, for testing only.
  • Direct - direct allows to connect clients directy a backend, bypassing the server. Use at your own risk!

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrAcquired error is returned if the resource is already acquired.
	ErrAcquired = errors.New("accord: acquired")
	// ErrDone error is returned if the resource is already marked done.
	ErrDone = errors.New("accord: done")
	// ErrClosed error is returned by the handle if closed.
	ErrClosed = errors.New("accord: closed")
)

Functions

This section is empty.

Types

type Client

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

Client is a convenience client to the accord API.

Example
package main

import (
	"context"
	"fmt"

	"github.com/bsm/accord"
)

func main() {
	ctx := context.Background()

	// Create a new client
	client, err := accord.DialClient(ctx, "10.0.0.1:8432", &accord.ClientOptions{
		Namespace: "/custom/namespace",
	})
	if err != nil {
		panic(err)
	}
	defer client.Close()

	// Acquire resource handle.
	handle, err := client.Acquire(ctx, "my::resource", nil)
	if err == accord.ErrDone {
		fmt.Println("Resource has been already marked as done")
		return
	} else if err == accord.ErrAcquired {
		fmt.Println("Resource is currently held by another process")
		return
	} else if err != nil {
		panic(err)
	}
	defer handle.Discard()

	// Yay, we have acquired a handle on the resource, now let's do something!
	// ...

	// When done, we can mark the resource as done.
	if err := handle.Done(ctx, nil); err != nil {
		panic(err)
	}
}
Output:

func DialClient

func DialClient(ctx context.Context, target string, opt *ClientOptions, dialOpt ...grpc.DialOption) (*Client, error)

DialClient creates a new client connection.

func RPCClient

func RPCClient(ctx context.Context, rpc rpc.V1Client, opt *ClientOptions) (*Client, error)

RPCClient inits a new client.

func WrapClient

func WrapClient(ctx context.Context, cc *grpc.ClientConn, opt *ClientOptions) (*Client, error)

WrapClient inits a new client by wrapping a gRCP client connection.

func (*Client) Acquire

func (c *Client) Acquire(ctx context.Context, name string, meta map[string]string) (*Handle, error)

Acquire implements ClientConn interface.

func (*Client) Close

func (c *Client) Close() error

Close implements Client interface.

func (*Client) RPC added in v0.3.2

func (c *Client) RPC() rpc.V1Client

RPC implements Client interface.

type ClientOptions

type ClientOptions struct {
	Owner     string            // owner, default: random UUID
	Namespace string            // namespace, default: ""
	Metadata  map[string]string // default metadata
	TTL       time.Duration     // TTL, default: 10 minutes
	Dir       string            // Temporary directory, defaults to os.TempDir()
	OnError   func(error)       // custom error handler for background tasks
}

ClientOptions contains options for the client

type Handle

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

Handle holds temporary ownership of a resource. It will automatically renew its ownership in the background until either Done or Discard is called (first one wins). After a call to Done or Discard, all operations on the handle fail with ErrClosed.

func (*Handle) Discard

func (h *Handle) Discard() error

Discard discards the handle.

func (*Handle) Done

func (h *Handle) Done(ctx context.Context, meta map[string]string) error

Done marks the resource as done and invalidates the handle.

func (*Handle) ID

func (h *Handle) ID() uuid.UUID

ID returns the handle ID.

func (*Handle) Metadata

func (h *Handle) Metadata() map[string]string

Metadata returns metadata.

func (*Handle) Renew

func (h *Handle) Renew(ctx context.Context, meta map[string]string) error

Renew manually renews the ownership of the resource with custom metadata.

func (*Handle) SetMeta

func (h *Handle) SetMeta(key, value string)

SetMeta sets a metadata key. It will only be persisted with the next (background) Renew or Done call.

Directories

Path Synopsis
direct
Package direct implements a backend wrapper which allows to connect clients to a backend directly bypassing grpc servers.
Package direct implements a backend wrapper which allows to connect clients to a backend directly bypassing grpc servers.
mock
Package mock implements an in-memory mock backend for testing.
Package mock implements an in-memory mock backend for testing.
postgres
Package postgres implements a postgres-based backend for storing state.
Package postgres implements a postgres-based backend for storing state.
cmd
internal

Jump to

Keyboard shortcuts

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