Skip to content

protocol-diver/pingu

Repository files navigation

PINGU GoDoc

This is a simple and tiny heartbeat library with UDP.
Heartbeat is a message that checks if you are healthy or if other connected peers are healthy. The core feature is to periodically request a heartbeat and send an 'alive' message when a heartbeat request is received. Also need to surface a method that allows the user to check the health status.
It could make it your building easier when you need build a server with heartbeat.
If you need heartbeat communication with an external network among peers, register the public IP obtained through a process such as hole punching(This library does not support communication across NAT).

Install

$ go get -u github.com/protocol-diver/pingu

Usage

Create your Pingu

myPingu, err := pingu.NewPingu("127.0.0.1:4874", nil)

// You could preconfig like below,
pingu.Config{
  RecvBufferSize: 512, // default value : 256
  Verbose: true // It's notify that what's going on, default value : false
}

Embed into your Server

type Server struct {
  conn  *net.TCPConn
  pingu *pingu.Pingu
}

Register other Pingus

if err := myPingu.RegisterWithRawAddr("127.0.0.1:4875"); err != nil {
  return err
}
if err := myPingu.RegisterWithRawAddr("127.0.0.1:4876"); err != nil {
  return err
}

Let work Pingu

myPingu.Start()

ticker := time.Ticker(5 * time.Second)
// Second param is time limit of send ping -> recv pong.
cancel, err := myPingu.BroadcastPingWithTicker(*ticker, 3*time.Second)
if err != nil {
  return err
}

Watch Pingu Working

// It's returns map[string]bool.
// Mapping string ip:port to health status.
fmt.Println(myPingu.PingTable())

table := myPingu.PingTable()
// true: target's Pingu is healthy
// false: target's Pingu is unhealthy
fmt.Println(table["127.0.0.1:8552"])

Controll the Pingu

myPingu.Stop()
// Continue previous BroadcastPingWithTicker if exist.
myPingu.Start()

// If you want stop the BroadcastPingWithTicker, close the 'cancel'.
cancel, _ := myPingu.BroadcastPingWithTicker(*ticker, 3*time.Second)
close(cancel)