mirror of
https://github.com/go-micro/go-micro.git
synced 2025-07-12 22:41:07 +02:00
add rate measure
This commit is contained in:
@ -32,9 +32,12 @@ type link struct {
|
||||
// channels keeps a mapping of channels and last seen
|
||||
channels map[string]time.Time
|
||||
|
||||
// the weighed moving average roundtrip
|
||||
// the weighted moving average roundtrip
|
||||
rtt int64
|
||||
|
||||
// weighted moving average of bits flowing
|
||||
rate float64
|
||||
|
||||
// keep an error count on the link
|
||||
errCount int
|
||||
}
|
||||
@ -55,7 +58,7 @@ func (l *link) setRTT(d time.Duration) {
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
if l.rtt < 0 {
|
||||
if l.rtt <= 0 {
|
||||
l.rtt = d.Nanoseconds()
|
||||
return
|
||||
}
|
||||
@ -110,7 +113,9 @@ func (l *link) Delay() int64 {
|
||||
|
||||
// Current transfer rate as bits per second (lower is better)
|
||||
func (l *link) Rate() float64 {
|
||||
return float64(10e8)
|
||||
l.RLock()
|
||||
defer l.RUnlock()
|
||||
return l.rate
|
||||
}
|
||||
|
||||
// Length returns the roundtrip time as nanoseconds (lower is better).
|
||||
@ -141,11 +146,43 @@ func (l *link) Close() error {
|
||||
}
|
||||
|
||||
func (l *link) Send(m *transport.Message) error {
|
||||
dataSent := len(m.Body)
|
||||
|
||||
// set header length
|
||||
for k, v := range m.Header {
|
||||
dataSent += (len(k) + len(v))
|
||||
}
|
||||
|
||||
// get time now
|
||||
now := time.Now()
|
||||
|
||||
// send the message
|
||||
err := l.Socket.Send(m)
|
||||
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
// calculate based on data
|
||||
if dataSent > 0 {
|
||||
// measure time taken
|
||||
delta := time.Since(now)
|
||||
|
||||
// bit sent
|
||||
bits := dataSent * 1024
|
||||
|
||||
// rate of send in bits per nanosecond
|
||||
rate := float64(bits) / float64(delta.Nanoseconds())
|
||||
|
||||
//
|
||||
if l.rate == 0 {
|
||||
// rate per second
|
||||
l.rate = rate * 1e9
|
||||
} else {
|
||||
// set new rate per second
|
||||
l.rate = 0.8*l.rate + 0.2*(rate*1e9)
|
||||
}
|
||||
}
|
||||
|
||||
// if theres no error reset the counter
|
||||
if err == nil {
|
||||
l.errCount = 0
|
||||
|
Reference in New Issue
Block a user