From 416eb4460bd198e6b29b77d171fafa019e5530cc Mon Sep 17 00:00:00 2001 From: "insanity@loafle.com" Date: Mon, 15 May 2017 19:05:54 +0900 Subject: [PATCH] long poller --- poller.go | 49 +++++++++++++++++++++++++++++-------------------- poller_event.go | 27 --------------------------- poller_test.go | 19 ------------------- 3 files changed, 29 insertions(+), 66 deletions(-) delete mode 100644 poller_event.go diff --git a/poller.go b/poller.go index 52edb7a..6fa8f54 100644 --- a/poller.go +++ b/poller.go @@ -12,7 +12,7 @@ import ( ) const ( - POLLING_ID = "OVERFLOW_LONG_POLLING" + POLLING_ID = "OVERFLOW_LONG_POLLING" ) var ( @@ -21,23 +21,35 @@ var ( ) type Poller struct { - once sync.Once runStat chan bool gconf *cm.GlobalConfig scheduler *s.Scheduler taskCh chan interface{} } -func Start(conf *cm.GlobalConfig) (chan interface{}, error) { +func Start(ch chan string, conf *cm.GlobalConfig) (chan interface{}, error) { p := GetInstance() p.taskCh = make(chan interface{}) err := p.startPolling(conf) if err != nil { return nil, err } + + key := getSecretKey() //todo. getting api key + ch <- key return p.taskCh, nil } +func getSecretKey() string { + return "tempkey" +} + +func Stop(polStopped chan bool) error { + GetInstance().stop() + polStopped <- true + return nil +} + func GetInstance() *Poller { once.Do(func() { instance = &Poller{} @@ -45,6 +57,14 @@ func GetInstance() *Poller { return instance } +func UpdateInterval(interval string) error { + v, err := strconv.Atoi(interval) + if err != nil { + return err + } + GetInstance().updateInterval(v) +} + func (p *Poller) startPolling(conf *cm.GlobalConfig) error { p.gconf = conf p.scheduler = &s.Scheduler{} @@ -70,9 +90,8 @@ func (p *Poller) handleInterval(ch chan interface{}) { }() } -func (p *Poller) Stop(ch chan interface{}) { +func (p *Poller) stop() { p.scheduler.Stop() - p.runStat <- false } func (p *Poller) polling(agentId string) { @@ -87,28 +106,18 @@ func (p *Poller) polling(agentId string) { //todo temporary client := pb.NewStatusClient(conn) - //printStatusStream(client) //stream - out, err := client.Status(context.Background(), &pb.Empty{}) if err != nil { log.Println(err) } - p.dispatchNotify(out) + + + p.taskCh <- out } -func (p *Poller) dispatchNotify(result interface{}) { - log.Printf("Polling result - %s ", result) - p.taskCh <- result -} - -func (p *Poller) updateInterval(interval string) { - i, err := strconv.Atoi(interval) - if err != nil { - log.Println(err) - return - } - p.scheduler.UpdateInterval(POLLING_ID, uint64(i)) +func (p *Poller) updateInterval(interval int) { + p.scheduler.UpdateInterval(POLLING_ID, uint64(interval)) } /* diff --git a/poller_event.go b/poller_event.go deleted file mode 100644 index fc55c2a..0000000 --- a/poller_event.go +++ /dev/null @@ -1,27 +0,0 @@ -package long_poller_go - -//import ( -// "loafle.com/overflow/agent_api/observer" -// "loafle.com/overflow/agent_api/observer/messages" -//) -// -//func handleConfigLoaded() { -// ch := make(chan interface{}, 0) -// observer.Add(messages.CFG_LOADED, ch) -// startPoller(ch) -//} -// -//func (p *Poller) handleAgentStop() { -// ch := make(chan interface{}, 0) -// observer.Add(messages.AGT_STOPPED, ch) -// _ = <-ch -// p.Stop(ch) -//} -// -//func (p *Poller) removeConfigLoadedHandler(ch chan interface{}) { -// observer.Remove(messages.CFG_LOADED, ch) -//} -// -//func (p *Poller) removeAgentStopHandler(ch chan interface{}) { -// observer.Remove(messages.AGT_STOPPED, ch) -//} diff --git a/poller_test.go b/poller_test.go index da85903..39ad419 100644 --- a/poller_test.go +++ b/poller_test.go @@ -1,20 +1 @@ package long_poller_go - -//import ( -// "loafle.com/overflow/agent_api/observer" -// "loafle.com/overflow/agent_api/observer/messages" -// "testing" -// "time" -//) -// -//func TestPolling(t *testing.T) { -// poller := &Poller{} -// poller.start() -//} -// -//func TestTotal(t *testing.T) { -// time.Sleep(time.Second * 5) -// -// observer.Notify(messages.CFG_LOADED, "") //CONFIG LOCATION -// time.Sleep(time.Second * 100) -//}