feat(longpoll-user): invoke func in a goroutine
@@ -95,6 +95,7 @@ type LongPoll struct {
funcList map[int][]EventNewFunc
funcFullResponseList []func(object.LongPollResponse)
inShutdown int32
+ goroutine bool
}
// NewLongPoll returns a new LongPoll.
@@ -117,6 +118,11 @@ func NewLongPoll(vk *api.VK, mode Mode) (*LongPoll, error) {
return lp, err
}
+// Goroutine invoke functions in a goroutine.
+func (lp *LongPoll) Goroutine(v bool) {
+ lp.goroutine = v
+}
+
func (lp *LongPoll) updateServer(updateTs bool) error {
params := api.Params{
"lp_version": lp.Version,
@@ -187,10 +193,15 @@ func (lp LongPoll) handler(event []interface{}) error {
key := int(event[0].(float64))
for _, f := range lp.funcList[key] {
- if err := f(event); err != nil {
+ if lp.goroutine {
+ go func() { _ = f(event) }()
+ } else {
+ err := f(event)
+ if err != nil {
return err
}
}
+ }
return nil
}
@@ -212,9 +223,13 @@ func (lp *LongPoll) Run() error {
}
for _, f := range lp.funcFullResponseList {
+ if lp.goroutine {
+ go func() { f(resp) }()
+ } else {
f(resp)
}
}
+ }
return nil
}
