request_wait.go 830 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package rest_websocket
  2. import (
  3. "log"
  4. "time"
  5. "git.ali33.ru/fcg-xvii/rest"
  6. )
  7. func newWaitRequest(id int64, timeout time.Time) *waitRequest {
  8. now := time.Now()
  9. if timeout.Before(now) {
  10. timeout = now.Add(time.Second * 10)
  11. }
  12. res := &waitRequest{
  13. id: id,
  14. answer: make(chan *rest.RequestStream, 1),
  15. answerIn: make(chan *rest.RequestStream, 1),
  16. timeout: timeout,
  17. }
  18. go res.exec()
  19. return res
  20. }
  21. type waitRequest struct {
  22. id int64
  23. answer chan *rest.RequestStream
  24. answerIn chan *rest.RequestStream
  25. timeout time.Time
  26. }
  27. func (s *waitRequest) exec() {
  28. select {
  29. case answ, ok := <-s.answerIn:
  30. if !ok {
  31. close(s.answer)
  32. return
  33. }
  34. s.answer <- answ
  35. case <-time.After(time.Until(s.timeout)):
  36. log.Println("RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR")
  37. close(s.answer)
  38. }
  39. }