Add dynamic api throthling

This commit is contained in:
DariusIII
2018-06-04 15:06:52 +02:00
parent c006070a01
commit 3922584879
11 changed files with 33 additions and 3 deletions
+1
View File
@@ -1,4 +1,5 @@
2018-06-04 DariusIII
* Chg: Add dynamic api throthling
* Fix: Fix headers that are being sent with getNzb function
* Fix: Make queries properly formatted for API V2
* Chg: Remove references to extended info in API V2, API always sends extended information
@@ -92,6 +92,7 @@ class UserController extends BasePageController
'password' => '',
'role' => User::ROLE_USER,
'notes' => '',
'rate_limit' => 60,
];
$title = 'View User';
+6
View File
@@ -332,6 +332,8 @@ class User extends Authenticatable
$userName = trim($userName);
$email = trim($email);
$rateLimit = UserRole::query()->where('id', $role)->value('rate_limit');
if (! self::isValidUsername($userName)) {
return self::ERR_SIGNUP_BADUNAME;
}
@@ -380,6 +382,7 @@ class User extends Authenticatable
'nzbvortex_api_key' => $nzbvortexApiKey,
'cp_url' => $cp_url,
'cp_api' => $cp_api,
'rate_limit' => $rateLimit,
];
self::query()->where('id', $id)->update($sql);
@@ -888,6 +891,8 @@ class User extends Authenticatable
return false;
}
$rateLimit = UserRole::query()->where('id', $role)->value('rate_limit');
if (\defined('NN_INSTALLER')) {
$storeips = '';
} else {
@@ -906,6 +911,7 @@ class User extends Authenticatable
'invitedby' => (int) $invitedBy === 0 ? 'NULL' : $invitedBy,
'userseed' => md5(Utility::generateUuid()),
'notes' => $notes,
'rate_limit' => $rateLimit,
]
)->id;
}
+2
View File
@@ -104,6 +104,7 @@ class UserRole extends Model
'hideads' => $request['hideads'],
'donation' => $request['donation'],
'addyears' => $request['addyears'],
'rate_limit' => $request['rate_limit'],
]
);
}
@@ -126,6 +127,7 @@ class UserRole extends Model
'hideads' => $request['hideads'],
'donation' => $request['donation'],
'addyears' => $request['addyears'],
'rate_limit' => $request['rate_limit'],
]
);
}
+2 -2
View File
@@ -16,8 +16,8 @@
</last>
</scripts>
<sql>
<db>347</db>
<file>347</file>
<db>348</db>
<file>348</file>
</sql>
</versions>
</nntmux>
@@ -39,6 +39,7 @@ class CreateUsersTable extends Migration {
$table->integer('consoleview')->default(1);
$table->integer('bookview')->default(1);
$table->integer('gameview')->default(1);
$table->integer('rate_limit')->default(60);
$table->string('saburl')->nullable();
$table->string('sabapikey')->nullable();
$table->boolean('sabapikeytype')->nullable();
@@ -20,6 +20,7 @@ class CreateUserRolesTable extends Migration {
$table->integer('id', true);
$table->string('name', 32);
$table->integer('apirequests')->unsigned();
$table->integer('rate_limit')->default(60);
$table->integer('downloadrequests')->unsigned();
$table->integer('defaultinvites')->unsigned();
$table->boolean('isdefault')->default(0);
+5
View File
@@ -0,0 +1,5 @@
# Add rate_limit column to users and user_roles tables, for api rate limiting purposes
ALTER TABLE users ADD rate_limit INT DEFAULT 60 COMMENT 'Rate limiting, requests per minute';
ALTER TABLE user_roles ADD rate_limit INT DEFAULT 60 COMMENT 'Rate limiting, requests per minute';
+5
View File
@@ -0,0 +1,5 @@
# Add rate_limit column to users and user_roles tables, for api rate limiting purposes
ALTER TABLE users ADD rate_limit INT DEFAULT 60 COMMENT 'Rate limiting, requests per minute';
ALTER TABLE user_roles ADD rate_limit INT DEFAULT 60 COMMENT 'Rate limiting, requests per minute';
@@ -28,6 +28,14 @@
</td>
</tr>
<tr>
<td>Api rate limit:</td>
<td>
<input name="rate_limit" type="text" value="{$role.rate_limit}"/>
<div class="hint">Number of api requests allowed per 1 minute</div>
</td>
</tr>
<tr>
<td>Download Requests:</td>
<td>
+1 -1
View File
@@ -26,7 +26,7 @@ Route::prefix('v2')->group(function () {
});
});
Route::prefix('v2')->middleware('auth:api')->group(function () {
Route::prefix('v2')->middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
Route::namespace('Api')->group(function () {
Route::get('movies', 'ApiV2Controller@movie');
Route::post('movies', 'ApiV2Controller@movie');