heartbeat

package module
v0.0.0-...-a41af84 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 10 Imported by: 0

README

go-heartbeat

Simple server/client library to monitor connectivity via heartbeats using gRPC client-side streaming.
The server provides Peer events with Id and Addr via channels Alive and Dead.
The client automatically (re)connects and sends heartbeats with the user-provided Id with Interval frequency.

Example Server
package main

import (
	"fmt"

	"github.com/franklange/go-heartbeat"
)

func main() {
	s := heartbeat.NewHeartbeatServer("9000")
	defer s.Stop()

	for {
		select {
		case c := <-s.Alive:
			fmt.Printf("[conn] id: %s addr: %s\n", c.Id, c.Addr)
		case c := <-s.Dead:
			fmt.Printf("[dead] id: %s addr: %s\n", c.Id, c.Addr)
		}
	}
}

Example Client
package main

import (
	"time"

	hb "github.com/franklange/go-heartbeat"
)

func main() {
	c := hb.NewClient(&hb.ClientConfig{
		Id:       "c1",
		Addr:     "localhost:9000",
		Interval: 1 * time.Second,
	})
	defer c.Stop()

	for {
		time.Sleep(time.Second)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(config *ClientConfig) *Client

func (*Client) Stop

func (c *Client) Stop()

type ClientConfig

type ClientConfig struct {
	Id       string
	Addr     string
	Interval time.Duration
}

type Peer

type Peer struct {
	Id   string
	Addr string
}

type Server

type Server struct {
	proto.UnimplementedHeartbeatServer

	Dead  chan Peer
	Alive chan Peer
	// contains filtered or unexported fields
}

func NewHeartbeatServer

func NewHeartbeatServer(port string) *Server

func (*Server) Connect

func (s *Server) Connect(stream proto.Heartbeat_ConnectServer) error

func (*Server) Stop

func (s *Server) Stop()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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