LoginSignup
0
0

More than 5 years have passed since last update.

mod n を利用して負荷分散

Posted at

概要

APIキー複数作って負荷を均等に分散させ、利用制限を緩和したい

ここはJKらしくmodを使おう:innocent:

modてなに

高校数学で出てくる。数Aだったかな?

modて言うと難しそう:frowning2:

ある数をnで割った余りと考えれば簡単:relaxed:

$ 10 ≡ 1(mod3) $
→ 10÷3の余りは1

ソースコード

jQueryで書いたよ

script.js
var clientId;
$.ajax({
  url: './count.php' // Simple count up,
  type: 'GET',
  dataType: 'text',
  cache: false,
  async: false
})
.done((response) => {
  var count = response;
  var calc = count % 2;
  if (calc === 0){
    clientId = 'API-key 111111111111111'
  }else if(calc === 1){
    clientId = 'API-key 000000000000000'
  }
})

phpはONZEさんを参考にしてます。

count.php
<?php
$counter_file = 'count.txt';
$counter_lenght = 8;
$fp = fopen($counter_file, 'r+');
if ($fp) {
    if (flock($fp, LOCK_EX)) {
        $counter = fgets($fp, $counter_lenght);
        $counter++;
        rewind($fp);
        if (fwrite($fp,  $counter) === FALSE) {
            echo ('<p>'.'ファイル書き込みに失敗しました'.'</p>');
        }
        flock ($fp, LOCK_UN);
    }
}
fclose ($fp);

echo $counter;
?>

まとめ

一応mod使えば負荷分散出来んじゃね??

って思ったから書いたけどOKかはしらね (・ω<)

その辺詳しい人よろしくお願いします

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0