如何在NodeJS中检测循环引用?

时间:2012-01-31 16:07:48

标签: javascript node.js solr circular-reference

我正在NodeJS中开发一个守护进程脚本,它可以同时多次查询(在我们的例子中)SolR。更详细地说,我们有一个用PHP编写的大型应用程序,在某些页面上需要更多的SolR请求。 NodeJS是我们的解决方案,因为使用NodeJS,我能够向SolR服务器发出多个HTTP请求。已经证明它可以节省大量时间。

关键是,一切正常并且像预期的那样,但守护进程将永远运行。这意味着,如果存在内存泄漏,即使它非常小,也是一个(大)问题。我现在正在调试几天,只是尝试和错误减少了大量的内存使用量。但我无法确定没有泄漏。

我一直在网上搜索,我认为循环引用是我的问题。我的问题是如何避免它们?有时它对我来说是不可能的。我试图搜索一些东西来分析我在NodeJS中的内存使用情况,但是https://github.com/dannycoates/v8-profiler被不幸地打破了。

我来自PHP背景,所以事件驱动的开发等对我来说有点新鲜。我想知道是否有人可以在以下代码中确定可能泄漏并解释原因:

  function ProxyHttpServer () {
  this.httpServer = http.createServer();
  this.configuration = new ProxyConfiguration();

  var logWriter = new ProxyLogWriter();

  /**
   * This is the error event which occurs when the deamon is started
   * and encounters that already 'something' is listening to the same port, and tries restarting
   * for several times.
   */
  ProxyHttpServer.prototype.serverError = function(e) {
      this.configuration = new ProxyConfiguration();

      if (e.code == 'EADDRINUSE') {                
          if(this.configuration.numRestart > 0) {
              console.log('Address and/or port already in use, retrying (' 
                  + this.configuration.numRestart + ' attempts left)', 'WARNING');

              this.configuration.numRestart -= 1;

              setTimeout(function () {                    
                  self.httpServer.listen(this.configuration.port, this.configuration.host);
              }, 1000);                    
          } else {
              console.log('I tried my best, but i\'m too tired now, check ps -fauxwww | grep http-query-daemon to ' 
                  + 'see if this process is running already.', 'ERROR');
          }
      }

      return ;
  }

  ProxyHttpServer.prototype.requestListener = function(request, response) {
      this.proxyHttpClient = new ProxyHttpClient();
      this.proxyHttpResponseWriter = new ProxyHttpResponseWriter();

      this.proxyHttpClient.request = request;                        
      this.proxyHttpClient.response = response;                        
      this.proxyHttpClient.proxyHttpResponseWriter = this.proxyHttpResponseWriter;
      this.proxyHttpResponseWriter.response = response;

      this.proxyHttpClient.validate();

      switch(this.proxyHttpClient.getRoute()) {
          case 'stat' :
              logWriter.writeLog(this.proxyHttpClient);
              break;                
          case 'request' :
              this.queries = this.proxyHttpClient.getQueries();         
              this.logger = new ProxyLogger();                    

              for(var i in this.queries) {                                                           
                  this.httpRequest = new ProxyQueryRequest(this.queries[i], {
                      'client' : this.proxyHttpClient, 
                      'responseWriter' : this.proxyHttpResponseWriter, 
                      'queries' : this.queries,
                      'logger' : this.logger,
                      'finish' : function(responseXml, startTime, endTime, url, objectArguments) {
                          console.log('Request finished in ' + (endTime - startTime) + ' microsends', 'INFO');

                          objectArguments.responseWriter.addDocument(responseXml);

                          console.log(objectArguments.responseWriter.getNumDocs() + ' docs written, ' 
                              + objectArguments.queries.length + ' queries', 'INFO');

                          objectArguments.logger.addRequest((endTime - startTime), url, startTime);

                          //fire writer if all requests are finished
                          if(objectArguments.responseWriter.getNumDocs() == objectArguments.queries.length) {
                              objectArguments.responseWriter.writeResponse();                                             
                          }

                          responseXml = null;
                          startTime = null;
                          endTime = null;
                          url = null;
                          objectArguments = null;

                          return ;                            
                      },
                      'error' : function(message, objectArguments) {
                          objectArguments.responseWriter.writeError(message, true);
                      }
                  })        
                  .execute();
              }            
              break;
          default :
              this.proxyHttpResponseWriter.writeError('There is no valid route');
              break;                    
      }

      this.proxyHttpClient = null;
      this.proxyHttpResponseWriter = null;
      this.queries = null;
      this.logger = null;
      this.httpRequest = null;

      return ;
  };

我无法发布完整的代码,因为它超过500行,如果有人想要它我可以在pastebin中发布它。 或者,是否有人可以向我推荐有关此主题的良好文档?

0 个答案:

没有答案