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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.