You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pool.go 3.4 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2011 Xing Xing <mikespook@gmail.com>.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT
  4. // license that can be found in the LICENSE file.
  5. package client
  6. import (
  7. "sync"
  8. "errors"
  9. "math/rand"
  10. "github.com/mikespook/gearman-go/common"
  11. )
  12. const (
  13. PoolSize = 10
  14. )
  15. var (
  16. ErrNotFound = errors.New("Server Not Found")
  17. )
  18. type poolClient struct {
  19. *Client
  20. Rate int
  21. }
  22. type SelectionHandler func(map[string]*poolClient, string) string
  23. func SelectWithRate(pool map[string]*poolClient,
  24. last string) (addr string) {
  25. total := 0
  26. for _, item := range pool {
  27. total += item.Rate
  28. if rand.Intn(total) < item.Rate {
  29. return item.addr
  30. }
  31. }
  32. return last
  33. }
  34. func SelectRandom(pool map[string]*poolClient,
  35. last string) (addr string) {
  36. r := rand.Intn(len(pool))
  37. i := 0
  38. for k, _ := range pool {
  39. if r == i {
  40. return k
  41. }
  42. i ++
  43. }
  44. return last
  45. }
  46. type Pool struct {
  47. SelectionHandler SelectionHandler
  48. ErrHandler common.ErrorHandler
  49. clients map[string]*poolClient
  50. last string
  51. mutex sync.Mutex
  52. }
  53. // Create a new pool.
  54. func NewPool() (pool *Pool) {
  55. return &Pool{
  56. clients: make(map[string]*poolClient, PoolSize),
  57. SelectionHandler: SelectWithRate,
  58. }
  59. }
  60. // Add a server with rate.
  61. func (pool *Pool) Add(addr string, rate int) (err error) {
  62. pool.mutex.Lock()
  63. defer pool.mutex.Unlock()
  64. var item *poolClient
  65. var ok bool
  66. if item, ok = pool.clients[addr]; ok {
  67. item.Rate = rate
  68. } else {
  69. var client *Client
  70. client, err = New(addr)
  71. item = &poolClient{Client: client, Rate: rate}
  72. err = item.connect()
  73. pool.clients[addr] = item
  74. }
  75. return
  76. }
  77. // Remove a server.
  78. func (pool *Pool) Remove(addr string) {
  79. pool.mutex.Lock()
  80. defer pool.mutex.Unlock()
  81. delete(pool.clients, addr)
  82. }
  83. func (pool *Pool) Do(funcname string, data []byte,
  84. flag byte, h JobHandler) (addr, handle string) {
  85. client := pool.selectServer()
  86. handle = client.Do(funcname, data, flag, h)
  87. addr = client.addr
  88. return
  89. }
  90. func (pool *Pool) DoBg(funcname string, data []byte,
  91. flag byte) (addr, handle string) {
  92. client := pool.selectServer()
  93. handle = client.DoBg(funcname, data, flag)
  94. addr = client.addr
  95. return
  96. }
  97. // Get job status from job server.
  98. // !!!Not fully tested.!!!
  99. func (pool *Pool) Status(addr, handle string) (status *Status, err error) {
  100. if client, ok := pool.clients[addr]; ok {
  101. status = client.Status(handle)
  102. } else {
  103. err = ErrNotFound
  104. }
  105. return
  106. }
  107. // Send a something out, get the samething back.
  108. func (pool *Pool) Echo(addr string, data []byte) (r []byte, err error) {
  109. var client *poolClient
  110. if addr == "" {
  111. client = pool.selectServer()
  112. } else {
  113. var ok bool
  114. if client, ok = pool.clients[addr]; !ok {
  115. err = ErrNotFound
  116. return
  117. }
  118. }
  119. r = client.Echo(data)
  120. return
  121. }
  122. // Close
  123. func (pool *Pool) Close() (err map[string]error) {
  124. err = make(map[string]error)
  125. for _, c := range pool.clients {
  126. err[c.addr] = c.Close()
  127. }
  128. return
  129. }
  130. func (pool *Pool) selectServer() (client *poolClient) {
  131. for client == nil {
  132. addr := pool.SelectionHandler(pool.clients, pool.last)
  133. var ok bool
  134. if client, ok = pool.clients[addr]; ok {
  135. pool.last = addr
  136. break
  137. }
  138. }
  139. return
  140. }