File: status.go

package info (click to toggle)
golang-github-xiang90-probing 0.0~git20150806.0.6a0cc1a-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68 kB
  • ctags: 34
  • sloc: makefile: 3
file content (96 lines) | stat: -rw-r--r-- 1,510 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package probing

import (
	"sync"
	"time"
)

var (
	// weight factor
	α = 0.125
)

type Status interface {
	Total() int64
	Loss() int64
	Health() bool
	// Estimated smoothed round trip time
	SRTT() time.Duration
	// Estimated clock difference
	ClockDiff() time.Duration
	StopNotify() <-chan struct{}
}

type status struct {
	mu        sync.Mutex
	srtt      time.Duration
	total     int64
	loss      int64
	health    bool
	clockdiff time.Duration
	stopC     chan struct{}
}

// SRTT = (1-α) * SRTT + α * RTT
func (s *status) SRTT() time.Duration {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.srtt
}

func (s *status) Total() int64 {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.total
}

func (s *status) Loss() int64 {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.loss
}

func (s *status) Health() bool {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.health
}

func (s *status) ClockDiff() time.Duration {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.clockdiff
}

func (s *status) StopNotify() <-chan struct{} {
	return s.stopC
}

func (s *status) record(rtt time.Duration, when time.Time) {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.total += 1
	s.health = true
	s.srtt = time.Duration((1-α)*float64(s.srtt) + α*float64(rtt))
	s.clockdiff = time.Now().Sub(when) - s.srtt/2
}

func (s *status) recordFailure() {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.total++
	s.health = false
	s.loss += 1
}

func (s *status) reset() {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.srtt = 0
	s.total = 0
	s.health = false
	s.clockdiff = 0
}