client

package
v0.0.0-...-2345967 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2016 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

The client package helps developers connect to Gearmand, send jobs and fetch result.

Index

Constants

View Source
const (
	Network = "tcp"

	WorkComplate  = dtWorkComplete
	WorkData      = dtWorkData
	WorkStatus    = dtWorkStatus
	WorkWarning   = dtWorkWarning
	WorkFail      = dtWorkFail
	WorkException = dtWorkException
)
View Source
const (
	// Job type
	JobNormal = iota
	// low level
	JobLow
	// high level
	JobHigh
)

Variables

View Source
var (
	ErrWorkWarning   = errors.New("Work warning")
	ErrInvalidData   = errors.New("Invalid data")
	ErrWorkFail      = errors.New("Work fail")
	ErrWorkException = errors.New("Work exeption")
	ErrDataType      = errors.New("Invalid data type")
	ErrLostConn      = errors.New("Lost connection with Gearmand")
)
View Source
var (
	DefaultTimeout time.Duration = 1000
)
View Source
var (
	ErrNotFound = errors.New("Server Not Found")
)

Functions

func SelectRandom

func SelectRandom(pool map[string]*PoolClient,
	last string) (addr string)

func SelectWithRate

func SelectWithRate(pool map[string]*PoolClient,
	last string) (addr string)

Types

type Client

type Client struct {
	sync.Mutex

	ResponseTimeout time.Duration // response timeout for do() in ms

	ErrorHandler ErrorHandler
	// contains filtered or unexported fields
}

One client connect to one server. Use Pool for multi-connections.

func New

func New(network, addr string) (client *Client, err error)

Return a client.

func (*Client) Close

func (client *Client) Close() (err error)

Close connection

func (*Client) Do

func (client *Client) Do(funcname string, data []byte,
	flag byte, h ResponseHandler) (handle string, err error)

Call the function and get a response. flag can be set to: JobLow, JobNormal and JobHigh

func (*Client) DoBg

func (client *Client) DoBg(funcname string, data []byte,
	flag byte) (handle string, err error)

Call the function in background, no response needed. flag can be set to: JobLow, JobNormal and JobHigh

func (*Client) Echo

func (client *Client) Echo(data []byte) (echo []byte, err error)

Echo.

func (*Client) Status

func (client *Client) Status(handle string) (status *Status, err error)

Get job status from job server.

type ErrorHandler

type ErrorHandler func(error)

Error handler

type IdGenerator

type IdGenerator interface {
	Id() string
}

ID generator interface. Users can implament this for their own generator.

var (
	// Global ID generator
	// Default is an autoincrement ID generator
	IdGen IdGenerator
)

func NewAutoIncId

func NewAutoIncId() IdGenerator

Return an autoincrement ID generator

type Pool

type Pool struct {
	SelectionHandler SelectionHandler
	ErrorHandler     ErrorHandler
	Clients          map[string]*PoolClient
	// contains filtered or unexported fields
}

func NewPool

func NewPool() (pool *Pool)

Return a new pool.

func (*Pool) Add

func (pool *Pool) Add(net, addr string, rate int) (err error)

Add a server with rate.

func (*Pool) Close

func (pool *Pool) Close() (err map[string]error)

Close

func (*Pool) Do

func (pool *Pool) Do(funcname string, data []byte,
	flag byte, h ResponseHandler) (addr, handle string, err error)

func (*Pool) DoBg

func (pool *Pool) DoBg(funcname string, data []byte,
	flag byte) (addr, handle string, err error)

func (*Pool) Echo

func (pool *Pool) Echo(addr string, data []byte) (echo []byte, err error)

Send a something out, get the samething back.

func (*Pool) Remove

func (pool *Pool) Remove(addr string)

Remove a server.

func (*Pool) Status

func (pool *Pool) Status(addr, handle string) (status *Status, err error)

Get job status from job server. !!!Not fully tested.!!!

type PoolClient

type PoolClient struct {
	*Client
	Rate int
	// contains filtered or unexported fields
}

type Response

type Response struct {
	DataType  uint32
	Data, UID []byte
	Handle    string
}

response

func (*Response) Result

func (resp *Response) Result() (data []byte, err error)

Extract the Response's result. if data == nil, err != nil, then worker failing to execute job if data != nil, err != nil, then worker has a exception if data != nil, err == nil, then worker complate job after calling this method, the Response.Handle will be filled

func (*Response) Status

func (resp *Response) Status() (status *Status, err error)

func (*Response) Update

func (resp *Response) Update() (data []byte, err error)

Extract the job's update

type ResponseHandler

type ResponseHandler func(*Response)

Response handler

type SelectionHandler

type SelectionHandler func(map[string]*PoolClient, string) string

type Status

type Status struct {
	Handle                 string
	Known, Running         bool
	Numerator, Denominator uint64
}

type StatusHandler

type StatusHandler func(string, bool, bool, uint64, uint64)

Status handler handle, known, running, numerator, denominator

Jump to

Keyboard shortcuts

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