public function _loop($connectionIndex, $connection, &$jobs) { // Refresh statistics from mongrel2 $tnetstring = $connection->control('26:6:status,13:4:what,3:net,}]'); if ($tnetstring === null) { return; } $stats = \tnetstring_decode($tnetstring); if (isset($stats['rows'][0]) === false) { return; } if (is_array($stats['rows'][0]) === false) { $r = $stats['rows']; unset($stats['rows']); $stats['rows'][0] = $r; } // Mark all jobs as inactive to detect disconnected foreach ($jobs as $key => $job) { if (isset($jobs[$key]) === true) { $jobs[$key]['active'] = false; $jobs[$key]['bytes_written_last'] = $jobs[$key]['bytes_written']; } } // Update internal statistics foreach ($stats['rows'] as $row) { if ($row[0] === -1) { continue; } if (isset($jobs[$row[0]])) { $jobs[$row[0]]['bytes_written'] = $row[7]; $jobs[$key]['active'] = true; } } // Remove disconnected foreach ($jobs as $key => $job) { if ($job['active'] === false) { unset($jobs[$key]); } } // Grow mongrel2 buffer foreach ($jobs as $key => &$job) { // bytes transfered last second per the client $size = $job['bytes_written'] - $job['bytes_written_last']; // Try to increase the bandwidth, maybe the client can follow $size = $size < 1024 * 1024 ? 1024 * 1024 : $size; $overcommit = $job['bytes_written'] === $job['bytes_sent'] ? 2 : 1; // Iterate on the data, we can not known numbers and size of chuncks in the iterator do { // Ensure the mongrel2 buffer is at least 1MB or the size transfered last seconds if ($job['bytes_sent'] - $job['bytes_written'] > $size * $overcommit) { break; // Lot of data available in mongrel2 } // Ensure they have a chunck slot available in mongrel2 $limit = array_sum($job['last_chunck']) - $job['last_chunck'][0]; if ($job['bytes_sent'] - $job['bytes_written'] > $limit) { break; // No more slot } $it =& $job['iterator']; $it->next(); if ($it->valid() === false) { // Notify mongrel2 about the end $connection->send('PhotonDownload', $key, ''); // End of the iterator unset($it); unset($this->jobs[$key]); break; } $buffer = $it->current(); $sz = strlen($buffer); $job['bytes_sent'] += $sz; array_shift($job['last_chunck']); array_push($job['last_chunck'], $sz); $connection->send('PhotonDownload', $key, $buffer); } while (true); } unset($job); }
function thrash() { $tnetstring = "51:5:hello,39:11:12345678901#4:this,4:true!0:~4:,]}"; $result = tnetstring_decode($tnetstring); tnetstring_encode($result); }