heartbeat

package
v6.3.5 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2021 License: Apache-2.0 Imports: 6 Imported by: 6

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/erpc/v6/plugin/heartbeat"

Test
package heartbeat_test

import (
	"testing"
	"time"

	"github.com/henrylee2cn/erpc/v6"
	"github.com/henrylee2cn/erpc/v6/plugin/heartbeat"
)

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

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

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

	cli := erpc.NewPeer(
		erpc.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 := erpc.NewPeer(
		erpc.PeerConfig{ListenPort: 9090, PrintDetail: true},
		heartbeat.NewPing(3, false),
	)
	go srv.ListenAndServe()
	time.Sleep(time.Second)

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

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

	cli := erpc.NewPeer(
		erpc.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 erpc.EarlyPeer) error
	// PostDial initializes heartbeat information.
	PostDial(sess erpc.PreSession, isRedial bool) *erpc.Status
	// PostAccept initializes heartbeat information.
	PostAccept(sess erpc.PreSession) *erpc.Status
	// PostWriteCall updates heartbeat information.
	PostWriteCall(ctx erpc.WriteCtx) *erpc.Status
	// PostWritePush updates heartbeat information.
	PostWritePush(ctx erpc.WriteCtx) *erpc.Status
	// PostReadCallHeader updates heartbeat information.
	PostReadCallHeader(ctx erpc.ReadCtx) *erpc.Status
	// PostReadPushHeader updates heartbeat information.
	PostReadPushHeader(ctx erpc.ReadCtx) *erpc.Status
}

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 erpc.EarlyPeer) error
	// PostWriteCall updates heartbeat information.
	PostWriteCall(ctx erpc.WriteCtx) *erpc.Status
	// PostWritePush updates heartbeat information.
	PostWritePush(ctx erpc.WriteCtx) *erpc.Status
	// PostReadCallHeader updates heartbeat information.
	PostReadCallHeader(ctx erpc.ReadCtx) *erpc.Status
	// PostReadPushHeader updates heartbeat information.
	PostReadPushHeader(ctx erpc.ReadCtx) *erpc.Status
}

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