autobackends

package module
v0.0.0-...-3afbaae Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2019 License: Apache-2.0 Imports: 10 Imported by: 0

README

go-auto-backends

auto registers service ip:port+weight to backends list with health checks. ( kind of autonomous system )

server1

package main 

import (
    autobackends "github.com/9glt/go-auto-backends"
    "fmt"
    "flag"
)

type  PuBsub struct {
    /// implmenet autobackends.PubSub interface
}

var (
    flagMe = flag.String("me", "127.0.0.1", "my ip:port")
)

func main() {
    flag.Parse()

    routes := autobackends.NewTable()

    // area begin
    routes.Add("127.0.0.2", "127.0.0.1", 10)
	routes.Add("127.0.0.20", "127.0.0.2", -1) // failover for 5 and 6 and 3 and 4
	// area end

	// area begin
	routes.Add("127.0.0.3", "127.0.0.20", 10)
	routes.Add("127.0.0.4", "127.0.0.3", -1) // failover for 5 and 6
	// area end

	// area begin
	routes.Add("127.0.0.5", "127.0.0.4", 10)
    routes.Add("127.0.0.6", "127.0.0.5", -1)
    // area end

    b := autobackends.New(autobackends.Nats("nats://127.0.0.1", "autobackends.alive"), routes.List(*flagMe, nil), *flagMe)

    for {
        be, err := b.Get()
        if err != nil {
            println(err.Error())
        } else {
            fmt.Printf("%+v\n", be)
        }
        time.Sleep(time.Second)
    }
    runtime.Goexit()
}
go run main.go -me 127.0.0.1
go run main.go -me 127.0.0.2
go run main.go -me 127.0.0.3
go run main.go -me 127.0.0.4
go run main.go -me 127.0.0.5
go run main.go -me 127.0.0.6

turn random node off

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoAliveBackends = errors.New("no alive backends")
)

Functions

This section is empty.

Types

type Backends

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

func New

func New(pubsub PubSub, routes []Node, me string) *Backends

func (*Backends) Get

func (b *Backends) Get() (string, error)

func (*Backends) GetCached

func (b *Backends) GetCached() (string, error)

func (*Backends) Routes

func (b *Backends) Routes() []Node

func (*Backends) SetRoutes

func (b *Backends) SetRoutes(r []Node)

func (*Backends) Start

func (b *Backends) Start(addr string, d int)

func (*Backends) Stop

func (b *Backends) Stop()

type NatsClient

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

func Nats

func Nats(s string, t string) *NatsClient

func (*NatsClient) Publish

func (pub *NatsClient) Publish(msg string)

func (*NatsClient) Subscribe

func (sub *NatsClient) Subscribe(fn func(string))

type Node

type Node struct {
	Addr   string
	Weight int
}

type PubSub

type PubSub interface {
	// contains filtered or unexported methods
}

type R

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

func NewTable

func NewTable() R

NewTable crates new routing table

func (*R) Add

func (r *R) Add(m, p string, weight int)

func (*R) List

func (r *R) List(me string, root *string) []Node

func (*R) Remove

func (r *R) Remove(m, p string)

type Route

type Route struct {
	Addr   string
	Weight int64
	Alive  bool
}

type SortBy

type SortBy []Node

func (SortBy) Len

func (a SortBy) Len() int

func (SortBy) Less

func (a SortBy) Less(i, j int) bool

func (SortBy) Swap

func (a SortBy) Swap(i, j int)

Jump to

Keyboard shortcuts

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