N, 'day' => N]. Use 0 to skip that period. * @return bool True if request is allowed, false if over limit */ function quota_limiter_allow($key, $limits = array()) { $CI = &get_instance(); if (!isset($CI->db) || !$CI->db->conn_id) { return true; } $table = 'quota_usage'; $hour_period = date('Y-m-d-H'); $day_period = date('Y-m-d'); $hour_limit = isset($limits['hour']) ? (int) $limits['hour'] : 100; $day_limit = isset($limits['day']) ? (int) $limits['day'] : 500; $key_safe = substr($key, 0, 255); foreach (array('hour' => array($hour_period, $hour_limit), 'day' => array($day_period, $day_limit)) as $period_type => $pair) { list($period_value, $limit) = $pair; if ($limit <= 0) { continue; } $CI->db->query(" INSERT INTO {$table} (key_name, period_type, period_value, count, updated_at) VALUES (?, ?, ?, 1, NOW()) ON DUPLICATE KEY UPDATE count = count + 1, updated_at = NOW() ", array($key_safe, $period_type, $period_value)); $row = $CI->db->query(" SELECT count FROM {$table} WHERE key_name = ? AND period_type = ? AND period_value = ? ", array($key_safe, $period_type, $period_value))->row(); if ($row && (int) $row->count > $limit) { return false; } } return true; } } if (!function_exists('quota_limiter_fcm_key')) { function quota_limiter_fcm_key($recipient_identifier) { return 'fcm:' . substr(md5($recipient_identifier), 0, 32); } }