-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.nim
More file actions
75 lines (62 loc) · 3.05 KB
/
env.nim
File metadata and controls
75 lines (62 loc) · 3.05 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
import strutils, os, osproc
import chronos, metrics/chronos_httpserver, chronicles
from nativesockets import getHostname
let
mountsMix* = existsEnv("MOUNTSMIX") #Full mix-net peer
usesMix* = existsEnv("USESMIX") #Supports sending mix messages
mixCount* = parseInt(getEnv("NUMMIX", "0")) #Number of mix peers (mountsMix + usesMix)
inShadow* = getEnv("SHADOWENV").cmpIgnoreCase("true") == 0 #If Running for shadow simulator
httpPublishPort* = Port(8645)
prometheusPort* = Port(8008)
myPort* = Port(5000)
chunks* = parseInt(getEnv("FRAGMENTS", "1")) #No. of fragments for each message
mix_D* = parseInt(getEnv("MIXD", "4")) #No. of mix tunnels
proc getPeerDetails*(): Result[(int, int, int, string, string, string), string] =
let
hostname = getHostname()
myId = parseInt(hostname.split('-')[^1])
networkSize = parseInt(getEnv("PEERS", "100"))
connectTo = parseInt(getEnv("CONNECTTO", "10"))
muxer = getEnv("MUXER", "yamux")
filePath = if inShadow: "../" else: getEnv("FILEPATH", "./")
address = if muxer.toLowerAscii() == "quic":
"/ip4/0.0.0.0/udp/" & $myPort & "/quic-v1"
else:
"/ip4/0.0.0.0/tcp/" & $myPort
if muxer.toLowerAscii() notin ["quic", "yamux", "mplex"]:
return err("Unknown muxer type : " & muxer)
if connectTo >= networkSize:
return err("Not enough peers to make target connections. Network size : " & $networkSize)
info "Host info ", hostname = hostname, peer = myId, muxer = muxer, mountsMix = mountsMix, usesMix = usesMix, mixCount = mixCount, inShadow = inShadow, address = address
return ok((myId, networkSize, connectTo, muxer, filePath, address))
#Prometheus metrics
proc startMetricsServer*(
serverIp: IpAddress, serverPort: Port
): Result[MetricsHttpServerRef, string] =
info "Starting metrics HTTP server", serverIp = $serverIp, serverPort = $serverPort
let metricsServerRes = MetricsHttpServerRef.new($serverIp, serverPort)
if metricsServerRes.isErr():
return err("metrics HTTP server start failed: " & $metricsServerRes.error)
let server = metricsServerRes.value
try:
waitFor server.start()
except CatchableError:
return err("metrics HTTP server start failed: " & getCurrentExceptionMsg())
info "Metrics HTTP server started", serverIp = $serverIp, serverPort = $serverPort
ok(metricsServerRes.value)
#log metrics if needed (useful for shadow simulations)
proc storeMetrics*(myId: int) {.async.} =
await sleepAsync((myId*60).milliseconds)
while true:
try:
let cmd = "curl -s --connect-timeout 5 --max-time 5 http://localhost:" &
$prometheusPort & "/metrics >> metrics_pod-" & $myId & ".txt"
let exitCode = execCmd(cmd)
if exitCode == 0:
info "Metrics saved for peer ", pod = myId
else:
info "Failed to fetch metrics for peer ", pod = myId, curlExitCode = $exitCode
except CatchableError as e:
info "Error storing metrics: ", error = e.msg
return
await sleepAsync(5.minutes)