apache - Parallel request on the same PHP script causes a second long delay -


background

this driving me crazy weeks. use mrclay's php minify script minify , concatenate js , css. works great, files merged common.css , common.js, virtual files, translated link to script this:

rewriterule ^common.js$ /tynamic/min/?g=js [l,qsa] rewriterule ^common.css$ /tynamic/min/?g=css [l,qsa] 

a query string appended denote version of these files, , have 3 year long caching, person visited site never have download css or js on future visit site (if don't change, obviously). far works.

the problem

often (sadly not always), when browser asks these 2 files (which done @ same time), one of files takes second returned. 1 request arrives later server, usually 1 later in html, not rule.

see these screenshots:

taken firefox

test report pingdom

i'd okay server putting other file queue , proccesing after first one, doesn't take whole second. few more things: no action concatetion or gzipping being performed in case.. script doing fpassthru() of existing pre-gzipped file. doesn't happen time however.. there gets little bit odd, if large number of consecutive pageloads, 30 or more, goes "normal" when both files processed in trivial time. when check after time, on second hang. the time little on second.

what tried

  1. putting if($_get["g"]=="js") exit; right @ start of script.

thats right, of no help. file still delayed, outputting nothing. exit; (for both files) works... :)

  1. timing scripts

both runs report minimal times (units or tens of milliseconds) of runs, there no function delay it.

  1. different server/hosting

no help, 3 different servers , hosting providers. not hosting related.

  1. making full copy of script

so made copy of full script directory ensure both runs made different set of files - no help.

  1. disabling file locking nad other tweaks script config or script itself.

so far didn't came :(

  1. different script - doing else.

this interesting, modifying files else, e.g. scandir , pick file didn't either. analysis showed, php scripts being assigned free cpu threads every second. if there e.g. 5 threads, , 6 scripts need run @ once, first 5 done in 10 msecs, 6th has wait whole second start being proccessed... why happening?

thanks lot in advance effort put in helping me

cbroe right. if using sessions (session_start()), php serve 1 request 1 client (session_id) @ time. while 1 request being served, other queued until session written first one. session file locked prevent multiple requests writing same session can cause unexpected results. session written either when script finished or calling session_write_close(). free session next request.

however, feel obliged tell you doing wrong. shouldn't minimizing js , css php. here reasons:

  1. using php causes unnecessary load on server
  2. the browser still requesting files 304 response - again unnecessary load on server , reduced user experience (these requests still take time)
  3. it's hard build minification tool , there's no need reinvent wheel. better 1 readily available.
  4. there's more reasons...

i suggest better invest time in not writing minification scripts, in learning build tools (grunt or gulp) job , more want/be able write in php.

in nutshell, how whole process works is

  1. you set server sent expires header. prevent client requesting changes in files. google how done using apache: https://www.google.com/search?q=apache+expires+htaccess&ie=utf-8&oe=utf-8&gws_rd=cr&ei=y12dvrqkg8kvsahfikjyda
  2. set tools above "build" minified resources - concatenate multiple files, "compile" stylesheets, minify etc. have these built files on disk , served directly webserver.
  3. you set website use minified resources in production. (you want able debug full source in development).
  4. when deploy changes website .

the skill of setting come in handy web developer. also, free time building web app itself.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -