pinger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2020 License: MIT Imports: 12 Imported by: 2

README

Pinger

📌 A portable ping library written in Go.

Pinger is a library used to evaluate the quality of services in ICMP/TCP/HTTP protocol.

What's worth pointing out is that the ping here is not only represents the standard IMCP Protocol, but also the TCP/HTTP/HTTPS. It's the more general sense here as an approach to detecte the network qualtily.

GoDoc Travis Appveyor Go Report Card License

🔰 Installation
$ go get -u github.com/chenjiandongx/pinger
📝 Basic Usage
package main

import (
	"fmt"

	"github.com/chenjiandongx/pinger"
)

func main() {
	// ICMP
	stats, err := pinger.ICMPPing(nil, "huya.com", "youtube.com", "114.114.114.114")

	// TCP
	// stats, err := pinger.TCPPing(nil, "huya.com:80", "google.com:80", "huya.com:443", "google.com:443")

	// HTTP/HTTPS
	// stats, err := pinger.HTTPPing(nil, "http://huya.com", "https://google.com", "http://39.156.69.79")
	if err != nil {
		panic(err)
	}

	for _, s := range stats {
		fmt.Printf("%+v\n", s)
	}
}

// output
{Host:huya.com PktSent:10 PktLossRate:0 Mean:36.217604ms Last:37.636144ms Best:34.949608ms Worst:37.636144ms}
{Host:youtube.com PktSent:10 PktLossRate:0 Mean:12.853867ms Last:13.105932ms Best:11.836598ms Worst:14.844776ms}
{Host:114.114.114.114 PktSent:10 PktLossRate:0 Mean:7.689701ms Last:7.711122ms Best:6.252377ms Worst:9.427482ms}
🎉 Options

PingTimeout / Interval

opts := pinger.DefaultICMPPingOpts
opts.PingTimeout = 50 * time.Millisecond
// sleep 60 mills after a request every time.
opts.Interval = func() time.Duration { return 60 * time.Millisecond }

PingCount / MaxConcurrency / FailOver

opts := pinger.DefaultICMPPingOpts
// network is unstable thus we need more ping-ops to evaluate the network quality overall.
opts.PingCount = 20
// set the maximum concurreny, goroutine is cheap, but not free :)
opts.MaxConcurrency = 5
// set per host maximum failed allowed
opts.FailOver = 5

Headers / Method

// in http/https case, there are more options could be used.
opts := pinger.DefaultHTTPPingOpts
// HTTP headers, something speical for authentication or anything else.
opts.Headers = map[string]string{"token": "my-token", "who": "me"}
// HTTP Method, feel free to use any standard HTTP methods you need.
opts.Method = http.MethodPost

For more information, please refer to godoc.

📃 License

MIT ©chenjiandongx

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultHTTPPingOpts = &HTTPPingOpts{
	PingTimeout:    3 * time.Second,
	PingCount:      10,
	Method:         http.MethodGet,
	Body:           nil,
	Headers:        nil,
	Interval:       func() time.Duration { return time.Duration(rand.Int63n(200)) * time.Millisecond },
	MaxConcurrency: 10,
	FailOver:       5,
}

DefaultHTTPPingOpts will be used if PingOpts is nil with the HTTPPing function.

View Source
var DefaultICMPPingOpts = &ICMPPingOpts{
	PingTimeout:     3 * time.Second,
	PingCount:       10,
	MaxConcurrency:  10,
	FailOver:        5,
	Interval:        func() time.Duration { return time.Duration(rand.Int63n(200)) * time.Millisecond },
	Bind4:           "0.0.0.0",
	ResolverTimeout: 1500 * time.Millisecond,
	PayloadSize:     56,
}

DefaultICMPPingOpts will be used if PingOpts is nil with the ICMPPing function.

View Source
var DefaultTCPPingOpts = &TCPPingOpts{
	PingTimeout:    3 * time.Second,
	PingCount:      10,
	Interval:       func() time.Duration { return time.Duration(rand.Int63n(200)) * time.Millisecond },
	MaxConcurrency: 10,
	FailOver:       5,
}

DefaultTCPPingOpts will be used if PingOpts is nil with the TCPPing function.

Functions

This section is empty.

Types

type HTTPPingOpts

type HTTPPingOpts struct {
	// PingTimeout is the timeout for a ping request.
	PingTimeout time.Duration
	// PingCount is the number of requests that will be sent to compute the ping quality of a host.
	PingCount int
	// MaxConcurrency sets the maximum goroutine used.
	MaxConcurrency int
	// FailOver is the per host maximum failed allowed.
	FailOver int
	// Interval returns a time.Duration as the delay.
	Interval func() time.Duration
	// Method represents the HTTP Method(GET/POST/PUT/...).
	Method string
	// Body represents the HTTP Request body.
	Body io.Reader
	// Headers represents for the HTTP Headers.
	Headers map[string]string
}

HTTPPingOpts is the option set for the HTTP Ping.

type ICMPPingOpts

type ICMPPingOpts struct {
	// PingTimeout is the timeout for a ping request.
	PingTimeout time.Duration
	// PingCount counting requests for calculating ping quality of host.
	PingCount int
	// MaxConcurrency sets the maximum goroutine used.
	MaxConcurrency int
	// FailOver is the per host maximum failed allowed.
	FailOver int
	// Interval returns a time.Duration as the delay.
	Interval func() time.Duration
	// ResolverTimeout is the timeout for the net.ResolveIPAddr request.
	ResolverTimeout time.Duration
	// Bind4 is the ipv4 bind to start a raw socket.
	Bind4 string
	// PayloadSize represents the request body size for a ping request.
	PayloadSize uint16
}

ICMPPingOpts is the option set for the ICMP Ping.

type PingStat

type PingStat struct {
	Host        string
	PktSent     int
	PktLossRate float64
	Mean        time.Duration
	Last        time.Duration
	Best        time.Duration
	Worst       time.Duration
}

PingStat struct is used to record the ping result.

func HTTPPing

func HTTPPing(opts *HTTPPingOpts, hosts ...string) ([]PingStat, error)

func ICMPPing

func ICMPPing(opts *ICMPPingOpts, hosts ...string) ([]PingStat, error)

func TCPPing

func TCPPing(opts *TCPPingOpts, hosts ...string) ([]PingStat, error)

type TCPPingOpts

type TCPPingOpts struct {
	// PingTimeout is the timeout for a ping request.
	PingTimeout time.Duration
	// PingCount counting requests for calculating ping quality of host.
	PingCount int
	// MaxConcurrency sets the maximum goroutine used.
	MaxConcurrency int
	// FailOver is the per host maximum failed allowed.
	FailOver int
	// Interval returns a time.Duration as the delay.
	Interval func() time.Duration
}

TCPPingOpts is the option set for the TCP Ping.

Jump to

Keyboard shortcuts

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