Created
March 24, 2018 21:57
-
-
Save aguspiza/80e34b5cf65aa3bbfd19c7339ee9b695 to your computer and use it in GitHub Desktop.
nim 0.18.0 memory use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncdispatch, asyncnet | |
| import times, os, strutils, posix | |
| proc c_signal(sig: cint, handler: proc (a: cint) {.noconv.}) {.importc: "signal", header: "<signal.h>".} | |
| type | |
| Service = ref object of RootObj | |
| name: string | |
| method start(this: Service): string {.base.} = "Started ..." & this.name | |
| method stop(this: Service): string {.base.} = "... stopped " & this.name | |
| type | |
| ControllersService = ref object of Service | |
| controllersConnected: seq[string] | |
| method start(this: ControllersService): string = | |
| var clients {.threadvar.}: seq[AsyncSocket] | |
| proc processClient(client: AsyncSocket): Future[void] {.async.} = | |
| echo "Processing" | |
| while not client.isClosed: | |
| let rec = await client.recvLine() | |
| if rec == "": | |
| echo "closing..." | |
| client.close() | |
| return | |
| await client.send("hey! $1\c\L" % rec) | |
| proc createServer(port: Port) {.async.} = | |
| echo "Creating server ..." | |
| clients = @[] | |
| var server = newAsyncSocket() | |
| server.setSockOpt(OptReuseAddr, true) | |
| server.bindAddr(port) | |
| server.listen() | |
| echo "Listeninig in " & $port.int | |
| while true: | |
| let client = await server.accept() | |
| echo "Connected" | |
| clients.add client | |
| asyncCheck processClient(client) | |
| asyncCheck createServer(10335.Port) | |
| procCall Service(this).start() #super call | |
| method stop(this: ControllersService): string = | |
| procCall Service(this).stop() | |
| proc startServices(services: openArray[Service] ) = | |
| for service in services: | |
| echo service.start() | |
| proc stopServices(services: openArray[Service] ) = | |
| for service in services: | |
| echo service.stop() | |
| let services = @[Service(name: "TestService"), ControllersService(name: "ControllersService")] | |
| #Handle Ctrl-C | |
| type EKeyboardInterrupt = object of Exception | |
| proc ctrlCHandler() {.noconv.} = | |
| raise newException(EKeyboardInterrupt, "Keyboard Interrupt") | |
| setControlCHook(ctrlCHandler) | |
| #Handle SIGTERM | |
| type TermInterrupt = object of Exception | |
| proc termHandler(sig: cint) {.noconv.} = | |
| raise newException(TermInterrupt, "TERM Interrupt") | |
| #setTermHook(termHandler) | |
| c_signal(SIGTERM, termHandler) | |
| let t = epochTime() | |
| try: | |
| startServices(services) | |
| for na in 1 .. <1000: | |
| 1.sleep | |
| echo na | |
| proc f(n: int): Future[void] {.async.} = | |
| echo "Connect" | |
| let s = newAsyncNativeSocket() | |
| await s.connect("localhost", 10335.Port) | |
| echo "Client connected" | |
| var r = 0 | |
| while r < 1000: | |
| r += 1 | |
| echo "Send $1 $2" % [$n, $r] | |
| await s.send("Client: $1 $2\c\L" % [$n, $r]) | |
| echo "Receive $1 $2" % [$n, $r] | |
| let line = await s.recvLine() | |
| echo line | |
| #echo "Wait $1 $2" % [$n, $r] | |
| #await sleepAsync(10) | |
| echo "Finished: " & $n | |
| asyncCheck f(na) | |
| runForever() | |
| except Exception: | |
| echo " **** EXCEPTION: " & getCurrentExceptionMsg() | |
| stopServices(services) | |
| echo "Program has run for ", formatFloat(epochTime() - t, precision = 0), " seconds." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment