Skip to content

Commit

Permalink
change ping lib
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoyuepan committed Dec 8, 2020
1 parent 7de25a5 commit 75d742f
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -8,7 +8,7 @@ env:

before_script:
- rm -f go.sum
- go get -tags "quic kcp ping utp" -v github.com/smallnest/rpcx/...
- go get -tags "quic kcp" -v github.com/smallnest/rpcx/...
- go get github.com/mattn/goveralls

script:
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Expand Up @@ -12,7 +12,7 @@ tools:
go get github.com/matm/gocov-html

golangci-lint:
golangci-lint run -D errcheck --build-tags 'quic kcp ping'
golangci-lint run -D errcheck --build-tags 'quic kcp'

lint:
golint ./...
Expand All @@ -30,13 +30,13 @@ build:
go build ./...

build-all:
go build -tags "kcp quic ping" ./...
go build -tags "kcp quic" ./...

test:
go test -race -tags "kcp quic ping" ./...
go test -race -tags "kcp quic" ./...

cover:
gocov test -tags "kcp quic ping" ./... | gocov-html > cover.html
gocov test -tags "kcp quic" ./... | gocov-html > cover.html
open cover.html

check-libs:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -40,10 +40,10 @@ install the basic features:
`go get -v github.com/smallnest/rpcx/...`


If you want to use `ping`ใ€`quic`ใ€`kcp`ใ€`utp` registry, use those tags to `go get` ใ€ `go build` or `go run`. For example, if you want to use all features, you can:
If you want to use `quic`ใ€`kcp` registry, use those tags to `go get` ใ€ `go build` or `go run`. For example, if you want to use all features, you can:

```sh
go get -v -tags "quic kcp ping utp" github.com/smallnest/rpcx/...
go get -v -tags "quic kcp" github.com/smallnest/rpcx/...
```

**_tags_**:
Expand Down
20 changes: 0 additions & 20 deletions client/ping_excluded.go

This file was deleted.

23 changes: 6 additions & 17 deletions client/ping_utils.go
@@ -1,14 +1,11 @@
// +build ping

package client

import (
"context"
"net"
"strings"
"time"

fastping "github.com/tatsushid/go-fastping"
ping "github.com/go-ping/ping"
)

func newWeightedICMPSelector(servers map[string]string) Selector {
Expand Down Expand Up @@ -52,21 +49,13 @@ func createICMPWeighted(servers map[string]string) []*Weighted {
func Ping(host string) (rtt int, err error) {
rtt = 1000 //default and timeout is 1000 ms

p := fastping.NewPinger()
p.Network("udp")
ra, err := net.ResolveIPAddr("ip4:icmp", host)
pinger, err := ping.NewPinger(host)
if err != nil {
return 0, err
}
p.AddIPAddr(ra)

p.OnRecv = func(addr *net.IPAddr, r time.Duration) {
rtt = int(r.Nanoseconds() / 1000000)
return rtt, err
}
// p.OnIdle = func() {

// }
err = p.Run()
pinger.Count = 3
stats := pinger.Statistics()
rtt = int(stats.AvgRtt)

return rtt, err
}
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Expand Up @@ -5,17 +5,16 @@ go 1.14
require (
github.com/ChimeraCoder/gojson v1.1.0
github.com/aliyun/alibaba-cloud-sdk-go v1.61.69 // indirect
github.com/anacrolix/envpprof v1.1.0 // indirect
github.com/anacrolix/sync v0.2.0 // indirect
github.com/anacrolix/utp v0.0.0-20180219060659-9e0e1d1d0572
github.com/apache/thrift v0.13.0
github.com/cenk/backoff v2.0.0+incompatible // indirect
github.com/edwingeng/doublejump v0.0.0-20200219153503-7cfc0ed6e836
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/fatih/color v1.9.0
github.com/go-ping/ping v0.0.0-20201115131931-3300c582a663
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.4.3
github.com/golang/snappy v0.0.2
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/golang-lru v0.5.1
Expand All @@ -28,7 +27,6 @@ require (
github.com/klauspost/reedsolomon v1.9.9 // indirect
github.com/kr/pretty v0.2.0
github.com/lucas-clemente/quic-go v0.18.0
github.com/matm/gocov-html v0.0.0-20200509184451-71874e2e203b // indirect
github.com/mmcloughlin/avo v0.0.0-20201105074841-5d2f697d268f // indirect
github.com/nacos-group/nacos-sdk-go v1.0.1
github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e
Expand All @@ -41,7 +39,6 @@ require (
github.com/soheilhy/cmux v0.1.4
github.com/stretchr/testify v1.6.1
github.com/syndtr/goleveldb v1.0.0
github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e
github.com/valyala/fastrand v0.0.0-20170531153657-19dd0f0bf014
github.com/vmihailenco/msgpack v4.0.0+incompatible
github.com/xtaci/kcp-go v2.0.3+incompatible
Expand Down

0 comments on commit 75d742f

Please sign in to comment.