|
| 1 | +package registration |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/serverledge-faas/serverledge/internal/config" |
| 10 | +) |
| 11 | + |
| 12 | +func InitCloudMonitoring() { |
| 13 | + Reg.CloudLatency = make(map[string]float64) |
| 14 | + //complete monitoring phase at startup |
| 15 | + monitorCloudLatency() |
| 16 | + go runCloudMonitor() |
| 17 | +} |
| 18 | + |
| 19 | +func runCloudMonitor() { |
| 20 | + cloudMonitorTicker := time.NewTicker(time.Duration(config.GetInt(config.REG_CLOUD_MONITORING_INTERVAL, 60)) * time.Second) |
| 21 | + for { |
| 22 | + select { |
| 23 | + case <-cloudMonitorTicker.C: |
| 24 | + monitorCloudLatency() |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func tcpLatency(hostAndPort string) (float64, error) { |
| 30 | + start := time.Now() |
| 31 | + conn, err := net.DialTimeout("tcp", hostAndPort, 3*time.Second) |
| 32 | + if err != nil { |
| 33 | + return 0, err |
| 34 | + } |
| 35 | + _ = conn.Close() |
| 36 | + return float64(time.Since(start).Milliseconds()), nil |
| 37 | +} |
| 38 | + |
| 39 | +func monitorCloudLatency() { |
| 40 | + region := config.GetString(config.REGISTRY_AREA, "ROME") |
| 41 | + cloudNodes, err := GetCloudNodes(region) |
| 42 | + if err != nil { |
| 43 | + log.Println(fmt.Printf("Error while retrieving cloud nodes: %v", err)) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + if len(cloudNodes) == 0 { |
| 48 | + cloudAddress := config.GetString(config.CLOUD_URL, "") |
| 49 | + if cloudAddress != "" { |
| 50 | + cloudNodes["from-config"] = cloudAddress |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + for _, url := range cloudNodes { |
| 55 | + // TODO: retrieve from offloadingLatency? |
| 56 | + latency, latencyError := tcpLatency(url) |
| 57 | + if latencyError != nil { |
| 58 | + log.Printf("Unable to compute latency towards %v: %v\n", url, latencyError) |
| 59 | + delete(Reg.CloudLatency, url) |
| 60 | + continue |
| 61 | + } |
| 62 | + Reg.CloudLatency[url] = latency |
| 63 | + } |
| 64 | + |
| 65 | + log.Printf("Map of cloud node latency at the end of monitoring: %v\n", Reg.CloudLatency) |
| 66 | +} |
0 commit comments