Strengthened validation for HashRing a bit.
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 13 May 2013 18:20:11 +0000 (11:20 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Mon, 13 May 2013 18:20:19 +0000 (11:20 -0700)
Change-Id: Ib0331662c785d806063ca730905dd88b6545d8f0

includes/HashRing.php

index 227a287..73a38af 100644 (file)
@@ -36,8 +36,8 @@ class HashRing {
         * @param array $map (location => weight)
         */
        public function __construct( array $map ) {
-               $sum = array_sum( $map );
-               if ( !count( $map ) || $sum <= 0 ) {
+               $map = array_filter( $map, function( $w ) { return $w > 0; } );
+               if ( !count( $map ) ) {
                        throw new MWException( "Ring is empty or all weights are zero." );
                }
                // Sort the locations based on the hash of their names