featurebase/core/ping.go
Ben Johnson 2f1e3b6078 Refactor config & service
This commit refactors the config into a `main.Config` object instead
of a global singleton. The `core.Service` is also refactored into
the `main` package and individual pieces of the service are wired
together by the `pilosa` binary.

These two changes are required to begin to decouple packages from
one another and allow them to be individually unit tested. Previously
most top-level objects in the system could access any other top-level
object through the `core.Service` which effectively made `Service` a
singleton in the system. Each top-level object now has inline interfaces
for their dependencies so that can be set at runtime by the `main`
package or can be mocked by a test package.
2015-08-12 15:34:23 -06:00

62 lines
1 KiB
Go

package core
import (
"encoding/gob"
"time"
"github.com/umbel/pilosa/db"
"github.com/umbel/pilosa/util"
)
type PingRequest struct {
Id *util.GUID
Source *util.GUID
}
type PongRequest struct {
Id *util.GUID
}
func (self PongRequest) ResultId() *util.GUID {
return self.Id
}
func (self PongRequest) ResultData() interface{} {
return self.Id
}
func init() {
gob.Register(PingRequest{})
gob.Register(PongRequest{})
}
type Pinger struct {
ID util.GUID
Hold interface {
Get(id *util.GUID, timeout int) (interface{}, error)
}
Transport interface {
Send(message *db.Message, host *util.GUID)
}
}
func NewPinger(id util.GUID) *Pinger {
return &Pinger{
ID: id,
}
}
func (self *Pinger) Ping(process_id *util.GUID) (*time.Duration, error) {
id := util.RandomUUID()
ping := db.Message{Data: PingRequest{Id: &id, Source: &self.ID}}
start := time.Now()
self.Transport.Send(&ping, process_id)
_, err := self.Hold.Get(&id, 60)
if err != nil {
return nil, err
}
end := time.Now()
dur := end.Sub(start)
return &dur, nil
}