heartbeat

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

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/sqos/yrpc/plugin/heartbeat"

Test
package heartbeat_test

import (
	"testing"
	"time"

	"github.com/sqos/yrpc"
	"github.com/sqos/yrpc/plugin/heartbeat"
)

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

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

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

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

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

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

	cli := yrpc.NewPeer(
		yrpc.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-2023 HenryLee. All Rights Reserved. Copyright 2024 sqos. 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 yrpc.EarlyPeer) error
	// PostDial initializes heartbeat information.
	PostDial(sess yrpc.PreSession, isRedial bool) *yrpc.Status
	// PostAccept initializes heartbeat information.
	PostAccept(sess yrpc.PreSession) *yrpc.Status
	// PostWriteCall updates heartbeat information.
	PostWriteCall(ctx yrpc.WriteCtx) *yrpc.Status
	// PostWritePush updates heartbeat information.
	PostWritePush(ctx yrpc.WriteCtx) *yrpc.Status
	// PostReadCallHeader updates heartbeat information.
	PostReadCallHeader(ctx yrpc.ReadCtx) *yrpc.Status
	// PostReadPushHeader updates heartbeat information.
	PostReadPushHeader(ctx yrpc.ReadCtx) *yrpc.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 yrpc.EarlyPeer) error
	// PostWriteCall updates heartbeat information.
	PostWriteCall(ctx yrpc.WriteCtx) *yrpc.Status
	// PostWritePush updates heartbeat information.
	PostWritePush(ctx yrpc.WriteCtx) *yrpc.Status
	// PostReadCallHeader updates heartbeat information.
	PostReadCallHeader(ctx yrpc.ReadCtx) *yrpc.Status
	// PostReadPushHeader updates heartbeat information.
	PostReadPushHeader(ctx yrpc.ReadCtx) *yrpc.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