diff --git a/core/http.go b/core/http.go index d40e4f8a8..a6862128a 100644 --- a/core/http.go +++ b/core/http.go @@ -7,7 +7,6 @@ import ( "net/http" "pilosa/config" "pilosa/db" - "pilosa/hold" "pilosa/query" "strconv" @@ -135,11 +134,13 @@ func (self *WebService) HandlePing(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusNotFound) return } - id, _ := uuid.NewV4() - ping := db.Message{Data: db.PingRequest{Id: id, Source: self.service.Id}} - self.service.Transport.Send(&ping, process_id) - data, err := hold.Hold.Get(id, 60) - spew.Fdump(w, data, err) + duration, err := self.service.Ping(process_id) + if err != nil { + spew.Fdump(w, err) + return + } + encoder := json.NewEncoder(w) + encoder.Encode(map[string]float64{"duration": duration.Seconds()}) } func (self *WebService) HandleListen(w http.ResponseWriter, r *http.Request) { diff --git a/core/ping.go b/core/ping.go new file mode 100644 index 000000000..164453264 --- /dev/null +++ b/core/ping.go @@ -0,0 +1,42 @@ +package core + +import ( + "encoding/gob" + "pilosa/db" + "pilosa/hold" + "time" + + "github.com/nu7hatch/gouuid" +) + +type PingRequest struct { + Id *uuid.UUID + Source *uuid.UUID +} + +type PongRequest struct { + Id *uuid.UUID +} + +func (self PongRequest) ResultId() *uuid.UUID { + return self.Id +} + +func init() { + gob.Register(PingRequest{}) + gob.Register(PongRequest{}) +} + +func (self *Service) Ping(process_id *uuid.UUID) (*time.Duration, error) { + id, _ := uuid.NewV4() + ping := db.Message{Data: PingRequest{Id: id, Source: self.Id}} + start := time.Now() + self.Transport.Send(&ping, process_id) + _, err := hold.Hold.Get(id, 60) + if err != nil { + return nil, err + } + end := time.Now() + dur := end.Sub(start) + return &dur, nil +} diff --git a/db/db.go b/db/db.go index ff7d36279..46f93992d 100644 --- a/db/db.go +++ b/db/db.go @@ -12,17 +12,10 @@ type Message struct { Destination Location } -type PingRequest struct { - Id *uuid.UUID - Source *uuid.UUID -} - -type PongRequest struct { - Id *uuid.UUID +type HoldResult interface { + ResultId() *uuid.UUID } func init() { gob.Register(Message{}) - gob.Register(PingRequest{}) - gob.Register(PongRequest{}) } diff --git a/dispatch/dispatch.go b/dispatch/dispatch.go index 800860312..f79062f4f 100644 --- a/dispatch/dispatch.go +++ b/dispatch/dispatch.go @@ -5,6 +5,8 @@ import ( "pilosa/core" "pilosa/db" "pilosa/hold" + + "github.com/davecgh/go-spew/spew" ) type Dispatch struct { @@ -24,13 +26,15 @@ func (self *Dispatch) Run() { log.Println("Dispatch Run...") for { message := self.service.Transport.Receive() - log.Println("Processing ", message) + spew.Dump("Processing ", message) switch data := message.Data.(type) { - case db.PingRequest: - pong := db.Message{Data: db.PongRequest{Id: data.Id}} + case core.PingRequest: + pong := db.Message{Data: core.PongRequest{Id: data.Id}} self.service.Transport.Send(&pong, data.Source) - case db.PongRequest: - hold.Hold.Set(data.Id, 1, 10) + case db.HoldResult: + hold.Hold.Set(data.ResultId(), data, 30) + default: + log.Println("Unprocessed message", data) } /*