Thursday, March 31, 2011

PHP pcntl_fork

[cc lang="php"]
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
pcntl_wait($status); //Protect against Zombie children
} else {
// we are the child

echo "download files ...\n";
$cmd = "/usr/bin/wget --user-agent=\"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)\" \\
--header=\"Accept:text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\" \\
--header=\"Accept-Language: en-us,en;q=0.5\" \\
--header=\"Accept-Encoding: gzip,deflate\" \\
--header=\"Accept-Charset: GBK,utf-8;q=0.7,*;q=0.7\" \\
--header=\"Keep-Alive: 300\" -O /dev/null -i list.txt -o wget.log";
exec($cmd);
echo "done\n";
declare(ticks = 1);

pcntl_signal(SIGUSR1, function ($signal) {
echo 'HANDLE SIGNAL ' . $signal . PHP_EOL;
});

posix_kill(posix_getpid(), SIGUSR1);
die();
}
[/cc]

Monday, March 14, 2011

Friday, March 11, 2011

Caching WordPress with Varnish

WordPress is a fantastic application that lets you get a website up and running in a matter of minutes. Not only that, but you require absolutely no knowledge of how to code to do so. As with many web applications though, it needs a little help in getting it to perform better. Lots of these dynamic apps breaks caching by using lots of cookies and setting headers like Cache-Control and Pragma. For a better in-depth tutorial on caching, read this article.

Requirements



  • Web server running on a different port than the standard one (TCP/80)

  • Varnish installed


Configuration


The standard location for Varnish’s configuration file is located at/etc/varnish/default.vcl. You will find below the configuration I use on this blog. See the comments to get a better understanding of what’s what. Also, make sure varnish will listen on port TCP/80 (use the -a flag when you start the daemon).
# Send all requests to your webserver
backend default {
.host = "127.0.0.1";
.port = "8080";
}

sub vcl_recv {
# If we don't set this, in our webserver's logs, we'll get varnish's IP instead
# of the real client's IP
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For ", " client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
# Unless we're in the admin/login section, remove all cookies
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
return (lookup);
}

sub vcl_pipe {
return (pipe);
}

sub vcl_pass {
return (pass);
}

sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
return (hash);
}

sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
return (deliver);
}

sub vcl_miss {
return (fetch);
}

sub vcl_fetch {
if (!beresp.cacheable) {
return (pass);
}
if (beresp.http.Set-Cookie) {
return (pass);
}
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
return (deliver);
}

sub vcl_deliver {
# Remove bad headers
remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Age;
remove resp.http.X-Powered-By;
remove resp.http.Cache-Control;
remove resp.http.Pragma;
return (deliver);
}

Restart Varnish to load the new configuration.

Testing the recipe


Open up varnishstat in your terminal. You should see some stats. Ctrl-F5 a few times your site in your favorite browser. That way, you’re telling your browser to bypass its own cache and ask the site for fresh items. The first time you access your site, you will get lots of misses, but after that, you should get lots of cache hits.
Hitrate ratio:        9        9        9
Hitrate avg:     0.7302   0.7302   0.7302
22         0.00         0.16 Client connections accepted
132         0.00         0.95 Client requests received
100         0.00         0.72 Cache hits
5         0.00         0.04 Cache hits for pass
25         0.00         0.18 Cache misses

As you can see, I’m getting about 4 times more cache hits than misses. This way, Varnish will server all the static items and will let your webserver take care of all the dynamic content. Hope this helps a bit.

 

Saturday, March 5, 2011

突破Windows XP SP3远程桌面连接数限制

长期在Linux下工作,但是常常需要在IE6下做WEB测试,于是在开发服务器上安装了Xen和Windows XP SP3。但是XP默认只能1个远程连接,所以需要“破解”。

  1. 下载破解过的[download id="2" format="3"],版本5.1.2600.5512,语言中文(中国)。

  2. 复制到windows\system32文件夹覆盖同名文件。(如果开着SFC还要覆盖dllcache或者servicepackfiles\i386里的)

  3. 运行RAR包里的MultipleTS.bat

  4. 运行gpedit.msc打开组策略,找到终端服务-限制连接数量改到2以上

  5. 确保启用快速用户切换和允许用户远程连接到此计算机

  6. 重启电脑确保更改生效

Thursday, March 3, 2011

How to compile all files in directory with Emacs

[cc lang='lisp' ]C-u 0 M-x byte-recompile-directory[/cc]

will compile all the .el files in the directory and in all subdirectories below.

The C-u 0 part is to make it not ask about every .el file that does not have a .elc counterpart.

Wednesday, March 2, 2011

HTC Desire 连接到Eclipse(Linux)

[cc lang="text"]
su
cd android-sdk-linux-linux/tools
./adb kill-server
./adb start-server
./adb devices
exit
[/cc]

You must use root (sudo)