long poller

This commit is contained in:
insanity@loafle.com 2017-04-27 10:25:43 +09:00
parent 59d517cb32
commit dccb4512f9
2 changed files with 31 additions and 7 deletions

View File

@ -10,23 +10,20 @@ import (
pb "loafle.com/overflow/crawler_go/grpc" //temp pb "loafle.com/overflow/crawler_go/grpc" //temp
) )
var ( const (
POLLING_ID = "OVERFLOW_LONG_POLLING" POLLING_ID = "OVERFLOW_LONG_POLLING"
DEFAULT_INTERVAL = uint64(3) DEFAULT_INTERVAL = uint64(3)
API_SERVER_ADDR = "127.0.0.1:50052" CENTRAL_ADDR = "127.0.0.1:50052"
) )
type LongPoller struct { type LongPoller struct {
once sync.Once once sync.Once
runStat chan bool runStat chan bool
wg sync.WaitGroup
} }
func (p *LongPoller) Start() { func (p *LongPoller) Start() {
p.once.Do(func() { p.once.Do(func() {
p.wg.Add(1)
p.startPolling() p.startPolling()
p.wg.Wait()
}) })
} }
@ -38,12 +35,11 @@ func (p *LongPoller) startPolling() {
func (p *LongPoller) Stop() { func (p *LongPoller) Stop() {
p.runStat <- false p.runStat <- false
p.wg.Done()
} }
func (p *LongPoller) polling(agentId string) { func (p *LongPoller) polling(agentId string) {
conn, err := grpc.Dial(API_SERVER_ADDR, grpc.WithInsecure()) conn, err := grpc.Dial(CENTRAL_ADDR, grpc.WithInsecure())
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
@ -52,6 +48,9 @@ func (p *LongPoller) polling(agentId string) {
//todo temporary //todo temporary
client := pb.NewStatusClient(conn) client := pb.NewStatusClient(conn)
//printStatus(client) //stream
out, err := client.Status(context.Background(), &pb.Empty{}) out, err := client.Status(context.Background(), &pb.Empty{})
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -68,3 +67,23 @@ func agentIdentifier() string {
//todo //todo
return "agent000001" return "agent000001"
} }
/*
func printStassssssssstus(client pb.StatusClient) {
stream, err := client.Status(context.Background(), &pb.Empty{})
if err != nil {
grpclog.Fatalf("%v.List(_) = _, %v", client, err)
}
for {
feature, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
grpclog.Fatalf("%v.List(_) = _, %v", client, err)
}
grpclog.Println(feature)
}
grpclog.Println("--------------------------")
}
*/

View File

@ -1,10 +1,15 @@
package long_poller_go package long_poller_go
import ( import (
"log"
"testing" "testing"
"time"
) )
func TestPolling(t *testing.T) { func TestPolling(t *testing.T) {
poller := &LongPoller{} poller := &LongPoller{}
poller.Start() poller.Start()
log.Println("hihihihihi")
} }