https://github.com/chenjiandongx/pinger
π A portable ping library written in Go
https://github.com/chenjiandongx/pinger
ping pinger
Last synced: 10 months ago
JSON representation
π A portable ping library written in Go
- Host: GitHub
- URL: https://github.com/chenjiandongx/pinger
- Owner: chenjiandongx
- License: mit
- Created: 2020-05-06T06:10:27.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-27T11:57:59.000Z (about 2 years ago)
- Last Synced: 2025-03-31T07:41:41.881Z (about 1 year ago)
- Topics: ping, pinger
- Language: Go
- Homepage:
- Size: 1.15 MB
- Stars: 58
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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 `ping` here means not only the standard **ICMP Protocol**, but also **TCP/HTTP/HTTPS**. It's the more general sense here as an approach to detect the network quality.
[](https://godoc.org/github.com/chenjiandongx/pinger)
[](https://travis-ci.org/chenjiandongx/pinger)
[](https://ci.appveyor.com/project/chenjiandongx/pinger/branch/master)
[](https://goreportcard.com/report/github.com/chenjiandongx/pinger)
[](https://opensource.org/licenses/MIT)
### π° Installation
```shell
$ go get -u github.com/chenjiandongx/pinger
```
### π Basic Usage
```golang
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**
```golang
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**
```golang
opts := pinger.DefaultICMPPingOpts()
// network is unstable thus we need more ping-ops to evaluate the network quality overall.
opts.PingCount = 20
// set the maximum concurrency, goroutine is cheap, but not free :)
opts.MaxConcurrency = 5
// set per host maximum failed allowed
opts.FailOver = 5
```
**Headers / Method**
```golang
// in http/https case, there are more options could be used.
opts := pinger.DefaultHTTPPingOpts()
// HTTP headers, something special 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 [the documentation](https://godoc.org/github.com/chenjiandongx/pinger).*
### π License
MIT [Β©chenjiandongx](https://github.com/chenjiandongx)