netdata_tsrelay.nim
changeset 7 c0bcf3bea772
parent 6 1f366fc61592
child 8 1ef3f2d6d10e
equal deleted inserted replaced
6:1f366fc61592 7:c0bcf3bea772
   189             " sample(s) parsed from ",
   189             " sample(s) parsed from ",
   190             address.hl( fgYellow, bright=true ),
   190             address.hl( fgYellow, bright=true ),
   191             " in ", hl($( round(cpu_time() - t0, 3) ), fgWhite, bright=true), " seconds."
   191             " in ", hl($( round(cpu_time() - t0, 3) ), fgWhite, bright=true), " seconds."
   192             # " ", hl($(round((get_occupied_mem()/1024/1024),1)), fgWhite, bright=true), "MB memory used."
   192             # " ", hl($(round((get_occupied_mem()/1024/1024),1)), fgWhite, bright=true), "MB memory used."
   193         )
   193         )
   194     when defined( testing ): dumpNumberOfInstances()
       
   195 
   194 
   196 
   195 
   197 proc serverloop: void =
   196 proc serverloop: void =
   198     ## Open a database connection, bind to the listening socket,
   197     ## Open a database connection, bind to the listening socket,
   199     ## and start serving incoming netdata streams.
   198     ## and start serving incoming netdata streams.
   200     let db = open( "", "", "", conf.dbopts )
   199     let db = open( "", "", "", conf.dbopts )
   201     if conf.verbose: echo( "Successfully connected to the backend database.".hl( fgGreen ) )
   200     if conf.verbose: echo( "Successfully connected to the backend database.".hl( fgGreen ) )
   202 
   201 
   203     var server = newSocket()
   202     var
       
   203         conn_count = 0
       
   204         server = newSocket()
   204 
   205 
   205     server.set_sock_opt( OptReuseAddr, true )
   206     server.set_sock_opt( OptReuseAddr, true )
   206     server.bind_addr( Port(conf.listen_port), conf.listen_addr )
   207     server.bind_addr( Port(conf.listen_port), conf.listen_addr )
   207     server.listen()
   208     server.listen()
   208 
   209 
   217 
   218 
   218     while true:
   219     while true:
   219         var client  = newSocket()
   220         var client  = newSocket()
   220         var address = ""
   221         var address = ""
   221 
   222 
       
   223         # Force a garbage collection pass.
       
   224         #
       
   225         conn_count = conn_count + 1
       
   226         if conn_count == 25:
       
   227             when defined( testing ): echo "Forcing GC pass."
       
   228             GC_full_collect()
       
   229             conn_count = 1
       
   230 
   222         client.close
   231         client.close
   223         server.acceptAddr( client, address ) # blocking call
   232         server.acceptAddr( client, address ) # blocking call
   224         spawn runthread( client, address, db, conf )
   233         spawn runthread( client, address, db, conf )
   225 
   234         when defined( testing ): dumpNumberOfInstances()
   226 
       
   227 proc atexit() {.noconv.} =
       
   228     ## Exit cleanly after waiting on any running threads.
       
   229     echo "Exiting..."
       
   230     sync()
       
   231     quit( 0 )
       
   232 
   235 
   233 
   236 
   234 proc parse_cmdline: void =
   237 proc parse_cmdline: void =
   235     ## Populate the config object with the user's preferences.
   238     ## Populate the config object with the user's preferences.
   236 
   239 
   271         of cmdEnd: assert( false ) # shouldn't reach here ever
   274         of cmdEnd: assert( false ) # shouldn't reach here ever
   272 
   275 
   273 
   276 
   274 when isMainModule:
   277 when isMainModule:
   275     system.addQuitProc( resetAttributes )
   278     system.addQuitProc( resetAttributes )
   276     system.addQuitProc( atexit )
       
   277 
   279 
   278     parse_cmdline()
   280     parse_cmdline()
   279 
       
   280     if conf.debug: echo hl( $conf, fgYellow )
   281     if conf.debug: echo hl( $conf, fgYellow )
       
   282 
   281     serverloop()
   283     serverloop()
   282 
   284