魏长东

weichangdong

Nginx 的 Echo 模块 —— echo-nginx-module

 location /hello {
    echo "hello, world!";
  }

  location /hello {
    echo -n "hello, "
    echo "world!";
  }

  location /timed_hello {
    echo_reset_timer;
    echo hello world;
    echo "'hello world' takes about $echo_timer_elapsed sec.";
    echo hiya igor;
    echo "'hiya igor' takes about $echo_timer_elapsed sec.";
  }

  location /echo_with_sleep {
    echo hello;
    echo_flush;  # ensure the client can see previous output immediately
    echo_sleep   2.5;  # in sec
    echo world;
  }

  # in the following example, accessing /echo yields
  #   hello
  #   world
  #   blah
  #   hiya
  #   igor
  location /echo {
      echo_before_body hello;
      echo_before_body world;
      proxy_pass $scheme://127.0.0.1:$server_port$request_uri/more;
      echo_after_body hiya;
      echo_after_body igor;
  }
  location /echo/more {
      echo blah;
  }

  # the output of /main might be
  #   hello
  #   world
  #   took 0.000 sec for total.
  # and the whole request would take about 2 sec to complete.
  location /main {
      echo_reset_timer;
 
      # subrequests in parallel
      echo_location_async /sub1;
      echo_location_async /sub2;
 
      echo "took $echo_timer_elapsed sec for total.";
  }
  location /sub1 {
      echo_sleep 2;
      echo hello;
  }
  location /sub2 {
      echo_sleep 1;
      echo world;
  }

  # the output of /main might be
  #   hello
  #   world
  #   took 3.003 sec for total.
  # and the whole request would take about 3 sec to complete.
  location /main {
      echo_reset_timer;
 
      # subrequests in series (chained by CPS)
      echo_location /sub1;
      echo_location /sub2;
 
      echo "took $echo_timer_elapsed sec for total.";
  }
  location /sub1 {
      echo_sleep 2;
      echo hello;
  }
  location /sub2 {
      echo_sleep 1;
      echo world;
  }

  # Accessing /dup gives
  #   ------ END ------
  location /dup {
    echo_duplicate 3 "--";
    echo_duplicate 1 " END ";
    echo_duplicate 3 "--";
    echo;
  }

  # /bighello will generate 1000,000,000 hello's.
  location /bighello {
    echo_duplicate 1000_000_000 'hello';
  }

  # echo back the client request
  location /echoback {
    echo_duplicate 1 $echo_client_request_headers;
    echo "\r";
 
    echo_read_request_body;
 
    echo_request_body;
  }

  # GET /multi will yields
  #   querystring: foo=Foo
  #   method: POST
  #   body: hi
  #   content length: 2
  #   ///
  #   querystring: bar=Bar
  #   method: PUT
  #   body: hello
  #   content length: 5
  #   ///
  location /multi {
      echo_subrequest_async POST '/sub' -q 'foo=Foo' -b 'hi';
      echo_subrequest_async PUT '/sub' -q 'bar=Bar' -b 'hello';
  }
  location /sub {
      echo "querystring: $query_string";
      echo "method: $echo_request_method";
      echo "body: $echo_request_body";
      echo "content length: $http_content_length";
      echo '///';
  }

  # GET /merge?/foo.js&/bar/blah.js&/yui/baz.js will merge the .js resources together
  location /merge {
      default_type 'text/javascript';
      echo_foreach_split '&' $query_string;
          echo "/* JS File $echo_it */";
          echo_location_async $echo_it;
          echo;
      echo_end;
  }

  # accessing /if?val=abc yields the "hit" output
  # while /if?val=bcd yields "miss":
  location ^~ /if {
      set $res miss;
      if ($arg_val ~* '^a') {
          set $res hit;
          echo $res;
      }
      echo $res;
  }

好的测试工具wrk

比ab好,我感觉,在并发数量上,wrk更加优胜。可以用lua脚本加强一些功能。

lua和php运行速度

在网上看到一篇文章,同样的功能用集中语言实现,然后执行比较速度。其中有我喜欢的php和正在学习的lua,就把代码搞到自己的机器上运行了,lua比php慢,但是luajit却快了十倍,让我开始喜欢luajit了。我喜欢一个语言,很简单,只需要一个简单的理由。

这是执行时间

php time:17811.112880707ms php5.6

lua time:31530ms lua5.14

luajit time:2790ms

检测你的nginx是否使用了jit

ldd /usr/local/openresty/nginx/sbin/nginx

下面上代码

»»阅读全文

lua的协程学习

有些知识当时看懂了,但是过一阵子发现看半天又看不懂了,真是不知所谓。

可能是学习没有复习的原因吧。

Lua所支持的协程全称被称作协同式多线程,协同程序与线程差不多,也就是一条执行序列,拥有自己独立的栈,局部变量和指令指针,同时又与其它协同程序共享全局变量和其它大部分东西。线程与协同程序的主要区别在于,一个具有多线程的程序可以同时运行几个线程,而协同程序却需要彼此协作地运行。就是说,一个具有多个协同程序的程序在任何时刻只能运行一个协同程序,并且正在运行的协同程序只会在其显示地挂起时,它的执行才会暂停。

我的理解。就是暂停程序,等下一次调用,然后从上次停下的地方继续执行。

»»阅读全文

2016年回家图

写了

从北京住的地方到北京西站,坐高铁到西安北站,然后坐地铁去西安南站附近,然后打车去西安南站,做高速到柞水,然后我哥哥接我,在我哥哥加住宿。然后我哥开车从柞水回老家,过完年我哥开车送我们去镇安,在镇安住一晚,然后坐软卧去襄阳,然后他哥哥在襄阳东站接我们,我们又在他哥哥家住宿一晚。在襄阳呆了几天我一个人从yy家出发,去襄阳火车站,然后无座13个小时到北京

的大概过程,结果一不小心全没了,擦擦擦擦擦擦。

懒得在写了,直接上图了吧。

»»阅读全文

qconf 批量添加配置

<?php
//之前是代码写死配置的,要让用qconf,但是一个一个的添加太麻烦,就写这个小工具。把之前代码要用的fb数据抓取信息都添加到qconf里面。

$qzk = new QConfZK("*:2181");
//$value = $qzk->nodeGet("/common/products/wcd/uiver");
//print_r($value) . PHP_EOL;

$app_info_old = [
        9 => ['app_id' => '*******************','token' => '*******************|kdn-***-zuiqCjXkf97I'], //
        8 => ['app_id' => '*******************','token' => '**********************************'], //
		10 => ['app_id' => '*******************','token' => '**********************************'],//
		30001 => ['app_id' => '*******************','token' => '**********************************'],//
        10000 => ['app_id' => '*******************', 'token' => '*******************|***--Ps7EezCrc'],//
        //20000 => ['app_id' => '*******************', 'token' => '**********************************'],//
		20000 => ['app_id' => '*******************', 'token' => '**********************************'],//
		20001 => ['app_id' => '*******************', 'token' => '*******************|alaGob-***-EzLsWG5gxKE'],//
	  20 => ['app_id' => '*******************','token' => '**********************************'],// 
	  21 => ['app_id' => '*******************','token' => '**********************************'],//
      23 => ['app_id' => '*******************','token' => '*******************|***-wuZI'],//
	  10023 => ['app_id' => '*******************','token' => '**********************************'],//
	  24 => ['app_id' => '*******************','token' => '**********************************'],//
	  25 => ['app_id' => '*******************','token' => '**********************************'],//
    ];

$path = '/common/products/';
$all_product = $qzk->list($path);
foreach ($all_product as  $pc) {
	if ($pc != 'wcd_test'){
		//continue;
	}
	$wcd = $qzk->listWithValue($path.$pc);
	$appid = $wcd['appid'];
	if (!isset($app_info_old[$appid])){
		continue;	
	}
	$qzk->nodeSet($path.$pc.'/fb_app_id',$app_info_old[$appid]['app_id']);
	$qzk->nodeSet($path.$pc.'/fb_token',$app_info_old[$appid]['token']);
	$qzk->nodeSet($path.$pc.'/fb_online',1);
	$get_value = $qzk->listWithValue($path.$pc);
	print_r($get_value);
}

test

[need password!]

Lua中的错误与异常

Lua中error的处理:Lua经常作为扩展语言嵌入在别的应用中,所以不能当错误发生时简单的崩溃或者退出。相反,当错误发生时Lua结束当前的chunk并返回到应用中。

 

  当Lua遇到不期望的情况时就会抛出错误,你也可以通过调用error函数显式地抛出错误。

 

  当函数遇到异常有两个基本的动作:返回错误代码或者抛出错误。选择哪一种方式,没有固定的规则,不过基本的原则是:对于程序逻辑上能够避免的异常,以抛出错误的方式处理之,否则返回错误代码。

»»阅读全文