netdata_tsrelay.nim
changeset 9 aa9d537f7067
parent 8 1ef3f2d6d10e
child 11 475c9942eb15
--- a/netdata_tsrelay.nim	Sun Feb 18 18:16:37 2018 -0800
+++ b/netdata_tsrelay.nim	Sun Feb 18 22:18:44 2018 -0800
@@ -1,5 +1,4 @@
 # vim: set et nosta sw=4 ts=4 :
-# im: set et nosta sw=4 ts=4 ft=nim : 
 #
 # Copyright (c) 2018, Mahlon E. Smith <mahlon@martini.nu>
 # All rights reserved.
@@ -76,6 +75,7 @@
 # Global configuration
 var conf: Config
 
+
 proc hl( msg: string, fg: ForegroundColor, bright=false ): string =
     ## Quick wrapper for color formatting a string, since the 'terminal'
     ## module only deals with stdout directly.
@@ -171,8 +171,7 @@
 
 
 proc process( client: Socket, address: string ): void =
-    ## Do the work for a connected client within a thread.
-    ## Returns the formatted json data keyed on sample time.
+    ## Do the work for a connected client within child process.
     let t0 = cpu_time()
     var raw_data = client.fetch_data
 
@@ -205,8 +204,14 @@
     ## Open a database connection, bind to the listening socket,
     ## and start serving incoming netdata streams.
     let db = open( "", "", "", conf.dbopts )
+    db.close
     if conf.verbose: echo( "Successfully tested connection to the backend database.".hl( fgGreen ) )
-    db.close
+
+    # Ensure children are properly reaped.
+    #
+    var sa: Sigaction
+    sa.sa_handler = SIG_IGN
+    discard sigaction( SIGCHLD, sa )
 
     # Setup listening socket.
     #
@@ -230,9 +235,9 @@
         var
             client  = new Socket
             address = ""
-            status: cint = 0
 
-        server.acceptAddr( client, address ) # block
+        # Block, waiting for new connections.
+        server.acceptAddr( client, address )
 
         if fork() == 0:
             server.close
@@ -240,8 +245,6 @@
             quit( 0 )
 
         client.close
-
-        discard waitpid( P_ALL, status, WNOHANG ) # reap all previous children
         when defined( testing ): dumpNumberOfInstances()
 
 
@@ -294,9 +297,7 @@
 
 when isMainModule:
     system.addQuitProc( resetAttributes )
-
     conf = parse_cmdline()
     if conf.debug: echo hl( $conf, fgYellow )
-
     serverloop( conf )