runner

package
v0.0.0-...-4919d5a Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package runner implements a multiplexer on top of the partition package.

This allows the requests, responses to be more strongly typed than raw byte slices. The serialization protocol used is gob-encoding.

This package makes heavy use of reflection.

See the example for sample usage.

Example
package main

import (
	"context"
	"encoding/gob"
	"fmt"

	"github.com/alicebob/miniredis"
	"github.com/tvastar/cluster/pkg/partition"
	"github.com/tvastar/cluster/pkg/partition/runner"
)

func main() {
	opt, cleanup := exampleSetup()
	defer cleanup()

	h := runner.Handlers{}
	intf := h.Register(func(ctx context.Context, r IntRequest) (string, error) {
		return "hello", nil
	}).(func(ctx context.Context, r IntRequest) (string, error))

	strf := h.Register(func(ctx context.Context, r StringRequest) (string, error) {
		return string(r), nil
	}).(func(ctx context.Context, r StringRequest) (string, error))

	h.Start(context.Background(), ":2000", opt)

	s, err := intf(context.Background(), IntRequest(0))
	fmt.Println("Got", s, err)

	s, err = strf(context.Background(), StringRequest("boo"))
	fmt.Println("Got", s, err)

}

type IntRequest int

func (i IntRequest) Hash() uint64 {
	return 0
}

type StringRequest string

func (s StringRequest) Hash() uint64 {
	return 5
}

func exampleSetup() (partition.Option, func()) {
	gob.Register(IntRequest(0))
	gob.Register(StringRequest("hello"))

	minir, err := miniredis.Run()
	if err != nil {
		panic(err)
	}
	opt := partition.WithEndpointRegistry(partition.NewRedisRegistry(minir.Addr(), "prefix_"))

	return opt, func() { minir.Close() }
}
Output:

Got hello <nil>
Got boo <nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handlers

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

Handlers allows multiplexing multiple handlers with the same partitioner.

Handlers must have the type func(Context, Req) (Res, error)

Further, the Req type must implement a method `Hash() uint64`.

This uses gob-encoding and so the Req/Res types must be registered via `gob.Register(sampleReqValue)`

See example for usage details

func (*Handlers) Register

func (h *Handlers) Register(fn interface{}) interface{}

Register registers function as a handler. It returns a function (of the same signature as the input function).

The fn must have the type func(Context, Req) (Res, error)

Further, the Req type must implement a method `Hash() uint64`.

The function returned can be used to make partitioned calls (i.e. the calls are automatically routed to the right endpoint).

func (*Handlers) Start

func (h *Handlers) Start(ctx context.Context, address string, opts ...partition.Option) error

Start creates a new partitioner under the covers and registers all the handlers with it. All the functions returned by the Register calls can now be used.

func (*Handlers) Stop

func (h *Handlers) Stop() error

Stop closes the partitioner

Jump to

Keyboard shortcuts

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