mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
Use HoldResult interface to route responses from dispatch. Added standalone ping function.
This commit is contained in:
parent
11d964339c
commit
0a50f18eca
4 changed files with 60 additions and 20 deletions
13
core/http.go
13
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) {
|
||||
|
|
|
|||
42
core/ping.go
Normal file
42
core/ping.go
Normal file
|
|
@ -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
|
||||
}
|
||||
11
db/db.go
11
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{})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue