1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package microservice
- import (
- "time"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- )
- func NewQuestion(data json.Map, timeout time.Duration) *Question {
- if timeout == 0 {
- timeout = time.Minute
- }
- q := &Question{
- data: data,
- timeout: timeout,
- wAnswer: make(chan json.Map, 1),
- }
- return q
- }
- // Request
- type Question struct {
- id int64
- data json.Map
- socket *Socket
- created time.Time
- timeout time.Duration
- wAnswer chan json.Map
- }
- func (s *Question) IsActual(t time.Time) bool {
- return s.created.Add(s.timeout).After(t)
- }
- func (s *Question) WaitAnswer() <-chan json.Map {
- return s.wAnswer
- }
- func (s *Question) Type() RequestType {
- return RequestQuestion
- }
- func (s *Question) JSONMap() json.Map {
- return json.Map{
- "id": s.id,
- "type": RequestQuestion,
- "data": s.data,
- }
- }
- func (s *Question) Answer(data json.Map) {
- if s.timeout > 0 {
- s.timeout = time.Minute
- }
- m := json.Map{
- "id": s.id,
- "type": RequestAnswer,
- "data": data,
- }
- s.socket.sendMap(m)
- }
- func (s *Question) Data() json.Map {
- return s.data
- }
|