ping

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultCounter  = 4
	DefaultInterval = time.Second
	DefaultTimeout  = time.Second * 5
)

Variables

This section is empty.

Functions

func FormatError

func FormatError(err error) string

func FormatIP

func FormatIP(IP string) (string, error)

FormatIP - trim spaces and format IP.

IP - the provided IP

string - return "" if the input is neither valid IPv4 nor valid IPv6

return IPv4 in format like "192.168.9.1"
return IPv6 in format like "[2002:ac1f:91c5:1::bd59]"

func ParseAddress

func ParseAddress(addr string) (*url.URL, error)

ParseAddress will try to parse addr as url.URL.

func ParseDuration

func ParseDuration(t string) (time.Duration, error)

ParseDuration parse the t as time.Duration, it will parse t as mills when missing unit.

func Register

func Register(p SProtocol, factory Factory)

Types

type Factory

type Factory func(url *url.URL, op *TOption) (IPing, error)

func Load

func Load(p SProtocol) Factory

type IPing

type IPing interface {
	Ping(ctx context.Context) *TStats
	SetTarget(t *TTarget)
}

type SProtocol

type SProtocol int

Protocol ...

const (
	// TCP is tcp protocol
	TCP SProtocol = iota
	// HTTP is http protocol
	HTTP
	// HTTPS is https protocol
	HTTPS
)

func NewProtocol

func NewProtocol(protocol string) (SProtocol, error)

NewProtocol convert protocol string to Protocol

func (SProtocol) String

func (p SProtocol) String() string

type TData

type TData struct {
}

type TOption

type TOption struct {
	Timeout  time.Duration //连接超时
	Resolver *net.Resolver // 自定义DNS域名解析
	Proxy    *url.URL      // Http代理(格式:http://192.168.3.157:32126
	UA       string        // 浏览器UA标识
}

type TPinger

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

func NewPinger

func NewPinger(out io.Writer, url *url.URL, ping IPing, interval time.Duration, counter int) *TPinger

func (*TPinger) Done

func (p *TPinger) Done() <-chan struct{}

func (*TPinger) GetStats

func (p *TPinger) GetStats() ([]TStats, error)

func (*TPinger) Ping

func (p *TPinger) Ping()

func (*TPinger) PingServer

func (p *TPinger) PingServer()

func (*TPinger) SetResult

func (p *TPinger) SetResult(st *TStats)

func (*TPinger) Stop

func (p *TPinger) Stop()

func (*TPinger) Summarize

func (p *TPinger) Summarize()

type TResult

type TResult struct {
	Counter       int           //总数
	CounterOK     int           //成功数
	Target        TTarget       //Ping的目标信息
	Items         []*TData      //Ping的历史记录
	MinDuration   time.Duration //Ping最短时长
	MaxDuration   time.Duration //Ping最长时长
	TotalDuration time.Duration //Ping总时长
}

func (TResult) Avg

func (p TResult) Avg() time.Duration

Avg return the average time of ping

func (TResult) Failed

func (p TResult) Failed() int

Failed return failed counter

func (TResult) String

func (p TResult) String() string

type TStats

type TStats struct {
	Connected   bool                    `json:"connected"`
	Duration    time.Duration           `json:"ping_duration"`
	DNSDuration time.Duration           `json:"dns_Duration"`
	Address     string                  `json:"address"`
	Meta        map[string]fmt.Stringer `json:"meta"`
	Extra       fmt.Stringer            `json:"extra"`
	Error       error                   `json:"error"`
}

func (*TStats) FormatMeta

func (s *TStats) FormatMeta() string

type TTarget

type TTarget struct {
	Protocol SProtocol
	URL      string
	IP       string
	Port     int
	Proxy    string
	Counter  int
	Interval time.Duration
	Timeout  time.Duration
}

func (TTarget) String

func (p TTarget) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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