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.
 
 
 

199 lines
5.2 KiB

  1. // Copyright 2011 Xing Xing <mikespook@gmail.com> All rights reserved.
  2. // Use of this source code is governed by a MIT
  3. // license that can be found in the LICENSE file.
  4. package worker
  5. import (
  6. "io"
  7. "net"
  8. "github.com/mikespook/gearman-go/common"
  9. )
  10. // The agent of job server.
  11. type agent struct {
  12. conn net.Conn
  13. worker *Worker
  14. in chan []byte
  15. out chan *Job
  16. addr string
  17. }
  18. // Create the agent of job server.
  19. func newAgent(addr string, worker *Worker) (a *agent, err error) {
  20. conn, err := net.Dial(common.NETWORK, addr)
  21. if err != nil {
  22. return
  23. }
  24. a = &agent{
  25. conn: conn,
  26. worker: worker,
  27. addr: addr,
  28. in: make(chan []byte, common.QUEUE_SIZE),
  29. out: make(chan *Job, common.QUEUE_SIZE),
  30. }
  31. // reset abilities
  32. a.WriteJob(newJob(common.REQ, common.RESET_ABILITIES, nil))
  33. return
  34. }
  35. // outputing loop
  36. func (a *agent) outLoop() {
  37. ok := true
  38. var job *Job
  39. for ok {
  40. if job, ok = <-a.out; ok {
  41. if err := a.write(job.Encode()); err != nil {
  42. a.worker.err(err)
  43. }
  44. }
  45. }
  46. }
  47. // inputing loop
  48. func (a *agent) inLoop() {
  49. defer func() {
  50. if r := recover(); r != nil {
  51. a.worker.err(common.Errorf("Exiting: %s", r))
  52. }
  53. close(a.in)
  54. close(a.out)
  55. a.worker.removeAgent(a)
  56. }()
  57. for a.worker.running {
  58. a.WriteJob(newJob(common.REQ, common.PRE_SLEEP, nil))
  59. RESTART:
  60. // got noop msg and in queue is zero, grab job
  61. rel, err := a.read()
  62. if err != nil {
  63. if err == common.ErrConnection {
  64. for i := 0; i < 3 && a.worker.running; i++ {
  65. if conn, err := net.Dial(common.NETWORK, a.addr); err != nil {
  66. a.worker.err(common.Errorf("Reconnection: %d faild", i))
  67. continue
  68. } else {
  69. a.conn = conn
  70. goto RESTART
  71. }
  72. }
  73. a.worker.err(err)
  74. break
  75. }
  76. a.worker.err(err)
  77. continue
  78. }
  79. job, err := decodeJob(rel)
  80. if err != nil {
  81. a.worker.err(err)
  82. continue
  83. }
  84. switch job.DataType {
  85. case common.NOOP:
  86. a.WriteJob(newJob(common.REQ, common.GRAB_JOB_UNIQ, nil))
  87. case common.ERROR, common.ECHO_RES, common.JOB_ASSIGN_UNIQ, common.JOB_ASSIGN:
  88. job.agent = a
  89. a.worker.in <- job
  90. }
  91. }
  92. }
  93. func (a *agent) Close() {
  94. a.conn.Close()
  95. }
  96. func (a *agent) Work() {
  97. go a.outLoop()
  98. go a.inLoop()
  99. }
  100. func (a *agent) readData(length int) (data []byte, err error) {
  101. n := 0
  102. buf := make([]byte, common.BUFFER_SIZE)
  103. // read until data can be unpacked
  104. for i := length; i > 0 || len(data) < common.PACKET_LEN; i -= n {
  105. if n, err = a.conn.Read(buf); err != nil {
  106. if err == io.EOF && n == 0 {
  107. if data == nil {
  108. err = common.ErrConnection
  109. return
  110. }
  111. return data, nil
  112. }
  113. return
  114. }
  115. data = append(data, buf[0:n]...)
  116. if n < common.BUFFER_SIZE {
  117. break
  118. }
  119. }
  120. return
  121. }
  122. func (a *agent) unpack(data []byte) ([]byte, int, bool) {
  123. tl := len(data)
  124. start := 0
  125. for i := 0; i < tl+1-common.PACKET_LEN; i++ {
  126. if start+common.PACKET_LEN > tl { // too few data to unpack, read more
  127. return nil, common.PACKET_LEN, false
  128. }
  129. if string(data[start:start+4]) == common.RES_STR {
  130. l := int(common.BytesToUint32([4]byte{data[start+8],
  131. data[start+9], data[start+10], data[start+11]}))
  132. total := l + common.PACKET_LEN
  133. if total == tl { // data is what we want
  134. return data, common.PACKET_LEN, true
  135. } else if total < tl { // data[:total] is what we want, data[total:] is the more
  136. a.in <- data[total:]
  137. data = data[start:total]
  138. return data, common.PACKET_LEN, true
  139. } else { // ops! It won't be possible.
  140. return nil, total - tl, false
  141. }
  142. } else { // flag was not found, move to next step
  143. start++
  144. }
  145. }
  146. return nil, common.PACKET_LEN, false
  147. }
  148. func (a *agent) read() (rel []byte, err error) {
  149. var data []byte
  150. ok := false
  151. l := common.PACKET_LEN
  152. for !ok {
  153. inlen := len(a.in)
  154. if inlen > 0 {
  155. // in queue is not empty
  156. for i := 0; i < inlen; i++ {
  157. data = append(data, <-a.in...)
  158. }
  159. } else {
  160. var d []byte
  161. d, err = a.readData(l)
  162. if err != nil {
  163. return
  164. }
  165. data = append(data, d...)
  166. }
  167. rel, l, ok = a.unpack(data)
  168. }
  169. return
  170. }
  171. // Send a job to the job server.
  172. func (a *agent) WriteJob(job *Job) {
  173. a.out <- job
  174. }
  175. // Internal write the encoded job.
  176. func (a *agent) write(buf []byte) (err error) {
  177. var n int
  178. for i := 0; i < len(buf); i += n {
  179. n, err = a.conn.Write(buf[i:])
  180. if err != nil {
  181. return err
  182. }
  183. }
  184. return
  185. }