omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / core / cortex / tests / conc / slow.om
| 1 | note slow.om: slow-client (slowloris) driver. usage: slow PORT N SECS OUT |
| 2 | note Opens N connections to 127.0.0.1:PORT, sends an incomplete request head ("GET /health |
| 3 | note HTTP/1.1" and one header line) and then one more header byte per connection every second, |
| 4 | note never finishing the head. Stops each connection when the server answers or closes it, or |
| 5 | note when SECS seconds have passed. OUT gets one line per connection: "<index> <ns alive> <how>" |
| 6 | note with how = closed (server closed without answering), answered:<first 12 answer bytes>, or |
| 7 | note open (still open at SECS). Exit 0 when every connection was opened. |
| 8 | global pa uchar 4096 |
| 9 | global sa uchar 16 |
| 10 | global ob uchar 128 |
| 11 | global rb uchar 4096 |
| 12 | note per connection: fd, start ns, end ns, state (0 open, 1 closed, 2 answered, 3 never opened) |
| 13 | global cfd ulong 1024 |
| 14 | global cst ulong 1024 |
| 15 | global cen ulong 1024 |
| 16 | global cmo ulong 1024 |
| 17 | global ans uchar 12288 |
| 18 | global sv ulong 4 |
| 19 | text u_use "usage: slow PORT N SECS OUT\n" |
| 20 | text h_req "GET /health HTTP/1.1\nhost: 127.0.0.1\nx-slow: " |
| 21 | text w_cl " closed\n" |
| 22 | text w_op " open\n" |
| 23 | text w_an " answered:" |
| 24 | fn say t view uchar * to void |
| 25 | w = write 2 t 0 len t |
| 26 | if lt w 0 |
| 27 | ret |
| 28 | end |
| 29 | end |
| 30 | fn argn i ulong to ulong |
| 31 | n = argread i pa 0 len pa |
| 32 | if le n 0 |
| 33 | ret 0 |
| 34 | end |
| 35 | v = ulong 0 |
| 36 | for k ulong ulong n |
| 37 | c = get pa k |
| 38 | if or lt c 48 gt c 57 |
| 39 | ret 0 |
| 40 | end |
| 41 | v = add mul v 10 ulong sub c 48 |
| 42 | end |
| 43 | ret v |
| 44 | end |
| 45 | fn obdec o ulong v ulong to ulong |
| 46 | k = o |
| 47 | if ge v 10 |
| 48 | k = obdec o div v 10 |
| 49 | end |
| 50 | put ob k uchar add rem v 10 48 |
| 51 | ret add k 1 |
| 52 | end |
| 53 | fn opn port ulong to int |
| 54 | fd = socknew 2 524289 0 |
| 55 | if lt fd 0 |
| 56 | ret -1 |
| 57 | end |
| 58 | put sa 0 2 |
| 59 | put sa 1 0 |
| 60 | put sa 2 uchar shr port 8 |
| 61 | put sa 3 uchar and port 255 |
| 62 | put sa 4 127 |
| 63 | put sa 5 0 |
| 64 | put sa 6 0 |
| 65 | put sa 7 1 |
| 66 | r = connect int fd sa 0 16 |
| 67 | if ne r 0 |
| 68 | x = fclose int fd |
| 69 | ret -1 |
| 70 | end |
| 71 | s = send int fd h_req 0 len h_req 0 |
| 72 | if ne s long len h_req |
| 73 | x = fclose int fd |
| 74 | ret -1 |
| 75 | end |
| 76 | ret int fd |
| 77 | end |
| 78 | note look at connection i once: answer, close, or still waiting; true while still open |
| 79 | fn poll1 i ulong to bool |
| 80 | fd = int get cfd i |
| 81 | w = waitio fd 1 0 |
| 82 | if le w 0 |
| 83 | ret true |
| 84 | end |
| 85 | r = recv fd rb 0 12 0 |
| 86 | put cen i ulong monons |
| 87 | x = fclose fd |
| 88 | if le r 0 |
| 89 | put cmo i 1 |
| 90 | ret false |
| 91 | end |
| 92 | put cmo i 2 |
| 93 | for k ulong ulong r |
| 94 | put ans add mul i 12 k get rb k |
| 95 | end |
| 96 | ret false |
| 97 | end |
| 98 | fn drip i ulong to void |
| 99 | put rb 0 120 |
| 100 | s = send int get cfd i rb 0 1 0 |
| 101 | if le s 0 |
| 102 | put cen i ulong monons |
| 103 | put cmo i 1 |
| 104 | x = fclose int get cfd i |
| 105 | end |
| 106 | end |
| 107 | fn emitln ofd int i ulong to void |
| 108 | o = obdec 0 i |
| 109 | put ob o 32 |
| 110 | o = add o 1 |
| 111 | o = obdec o sub get cen i get cst i |
| 112 | m = get cmo i |
| 113 | if eq m 1 |
| 114 | for k ulong len w_cl |
| 115 | put ob add o k get w_cl k |
| 116 | end |
| 117 | o = add o len w_cl |
| 118 | end |
| 119 | if eq m 0 |
| 120 | for k ulong len w_op |
| 121 | put ob add o k get w_op k |
| 122 | end |
| 123 | o = add o len w_op |
| 124 | end |
| 125 | if eq m 2 |
| 126 | for k ulong len w_an |
| 127 | put ob add o k get w_an k |
| 128 | end |
| 129 | o = add o len w_an |
| 130 | for k ulong 12 |
| 131 | c = get ans add mul i 12 k |
| 132 | if or lt c 32 gt c 126 |
| 133 | c = 46 |
| 134 | end |
| 135 | put ob add o k c |
| 136 | end |
| 137 | o = add o 12 |
| 138 | put ob o 10 |
| 139 | o = add o 1 |
| 140 | end |
| 141 | w = write ofd ob 0 o |
| 142 | end |
| 143 | fn main to int |
| 144 | port = argn 1 |
| 145 | n = argn 2 |
| 146 | secs = argn 3 |
| 147 | if or or eq port 0 eq n 0 gt n 1024 |
| 148 | say u_use |
| 149 | ret 2 |
| 150 | end |
| 151 | q = argread 4 pa 0 len pa |
| 152 | if le q 0 |
| 153 | say u_use |
| 154 | ret 2 |
| 155 | end |
| 156 | ofd = fopen pa 0 ulong q 524865 420 |
| 157 | if lt ofd 0 |
| 158 | say u_use |
| 159 | ret 2 |
| 160 | end |
| 161 | bad = ulong 0 |
| 162 | for i ulong n |
| 163 | put cst i ulong monons |
| 164 | f = opn port |
| 165 | if lt f 0 |
| 166 | put cmo i 3 |
| 167 | put cen i ulong monons |
| 168 | bad = add bad 1 |
| 169 | else |
| 170 | put cfd i ulong f |
| 171 | put cmo i 0 |
| 172 | end |
| 173 | end |
| 174 | dl = add monons mul long secs 1000000000 |
| 175 | nx = add monons 1000000000 |
| 176 | while lt monons dl bound 999999 |
| 177 | live = ulong 0 |
| 178 | for i ulong n |
| 179 | if eq get cmo i 0 |
| 180 | if poll1 i |
| 181 | live = add live 1 |
| 182 | end |
| 183 | end |
| 184 | end |
| 185 | if eq live 0 |
| 186 | break |
| 187 | end |
| 188 | if ge monons nx |
| 189 | for i ulong n |
| 190 | if eq get cmo i 0 |
| 191 | drip i |
| 192 | end |
| 193 | end |
| 194 | nx = add nx 1000000000 |
| 195 | end |
| 196 | x = sleepns 20000000 |
| 197 | end |
| 198 | for i ulong n |
| 199 | if eq get cmo i 0 |
| 200 | put cen i ulong monons |
| 201 | x = fclose int get cfd i |
| 202 | end |
| 203 | emitln int ofd i |
| 204 | end |
| 205 | if gt bad 0 |
| 206 | ret 1 |
| 207 | end |
| 208 | ret 0 |
| 209 | end |