42 lines
671 B
Go
42 lines
671 B
Go
package long_poller_go
|
|
|
|
import (
|
|
"fmt"
|
|
"loafle.com/overflow/cron_go"
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
POLLING_ID = "OVERFLOW_LONG_POLLING"
|
|
DEFAULT_INTERVAL = uint64(3)
|
|
)
|
|
|
|
type LongPoller struct {
|
|
once sync.Once
|
|
runStat chan bool
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
func (p *LongPoller) Start() {
|
|
p.once.Do(func() {
|
|
p.wg.Add(1)
|
|
p.startPolling()
|
|
p.wg.Wait()
|
|
})
|
|
}
|
|
|
|
func (p *LongPoller) startPolling() {
|
|
cr := &cron.Cron{}
|
|
p.runStat = cr.Start()
|
|
cr.AddTask(POLLING_ID, DEFAULT_INTERVAL).Invoke(p.poll, "agent0001")
|
|
}
|
|
|
|
func (p *LongPoller) Stop() {
|
|
p.runStat <- false
|
|
p.wg.Done()
|
|
}
|
|
|
|
func (p *LongPoller) poll(agentId string) {
|
|
fmt.Printf("%s :: LongPolling\n", agentId)
|
|
}
|