Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I am writing an Apache Module, based on an existing script Apache::BlockIP. The existing script will block IP addresses (contained on a text file). Basically, you abuse my site, your IP gets written to this file, and Apache::BlockIP will block you from further activity.
Hmm... once this file gets big, looking through this flatfile for every single request might be getting too expensive. There most be a way to make it faster...

You could, as the document page referred to by one of the previous replies, indeed use a dbm database... or a SQLite database could work too. You'd have to compare the results in a benchmark.

As this is for modperl, you probably could just as well store them in an in-memory hash. It'll be fast. It could eventually be big, too, depending on how many IPs you have blocked...

If you have more than one webserver (that could be just child processes of the same mother web server) and they share this database (probably), you may still have to set up a make like set-up: you load the data from the file into the hash at startup, and every time you notice the modification date of the text file has changed, for example.

One the hash gets too big, there might be a way to optimize this into something that's like a hash, but using much less memory. For example, you could use nested trees, splitting each IP address into bytes, and have a 256 entry array for each... and use vec at the last (4th) level, storing just a single bit, that'll use just 32 bytes for a block of 256 IP addresses. But it won't be as fast as a straight hash.

It might even be a good idea to turn the latter into an XS module.


In reply to Re: Send User To Oblivion! by bart
in thread Send User To Oblivion! by carfac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-29 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found