taskprocess.go 1.6 KB

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