gearman-go/client/pool.go

164 lines
3.5 KiB
Go
Raw Normal View History

2013-01-04 21:12:49 +08:00
// Copyright 2011 Xing Xing <mikespook@gmail.com>.
// All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package client
import (
2013-01-24 18:13:02 +08:00
"sync"
2013-04-23 16:58:06 +08:00
"time"
2013-01-25 15:16:11 +08:00
"errors"
"math/rand"
"github.com/mikespook/gearman-go/common"
)
const (
PoolSize = 10
)
2013-01-25 15:16:11 +08:00
var (
ErrNotFound = errors.New("Server Not Found")
)
2013-01-24 18:13:02 +08:00
type poolClient struct {
*Client
Rate int
}
2013-01-24 18:13:02 +08:00
type SelectionHandler func(map[string]*poolClient, string) string
2013-01-24 18:13:02 +08:00
func SelectWithRate(pool map[string]*poolClient,
last string) (addr string) {
total := 0
for _, item := range pool {
total += item.Rate
if rand.Intn(total) < item.Rate {
2013-01-24 18:13:02 +08:00
return item.addr
}
}
return last
}
2013-01-24 18:13:02 +08:00
func SelectRandom(pool map[string]*poolClient,
last string) (addr string) {
r := rand.Intn(len(pool))
i := 0
for k, _ := range pool {
if r == i {
return k
}
i ++
}
return last
2013-01-04 21:12:49 +08:00
}
type Pool struct {
SelectionHandler SelectionHandler
ErrHandler common.ErrorHandler
2013-01-25 15:16:11 +08:00
2013-01-24 18:13:02 +08:00
clients map[string]*poolClient
2013-01-25 15:16:11 +08:00
last string
2013-01-24 18:13:02 +08:00
mutex sync.Mutex
2013-01-04 21:12:49 +08:00
}
// Create a new pool.
2013-01-04 21:12:49 +08:00
func NewPool() (pool *Pool) {
return &Pool{
2013-01-24 18:13:02 +08:00
clients: make(map[string]*poolClient, PoolSize),
SelectionHandler: SelectWithRate,
}
2013-01-04 21:12:49 +08:00
}
// Add a server with rate.
2013-01-08 17:23:10 +08:00
func (pool *Pool) Add(addr string, rate int) (err error) {
2013-01-24 18:13:02 +08:00
pool.mutex.Lock()
defer pool.mutex.Unlock()
var item *poolClient
var ok bool
2013-01-24 18:13:02 +08:00
if item, ok = pool.clients[addr]; ok {
item.Rate = rate
} else {
2013-01-24 18:13:02 +08:00
var client *Client
client, err = New(addr)
item = &poolClient{Client: client, Rate: rate}
err = item.connect()
pool.clients[addr] = item
}
2013-01-08 17:23:10 +08:00
return
2013-01-04 21:12:49 +08:00
}
2013-01-24 18:13:02 +08:00
// Remove a server.
func (pool *Pool) Remove(addr string) {
pool.mutex.Lock()
defer pool.mutex.Unlock()
delete(pool.clients, addr)
}
func (pool *Pool) Do(funcname string, data []byte,
2013-01-25 15:16:11 +08:00
flag byte, h JobHandler) (addr, handle string) {
client := pool.selectServer()
handle = client.Do(funcname, data, flag, h)
addr = client.addr
2013-01-18 17:03:45 +08:00
return
}
func (pool *Pool) DoBg(funcname string, data []byte,
2013-01-25 15:16:11 +08:00
flag byte) (addr, handle string) {
client := pool.selectServer()
handle = client.DoBg(funcname, data, flag)
addr = client.addr
return
2013-01-04 21:12:49 +08:00
}
// Get job status from job server.
// !!!Not fully tested.!!!
2013-04-23 16:58:06 +08:00
func (pool *Pool) Status(addr, handle string, timeout time.Duration) (status *Status, err error) {
2013-01-25 15:16:11 +08:00
if client, ok := pool.clients[addr]; ok {
2013-04-23 16:58:06 +08:00
status, err = client.Status(handle, timeout)
2013-01-25 15:16:11 +08:00
} else {
err = ErrNotFound
}
2013-01-24 18:13:02 +08:00
return
2013-01-04 21:12:49 +08:00
}
// Send a something out, get the samething back.
2013-01-25 15:16:11 +08:00
func (pool *Pool) Echo(addr string, data []byte) (r []byte, err error) {
var client *poolClient
if addr == "" {
client = pool.selectServer()
} else {
var ok bool
if client, ok = pool.clients[addr]; !ok {
err = ErrNotFound
return
}
}
r = client.Echo(data)
2013-01-24 18:13:02 +08:00
return
2013-01-04 21:12:49 +08:00
}
// Close
2013-01-08 17:23:10 +08:00
func (pool *Pool) Close() (err map[string]error) {
err = make(map[string]error)
2013-01-24 18:13:02 +08:00
for _, c := range pool.clients {
err[c.addr] = c.Close()
}
return
2013-01-04 21:12:49 +08:00
}
2013-01-25 15:16:11 +08:00
func (pool *Pool) selectServer() (client *poolClient) {
for client == nil {
addr := pool.SelectionHandler(pool.clients, pool.last)
var ok bool
if client, ok = pool.clients[addr]; ok {
pool.last = addr
break
}
}
return
}