taskprocess.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. _ "bitcoin"
  4. _ "bucash"
  5. _ "dcrcash"
  6. "errors"
  7. "fmt"
  8. _ "lite"
  9. "template"
  10. "time"
  11. _ "zcash"
  12. )
  13. func coinStart() {
  14. go dataProcesser(CPUNUM)
  15. httpNetWorkServer()
  16. }
  17. type taskHandler struct {
  18. retcode int
  19. data []byte
  20. complete chan bool
  21. }
  22. func NewtaskHandler() *taskHandler {
  23. taskhdl := &taskHandler{}
  24. taskhdl.complete = make(chan bool, 1)
  25. return taskhdl
  26. }
  27. func (s *taskHandler) waitProcessData() ([]byte, error) {
  28. timeout := time.NewTicker(time.Duration(200) * time.Second)
  29. select {
  30. case <-timeout.C:
  31. log.Error("deal request timeout")
  32. return nil, errors.New("timeout")
  33. case <-s.complete:
  34. }
  35. // if s.retcode != 0 {
  36. // //sendstr := fmt.Sprintf(`{"errocde":%v,"msg":"%v"}`, s.retcode, GetErrMsg(s.retcode, s.data))
  37. // //return []byte(sendstr), nil
  38. // return s.data
  39. // }
  40. return s.data, nil
  41. }
  42. func dataProcesser(processNum int) {
  43. for i := 0; i < processNum; i++ {
  44. go func(index int) {
  45. for {
  46. task := <-taskChan[index]
  47. go processer(task)
  48. }
  49. }(i)
  50. }
  51. }
  52. func processer(task *taskInfo) {
  53. c, err := template.New(task.cointype)
  54. if err != nil {
  55. //task.resp.retcode = UNKNOWN_ERR
  56. sendstr := fmt.Sprintf(`{"errcode:%v,"msg":"%v"}`, INIT_DRIVER_ERR, GetErrMsg(INIT_DRIVER_ERR, nil))
  57. log.Error("sendstr:%v", sendstr)
  58. task.resp.data = []byte(sendstr)
  59. task.resp.complete <- false
  60. return
  61. }
  62. log.Debug("urlpath:%v", task.urlpath)
  63. resp, err := template.Handler[task.urlpath](c, task.jshandler)
  64. if err != nil {
  65. //task.resp.retcode = UNKNOWN_ERR
  66. task.resp.data = []byte(err.Error())
  67. task.resp.complete <- false
  68. return
  69. }
  70. task.resp.retcode = OK
  71. task.resp.data = resp
  72. task.resp.complete <- true
  73. return
  74. }