diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-13 20:39:39 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-13 20:39:39 +0300 |
commit | 9ea34f2a2e7aa0249b73b5d9bb05795a668f270d (patch) | |
tree | bca37e243209d8371e6a261b214bd0855ff4cad4 /api.go | |
parent | 3da482d48583a697e9cd0dab44d3147c9bc19387 (diff) |
Improve longpolling
Added chatID and userID to UpdateInterface
Diffstat (limited to 'api.go')
-rw-r--r-- | api.go | 52 |
1 files changed, 26 insertions, 26 deletions
@@ -21,7 +21,6 @@ type Api struct { Subscriptions *subscriptions Uploads *uploads client *client - updates chan UpdateInterface timeout int pause int } @@ -38,7 +37,6 @@ func New(key string) *Api { Messages: newMessages(cl), Subscriptions: newSubscriptions(cl), client: cl, - updates: make(chan UpdateInterface), timeout: timeout, pause: 1, } @@ -172,33 +170,35 @@ func (a *Api) getUpdates(limit int, timeout int, marker int, types []string) (*U return result, json.Unmarshal(jb, result) } -func (a *Api) UpdatesLoop(ctx context.Context) error { - for { - select { - case <-ctx.Done(): - return nil - case <-time.After(time.Duration(a.pause) * time.Second): - var marker int - for { - upds, err := a.getUpdates(50, a.timeout, marker, []string{}) - if err != nil { - return err - } - if len(upds.Updates) == 0 { - break - } - for _, u := range upds.Updates { - a.updates <- a.bytesToProperUpdate(u) +//GetUpdates returns updates channel +func (a *Api) GetUpdates(ctx context.Context) chan UpdateInterface { + ch := make(chan UpdateInterface) + go func() { + for { + select { + case <-ctx.Done(): + close(ch) + return + case <-time.After(time.Duration(a.pause) * time.Second): + var marker int + for { + upds, err := a.getUpdates(50, a.timeout, marker, []string{}) + if err != nil { + log.Println(err) + break + } + if len(upds.Updates) == 0 { + break + } + for _, u := range upds.Updates { + ch <- a.bytesToProperUpdate(u) + } + marker = *upds.Marker } - marker = *upds.Marker } } - } -} - -//GetUpdates returns updates channel -func (a *Api) GetUpdates() chan UpdateInterface { - return a.updates + }() + return ch } //GetHandler returns http handler for webhooks |