Thanks for this great module Wim. Even newbs like me are able to install and configured this module properly.

I need some help being able to use the cdn_pick_server() function, so that Drupal use our different CDN's for all the .js, .css and images, being able to parallelize downloads across different CDNs.

I've read the README and used the following code at PHP code for cdn_pick_server():

    /**
     * Implementation of cdn_pick_server().
     */
    function cdn_pick_server($servers_for_file) {
      $filename = basename($servers_for_file[0]['url']);
      $unique_file_id = hexdec(substr(md5($filename), 0, 5));
      return $servers_for_file[$unique_file_id % count($servers_for_file)];
    }

Inside the PHP code for cdn_pick_server(): block without success. Just in case, at the CDN mapping block, I've placed 2 URL's:

http://cdn.mydomain.com
http://cdn2.mydomain.com

Yet, all our content is being delivered by the first CDN.

Any idea or help will be greatly appreciated!

Comments

wim leers’s picture

Title: Parallelize downloads across different CDNs? » Eval'd PHP code version of cdn_pick_server() not working + improve docs
Assigned: Unassigned » wim leers
Category: support » bug
Priority: Normal » Critical
Status: Active » Fixed

I bet you used the form at admin/settings/cdn/other? I.e. you didn't create a new small module for this?

Then there are two reasons:
1) You didn't copy the body of the function, but the whole function. This is wrong. I've improved the README.txt about this:

Note: if you don't want to create a small module for this function, or if you
      would just like to experiment with this function, you can also enter the
      body of this function at admin/settings/cdn/other — it will work exactly
      the same!
      If you don't know what the "body" of a function is, it's the part
      between the curly brackets:
        function doSomething() {
          BODY
        }
      So, in the case of the cdn_pick_server() function, this is the body that
      you would enter:
        $filename = basename($servers_for_file[0]['url']);
        $unique_file_id = hexdec(substr(md5($filename), 0, 5));
        return $servers_for_file[$unique_file_id % count($servers_for_file)];

2) The eval'd PHP code version of cdn_pick_server() was not actually being called yet! Oops! Fixed that too!

Fix committed: http://drupal.org/cvs?commit=443636

Duplika’s picture

Terrific Wim! After installing the new release, everything seems to be working fine great!

In my case, most of the images are loaded from the CSS so I don't quite benefit from parallelization. This has nothing to do with your module, but to accomplish this, should I modify the CSS so that some images are being called from one cdn and others, from the other?

Is that the recommended approach?

wim leers’s picture

What you're suggesting would work, but it would also mean that you would have to edit the CSS files manually.

There's two alternatives to that:
1) Use File Conveyor, but you'll have to write a custom file processor in Python to automatically balance image files from different CDNs. This of course means that you'll have to install File Conveyor, which is a non-trivial task.
2) Use the Support File Cache module, which can generate smarter CSS bundles (well, not generate, it allows *you* to define these bundles). This effectively means that you've got more CSS files, and thus that the images will automatically be balanced over as many CDNs as you have CSS files (bundles).

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.