You didn't provide any details about your server, so I'm guessing...
You could write a plugin that checks how many users are already logged in whenever a user tries to login. If the number of logged in users is greater than the number of users that you want to allow then redirect the user to a "site is too busy page". But that would create a really bad user experience. Users don't care how busy the server is and they shouldn't be denied access to Moodle just because the server is too busy.
But if you want to persist with this...
In IIS you can set the "Connections limited to" setting on the Performance tab to your preferred value - the max number of users you want to allow. I'm not sure if you can do that in Apache...
Using an F5 load balancer you could:
- Count the number of post request that you get from the Moodle login page.
- Delay user logins if the logins per second is above your first limit.
- Send users to a maintenance page if logins per second is above your second limit, i.e. the site is really busy.
But the better solution is that if you think you are going to have more users than your existing server can handle then you need to look at upgrading your server/hosting solution, e.g. move your Moodle into some kind of server farm environment where there are enough web servers to handle the number of users that you are expecting. If any of the individual servers in the farm goes down at any time then the users will not be affected - their requests will simply get redirected to another server in the farm.
What you should really focus on first is fine tuning the server and the Moodle and PHP settings so that you get maximum performance out of that server, e.g. by setting the various Moodle, PHP and server caches optimally (MUC in Moodle, OPCache in PHP, Memcached for session management, etc.)
Hope that helps...