Skip to content

Commit

Permalink
feat: degradation mode
Browse files Browse the repository at this point in the history
- skip inserting transactions
- add manual toggle in Site Admin tasks
  • Loading branch information
nbey authored and themightychris committed Aug 21, 2020
1 parent 42dd82e commit d30f6c9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{extends "task.tpl"}

{block content}
<h4>
<b>Skip Insert Transaction (Degradation Mode):</b> <u>{tif $enabled ? Active : Inactive}</u>
</h4>

<form method="post">
<div class="checkbox">
<label>Enable
<input type="radio" name="status" value="enable" placeholder="Enabled" {tif $enabled ? checked} />
</label>
<label>Disable
<input type="radio" name="status" value="disable" placeholder="Disabled" {tif $enabled == false ? checked} />
</label>
</div>

<div class="form-group">
<label>TTL
<input type="number" min="60" name="ttl" placeholder="ttl seconds" value="{$ttl}">
</label>
</div>

<button type="submit" class="btn btn-primary">{tif $enabled ? Dis : En}able Degradation Mode</button>
</form>
{/block}
36 changes: 25 additions & 11 deletions php-classes/Gatekeeper/ApiRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class ApiRequestHandler extends \RequestHandler

public static $responseMode = 'json'; // override RequestHandler::$responseMode

public static $degradationTimeout = 60;

public static function handleRequest() {

// initialize request object
Expand Down Expand Up @@ -68,17 +70,29 @@ public static function handleRequest() {

// initialize log record
if (!Cache::fetch('flags/gatekeeper/skip-insert-transaction')) {
$Transaction = Transaction::create([
'Endpoint' => $request->getEndpoint()
,'Key' => $request->getKey()
,'ClientIP' => ip2long($_SERVER['REMOTE_ADDR'])
,'Method' => $_SERVER['REQUEST_METHOD']
,'Path' => $path
,'Query' => $query
,'ResponseTime' => $curlInfo['starttransfer_time'] * 1000
,'ResponseCode' => $curlInfo['http_code']
,'ResponseBytes' => $curlInfo['size_download']
]);
try {
$Transaction = Transaction::create([
'Endpoint' => $request->getEndpoint()
,'Key' => $request->getKey()
,'ClientIP' => ip2long($_SERVER['REMOTE_ADDR'])
,'Method' => $_SERVER['REQUEST_METHOD']
,'Path' => $path
,'Query' => $query
,'ResponseTime' => $curlInfo['starttransfer_time'] * 1000
,'ResponseCode' => $curlInfo['http_code']
,'ResponseBytes' => $curlInfo['size_download']
]);
} catch (\Exception $e) {
Cache::store('flags/gatekeeper/skip-insert-transaction', true, static::$degradationTimeout);
\Emergence\Logger::general_warning(
'Transaction Exception: {exceptionMessage}. Setting degredation flag for {seconds} seconds',
[
'exception' => $e,
'exceptionMessage' => $e->getMessage(),
'seconds' => static::$degradationTimeout
]
);
}
}


Expand Down
24 changes: 24 additions & 0 deletions site-tasks/configuration/skip-insert-transaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

return [
'title' => 'Skip Transaction Inserts (Degradation Mode)',
'description' => 'Enable or disable degredation mode which skips saving transactions to the DB. This functionality aids with the speed of requests with sites while under failure or high load',
'icon' => 'power-off',
'handler' => function () {
$flag = 'flags/gatekeeper/skip-insert-transaction';
$ttl = $_REQUEST['ttl'] ?: Gatekeeper\ApiRequestHandler::$degradationTimeout;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_REQUEST['status'] == 'enable') {
Cache::store($flag, true, $ttl);
} else {
Cache::delete($flag);
}
}

return static::respond('skip-insert-transactions', [
'ttl' => $ttl,
'enabled' => !!Cache::fetch($flag)
]);
}
];

0 comments on commit d30f6c9

Please sign in to comment.