lb

package
v0.0.0-...-1a196e1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2017 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package lb 能夠協助你建立一個負載平衡器給 gRPC 伺服器使用。

Index

Constants

This section is empty.

Variables

View Source
var (
	// RoundRobin 是輪詢模式,負載平衡器每次都會按照順序逐一採用不同的伺服器,確保每個伺服器都會被使用到。
	RoundRobin = 0
	// Random 是隨機模式,負載平衡器每次都會隨機挑選一個伺服器使用。
	Random = 1
)

Functions

func NewBalancer

func NewBalancer(opt interface{}) grpc.Balancer

NewBalancer 會依照接收的選項型態來建立新的指定形態負載平衡器。

Types

type ConsulBalancer

type ConsulBalancer struct {
	// Option 是負載平衡器建立時的選項。
	Option ConsulOption

	// Addresses 包含了所有可用的實例地址。
	Addresses []grpc.Address
	// contains filtered or unexported fields
}

ConsulBalancer 是一個基於 Consul 的負載平衡器。

func (*ConsulBalancer) Close

func (b *ConsulBalancer) Close() error

Close 會關閉負載平衡器。

func (*ConsulBalancer) Get

func (b *ConsulBalancer) Get(ctx context.Context, opts grpc.BalancerGetOptions) (addr grpc.Address, put func(), err error)

Get 會基於 ctx 內容來回傳一個 gRPC 伺服器的地址。

put 函式會在 RPC 完成或是失敗的時候呼叫。 這個函式可以用來收集該次 RPC 的狀態到負載平衡器做為統計。

當負載平衡器無法從錯誤中回復時應該回傳 err 錯誤,如果回傳了錯誤,gRPC 會將該次的 RPC 請求標記為失敗。

func (*ConsulBalancer) Notify

func (b *ConsulBalancer) Notify() <-chan []grpc.Address

func (*ConsulBalancer) Start

func (b *ConsulBalancer) Start(target string, config grpc.BalancerConfig) error

Start 是用來初始化負載平衡器的函式。這個函式會在 gRPC 進行 Dial 時呼叫。

func (*ConsulBalancer) Up

func (b *ConsulBalancer) Up(addr grpc.Address) (down func(error))

type ConsulOption

type ConsulOption struct {
	// Name 是服務名稱,我們將會搜尋此服務的所有主機。
	Name string
	// Mode 是查詢模式,如 `lb.RoundRobin` 或 `lb.Random`。
	Mode int
	// Client 是 Go Svc 的 Consul 客戶端。
	Client consul.Client
	//
	Tag string
}

ConsulOption 是用來建立基於 Consul 負載平衡器的選項。

type FixedBalancer

type FixedBalancer struct {
	// Option 是負載平衡器建立時的選項。
	Option FixedOption

	// Addresses 包含了所有可用的實例地址。
	Addresses []grpc.Address
	// contains filtered or unexported fields
}

FixedBalancer 是一個基本並帶有固定實例的負載平衡器。

func (*FixedBalancer) Close

func (b *FixedBalancer) Close() error

Close 會關閉負載平衡器。

func (*FixedBalancer) Get

func (b *FixedBalancer) Get(ctx context.Context, opts grpc.BalancerGetOptions) (addr grpc.Address, put func(), err error)

Get 會基於 ctx 內容來回傳一個 gRPC 伺服器的地址。

put 函式會在 RPC 完成或是失敗的時候呼叫。 這個函式可以用來收集該次 RPC 的狀態到負載平衡器做為統計。

當負載平衡器無法從錯誤中回復時應該回傳 err 錯誤,如果回傳了錯誤,gRPC 會將該次的 RPC 請求標記為失敗。

func (*FixedBalancer) Notify

func (b *FixedBalancer) Notify() <-chan []grpc.Address

func (*FixedBalancer) Start

func (b *FixedBalancer) Start(target string, config grpc.BalancerConfig) error

Start 是用來初始化負載平衡器的函式。這個函式會在 gRPC 進行 Dial 時呼叫。

func (*FixedBalancer) Up

func (b *FixedBalancer) Up(addr grpc.Address) (down func(error))

type FixedOption

type FixedOption struct {
	// Mode 是查詢模式,如 `lb.RoundRobin` 或 `lb.Random`。
	Mode int
	// Instances 集合了所有實例的地址,帶有位置和埠口。例如:`localhost:50050`。
	Instances []string
}

FixedOption 是用來建立基本、固定實例的負載平衡器選項。

Jump to

Keyboard shortcuts

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