Example #1
0
func NewFaultDetector(client protocols.Client, server string) (*FaultDetector, error) {
	pingClient, err := NewClient(client)
	if err != nil {
		return nil, err
	}
	err = pingClient.SetServer(server)
	if err != nil {
		_ = pingClient.Close()
		return nil, err
	}
	return &FaultDetector{
		FaultDetectorBase: protocols.NewFaultDetectorBase(client.Protocol(), pingClient.Server()),
		client:            pingClient,
	}, nil
}
Example #2
0
func (server *HeartbeatServer) ObserveServer(endpoint string, heartbeatFrequency time.Duration, acceptableTimeout time.Duration) (protocols.FaultDetector, error) {
	client, err := NewClientFor(endpoint)
	if err != nil {
		return nil, err
	}
	var token int64
	for {
		token = tokenRand.Int63()
		if _, ok := server.detectors[token]; !ok && token != 0 {
			break
		}
	}
	detector := &HeartbeatFaultDetector{
		FaultDetectorBase:  protocols.NewFaultDetectorBase(server.Protocol(), client.Server()),
		client:             client,
		acceptableTimeout:  acceptableTimeout,
		heartbeatFrequency: heartbeatFrequency,
		server:             server,
		token:              token,
		lastHeartbeatReceived: time.Now(),
	}
	server.detectors[token] = detector
	return detector, nil
}