-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.go
46 lines (37 loc) · 1.06 KB
/
ws.go
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
package main
import (
"math/rand"
"time"
)
type Args struct {
Batch int
}
type WeatherStation struct {
id int
averagePeriod float64
testName string
}
// RPC handler that simulates a weather station
//
// Returns a WeatherReport with a temperature Value after a randomized delay,
// with a small chance of hanging indefinitely.
func (t *WeatherStation) GetWeatherRPCHandler(args *Args, reply *WeatherReport) error {
// There is a 1/10 chance that the weather station does not respond at all.
random := rand.Intn(10)
if random == 6 {
select {}
}
before := rand.Intn(2)
margin := 0.15
var delay time.Duration
delay = time.Duration(rand.Float64() * (t.averagePeriod - margin) * float64(time.Second.Nanoseconds()))
if before == 1 {
// We're good, we've already delayed enough.
} else {
delay += time.Duration((t.averagePeriod + (2 * margin)) * float64(time.Second.Nanoseconds()))
}
time.Sleep(delay)
value := float64(rand.Intn(100))
*reply = WeatherReport{value, t.id, args.Batch}
return nil
}