-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatency.go
More file actions
91 lines (77 loc) · 2.46 KB
/
latency.go
File metadata and controls
91 lines (77 loc) · 2.46 KB
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
package l2
import (
"time"
)
// This is a wrapper that can take a device and limit the bandwidth that can
// go through it.
type deviceWithLatency struct {
dev FrameReadWriteCloser
// Maximum number of outgoing bytes per second.
send_bandwidth int
// Maximum number of incoming bytes per second.
receive_bandwidth int
// How much data we wrote the last time we called WriteFrame().
last_write_size int
// How much data we read the last time we called ReadFrame().
last_read_size int
}
// Basically initializes latency and then delegates to the tapDevice version of
// NewTapDevice. For bandwidth parameters, 0 means no limitation.
// Args:
// mac: MAC address of the device. If it is an empty string, no specific
// address will be set.
// dev: A name for the device.
// send_bandwidth: Maximum number of outgoing bytes per second.
// receive_bandwidth: Maximum number of incoming bytes per second.
// Returns:
// The new tap device, error.
func NewDeviceWithLatency(dev FrameReadWriteCloser, send_bandwidth,
receive_bandwidth int) FrameReadWriteCloser {
d := deviceWithLatency{
dev: dev,
send_bandwidth: send_bandwidth,
receive_bandwidth: receive_bandwidth,
}
return &d
}
// Reads a frame from the tap device.
// Returns:
// The frame read, error.
func (t *deviceWithLatency) ReadFrame() (EthFrame, error) {
start_time := time.Now()
frame, err := t.dev.ReadFrame()
if err != nil {
return nil, err
}
if t.receive_bandwidth == 0 {
// No limitation.
return frame, nil
}
// Compute how long it took us.
end_time := time.Now()
elapsed := end_time.Sub(start_time)
// Target latency based on our bandwidth. (This is the same formula OpenVPN
// uses, apparently.)
target_latency := float32(t.last_read_size) / float32(t.receive_bandwidth)
t.last_read_size = len(frame)
// We want to have the exact latency, so wait the rest of the time.
to_wait := target_latency*float32(time.Second) - float32(elapsed)
time.Sleep(time.Duration(to_wait))
return frame, nil
}
// Writes a frame to the tap device.
// Returns:
// Error.
func (t *deviceWithLatency) WriteFrame(data EthFrame) error {
if t.send_bandwidth != 0 {
// Wait the requisite latency.
target_latency := float32(t.last_write_size) / float32(t.send_bandwidth)
t.last_write_size = len(data)
time.Sleep(time.Duration(target_latency * float32(time.Second)))
}
return t.dev.WriteFrame(data)
}
// Closes the underlying device.
func (t *deviceWithLatency) Close() error {
return t.dev.Close()
}