Skip to content

u/canton7

In this archive from Jul 2008 to Dec 2022

1
posts
27
comments
0
media
91
score

Yeah: documenting that the method returns false on failure would appears to stop implementers from throwing exceptions on failure. They have to return false in order to meet the interface's contract, so they cannot throw an exception. This seems really odd to me, which is why I wondered if I was missing something.

I suspect that implementers will end up ignoring the return value of these methods entirely and doing the sensible thing of throwing exceptions on error, but then it seems silly that the interface has documented behaviour which everyone will break. This also means that users will have to handle both exceptions and a false return value.

TTL is in seconds, and a negative or zero TTL automatically expires

Aha, I missed that bit of the docs, thanks. I think the doc comments could probably do with some work, since that's what people are going to read from their editor.

2 points

There are a number of methods which return a boolean indicating whether or not an error occurred. However there's no way to get what the error was. This makes the interface feel very C-like, and not at all like a modern object-orientated language.

Why do these methods return a bool, rather than throwing an exception on failure?

In addition, get, getMultiple and has don't return a bool, so there's no way of indicating an error in the same way as the other methods.

As a separate issue, the $ttl parameter to set can take an int, but there's no indication of the units of this value in the doc comment. What happens if a negative or zero interval is given? Is an exception thrown? What?

39 points

I did read something (no source, for now, so don't take me at my word) which said that a notebook isn't such a bad idea. The sort of person who's going to break into your house and steal your computer isn't the sort of person who's going to steal your password and try and get your identity / bank account. They're two different demographics.

The person stealing your computer is shitting his pants trying to get in and out as quickly as possible, and already has a fair few hundred quid's worth of tech in his hands. He's not going to rifle through your notebooks in the offchance that there's a password he can use in there.

Notebooks, on the other hand, are very hard for malware to steal...

That said, I use and love KeePass and Keepass2Android.

2 points

I see it as a bit of an ugly workaround. Sure it's useful and I'm glad to see it, but it shouldn't be necessary. I don't want to start a PHP vs <some language> war, but other languages have solved this problem through nicer syntax, essentially:

// Ruby
some_array.map{ |element| element['name'] }

// Python
[element['name'] for element in some_array]

// PHP's array_map
array_map(function($element){ return $element['name']; }, $some_array);

// PHP's proposed list comprehensions
[foreach ($some_array as $element) yield $element['name']];

// PHP's array_column
array_column($some_array, 'name');

If PHP had nicer lambda or list comprehension syntax, array_column wouldn't be necessary.

In an ideal world, I'd like to see proper OO support (har har), and shorter lambdas, so something like the following would be possible:

$some_array->map($element => $element['name']);
2 points

Anything that does not match the "==" comparison operator.

That doesn't match up with your expected result. Using anything as the separator (as you stated) surely gives a result of

$out = array(
    "Mozilla/" => array(
        "5.0 (compatible; MSIE 10.0; Windows NT 6.1; " => array(
            "WOW64; Trident/6.0)",
            "Trident/" => array(
                "6.0",
                "5.0",
            ),
        "4.0 (compatible; MSIE 10.0; Windoes NT 6.1; Trident/5.0)"
    );

Anyway, I wrote a function which implements the spec as you describe it, as someone's already implemented the spec as given by your example:

$in = array(
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",
    "Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)",
);


function challenge($str, $result) {
    $lengths = array_map(function($a) use ($str){ return strspn($a ^ $str, chr(0)); }, array_keys($result));
    if (!count($lengths)) {
        $result[$str] = array();
        return $result;
    }
    $pos = array_keys($lengths, max($lengths))[0];
    $prefix_length = $lengths[$pos];
    $common = substr($str, 0, $prefix_length);
    $key = array_keys($result)[$pos];
    if ($key == $common) {
        if (count($result[$key]))
            $result[$key] = challenge(substr($str, $prefix_length), $result[$key]);
        else
            $result[$key][substr($str, $prefix_length)] = array();
    }
    else {
        $result[$common] = array(substr($key, $prefix_length) => $result[$key], substr($str, $prefix_length) => array());
        unset($result[$key]);
    }
    return $result;
}

function filter_empty($result) {
    $ret = array();
    foreach ($result as $k => $v) {
        if (count($v)) $ret[$k] = filter_empty($v);
        else           $ret[] = $k;
    }
    return $ret;
}

$result = array();
foreach ($in as $str) {
    $result = challenge($str, $result);
}

print_r(filter_empty($result));
2 points

I think I'm correct in remembering that if you haven't modified the config file which was installed by pacman, then a new version of the config file will simply overwrite the old one. .pacnew files only appear when you've made changes to the old config file, and pacman doesn't want to throw away your changes.

8 points

So let's assume you have 2 virtual sites using your server, and that 5% of people viewing those 2 sites are actually hitting the server at any one time. Let's also ignore the long-polling stuff for now. With a thread pool of 150, then we can handle 3000 or so users using those sites at once

If 5% of 3000 people are hitting your server at once, that's 5% of 3000 = 150 simultaneous connections.
This is how many working threads you have, meaning that your server's running at capacity.

If, however, just one of those virtual sites is running your CMS with its single long-polling worker, and a mere 100 users are using your CMS, then the number of users you can support between both sites drops to 1000.

Each user of your CMS ties up one worker thread permanently due to the long-polling (assuming each user only has one long polling connection). This means that there are only 50 free worker threads to handle normal requests, that is, 50 simultaneous connections are supported. 50 / 5% = 1000 users.

If 150 users are using your CMS, then both websites grind to a halt.

All the worker threads are tied up doing long-polling, meaning there are none free to serve other resources. Like web pages.

Also be aware that each worker takes somewhere around 5-15megs,

ps -ylC httpd --sort:rss

and two many active workers could cause to start swapping, which will bring everything to a halt.

Common sense

1 points

Depends on how big your max thread pool is. My defaults for apache are set at 150 workers max.

Remember than each thread that's handling a long poll can't handle any incoming requests.

So let's assume you have 2 virtual sites using your server, and that 5% of people viewing those 2 sites are actually hitting the server at any one time. Let's also ignore the long-polling stuff for now.
With a thread pool of 150, then we can handle 3000 or so users using those sites at once.

If, however, just one of those virtual sites is running your CMS with its single long-polling worker, and a mere 100 users are using your CMS, then the number of users you can support between both sites drops to 1000.

If 150 users are using your CMS, then both websites grind to a halt

Also be aware that each worker takes somewhere around 5-15megs, and two many active workers could cause to start swapping, which will bring everything to a halt.

EDIT: Assuming you combine the two long-polling scripts.

2 points

The recommended approach seems to be to run a separate, dedicated server that's intended to just handle long-poll requests. Google for "comet server".

This does, of course, make it a lot harder to just provide people with a zip package of your cms, which they can deploy by unzipping. If you need to keep with pure PHP, you'll have to weigh up long polling vs short polling.

2 points

I think 0bp's point was that you don't have separate views, instead choosing to mix HTML into various functional PHP files.

MVC is popular for a reason, and it's worth looking into why.

2 points

You're using long polling, which is good.

However, it looks a lot like pure PHP files are being used to handle the server side of the long poll, which is bad. The reason being you now have two worker threads permanently tied up for each user (chat_poll and aspect_poll), and your thread pool is going to disappear pretty rapidly.

Edit: Also, what's with all the print "<script>window.location=\"../index.php?id=$newID\"</script>";? What's wrong with a good old HTTP redirect?

2 points

Don't forget the good ol' array_merge (stolen from jQuery's way of doing things):

function something($params) {
    $params = array_merge($params, array(
        'a' => 'default_for_a',
        'b' => 'default_for_b',
    ));
}

That way you get the option of leaving out parameters if you want, and you don't have to check that all required params have been specifies.

1 points

I tend to go with:

  function randstr($length=32) {
     $alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
     $str = '';
     for ($i=0; $i<$length; $i++) {
        $str .= $alphabet[rand(0, strlen($alphabet)-1)];
     }
     return $str;
  }

Your implementation will have roughly the same proportion of numbers, lowercase and uppercase, which, as there are fewer numbers than letters, means (I believe) that your entropy is lower than it would be otherwise.

2 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.