DDoS can't be protected by code, it is more then protecting your server, usually DDoS hurt your load balancer and firewall if you have some, if not the DDoS will hurt your server.
DDoS can be done via many levels: UPD/TCP/HTTP etc...
The best way to protect yourself from DDoS is to use reverse proxy so if you go to your site it wont show the real IP, and happily we have now for free with Cloud Flare. https://www.cloudflare.com/
I wrote a small article about how Cloud Flare protects you as i'm using them since a year now and they are the BEST so far and the cheapest.
http://www.yourwwwdesign.com/2012/07/23/best-practice-to-protect-your-site-from-ddos-for-free/
Hope this helps! if you need more help please don't hesitate to contact me.
x
depending on the request rates such that it would let regular requests go through easily, while excessive repetitiveness will introduce delays. So it should not be a weak point. The weak point, as others have mentioned, is the distributed nature of requests. – Gezira