heartbeat

package
v5.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2018 License: Apache-2.0 Imports: 6 Imported by: 14

README

heartbeat

A generic timing heartbeat plugin.

During a heartbeat, if there is no communication, send a heartbeat message; When the connection is idle more than 3 times the heartbeat time, take the initiative to disconnect.

Usage

import "github.com/henrylee2cn/teleport/plugin/heartbeat"

Test
package heartbeat_test

import (
	"testing"
	"time"

	tp "github.com/henrylee2cn/teleport"
	"github.com/henrylee2cn/teleport/plugin/heartbeat"
)

func TestHeartbeatCall1(t *testing.T) {
	srv := tp.NewPeer(
		tp.PeerConfig{ListenPort: 9090, PrintDetail: true},
		heartbeat.NewPong(),
	)
	go srv.ListenAndServe()
	time.Sleep(time.Second)

	cli := tp.NewPeer(
		tp.PeerConfig{PrintDetail: true},
		heartbeat.NewPing(3, true),
	)
	cli.Dial(":9090")
	time.Sleep(time.Second * 10)
}

func TestHeartbeatCall2(t *testing.T) {
	srv := tp.NewPeer(
		tp.PeerConfig{ListenPort: 9090, PrintDetail: true},
		heartbeat.NewPong(),
	)
	go srv.ListenAndServe()
	time.Sleep(time.Second)

	cli := tp.NewPeer(
		tp.PeerConfig{PrintDetail: true},
		heartbeat.NewPing(3, true),
	)
	sess, _ := cli.Dial(":9090")
	for i := 0; i < 8; i++ {
		sess.Call("/", nil, nil)
		time.Sleep(time.Second)
	}
	time.Sleep(time.Second * 5)
}

func TestHeartbeatPush1(t *testing.T) {
	srv := tp.NewPeer(
		tp.PeerConfig{ListenPort: 9090, PrintDetail: true},
		heartbeat.NewPing(3, false),
	)
	go srv.ListenAndServe()
	time.Sleep(time.Second)

	cli := tp.NewPeer(
		tp.PeerConfig{PrintDetail: true},
		heartbeat.NewPong(),
	)
	cli.Dial(":9090")
	time.Sleep(time.Second * 10)
}

func TestHeartbeatPush2(t *testing.T) {
	srv := tp.NewPeer(
		tp.PeerConfig{ListenPort: 9090, PrintDetail: true},
		heartbeat.NewPing(3, false),
	)
	go srv.ListenAndServe()
	time.Sleep(time.Second)

	cli := tp.NewPeer(
		tp.PeerConfig{PrintDetail: true},
		heartbeat.NewPong(),
	)
	sess, _ := cli.Dial(":9090")
	for i := 0; i < 8; i++ {
		sess.Push("/", nil)
		time.Sleep(time.Second)
	}
	time.Sleep(time.Second * 5)
}

test command:

go test -v -run=TestHeartbeatCall1
go test -v -run=TestHeartbeatCall2
go test -v -run=TestHeartbeatPush1
go test -v -run=TestHeartbeatPush2

Documentation

Overview

Heartbeat is a generic timing heartbeat plugin.

Copyright 2018 HenryLee. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	// HeartbeatServiceMethod heartbeat service method
	HeartbeatServiceMethod = "/heartbeat"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Ping

type Ping interface {
	// SetRate sets heartbeat rate.
	SetRate(rateSecond int)
	// UseCall uses CALL method to ping.
	UseCall()
	// UsePush uses PUSH method to ping.
	UsePush()
	// Name returns name.
	Name() string
	// PostNewPeer runs ping woker.
	PostNewPeer(peer tp.EarlyPeer) error
	// PostDial initializes heartbeat information.
	PostDial(sess tp.PreSession) *tp.Rerror
	// PostAccept initializes heartbeat information.
	PostAccept(sess tp.PreSession) *tp.Rerror
	// PostWriteCall updates heartbeat information.
	PostWriteCall(ctx tp.WriteCtx) *tp.Rerror
	// PostWritePush updates heartbeat information.
	PostWritePush(ctx tp.WriteCtx) *tp.Rerror
	// PostReadCallHeader updates heartbeat information.
	PostReadCallHeader(ctx tp.ReadCtx) *tp.Rerror
	// PostReadPushHeader updates heartbeat information.
	PostReadPushHeader(ctx tp.ReadCtx) *tp.Rerror
}

Ping send heartbeat.

func NewPing

func NewPing(rateSecond int, useCall bool) Ping

NewPing returns a heartbeat(CALL or PUSH) sender plugin.

type Pong

type Pong interface {
	// Name returns name.
	Name() string
	// PostNewPeer runs ping woker.
	PostNewPeer(peer tp.EarlyPeer) error
	// PostWriteCall updates heartbeat information.
	PostWriteCall(ctx tp.WriteCtx) *tp.Rerror
	// PostWritePush updates heartbeat information.
	PostWritePush(ctx tp.WriteCtx) *tp.Rerror
	// PostReadCallHeader updates heartbeat information.
	PostReadCallHeader(ctx tp.ReadCtx) *tp.Rerror
	// PostReadPushHeader updates heartbeat information.
	PostReadPushHeader(ctx tp.ReadCtx) *tp.Rerror
}

Pong receive heartbeat.

func NewPong

func NewPong() Pong

NewPong returns a heartbeat receiver plugin.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL