Add CacheRateLimiter
This commit is contained in:
parent
a58ece7076
commit
bf9bcd4f7e
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Msr\LaravelBitunixApi\Contracts;
|
||||
|
||||
interface RateLimiterContract
|
||||
{
|
||||
public function throttle(
|
||||
string $bucket,
|
||||
string $identity,
|
||||
int $maxRequests,
|
||||
float $perSeconds
|
||||
): void;
|
||||
}
|
||||
|
|
@ -305,10 +305,8 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
|
|||
$body['slStopType'] = $slStopType;
|
||||
}
|
||||
|
||||
$response = $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
|
||||
return $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
|
||||
'json' => $body,
|
||||
]);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace Msr\LaravelBitunixApi;
|
||||
|
||||
use Msr\LaravelBitunixApi\Commands\LaravelBitunixApiCommand;
|
||||
use Msr\LaravelBitunixApi\Contracts\RateLimiterContract;
|
||||
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
|
||||
use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract;
|
||||
use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract;
|
||||
|
|
@ -13,8 +14,10 @@ use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
|
|||
use Msr\LaravelBitunixApi\Requests\PlaceOrderRequestContract;
|
||||
use Msr\LaravelBitunixApi\Requests\PlacePositionTpSlOrderRequestContract;
|
||||
use Msr\LaravelBitunixApi\Requests\PlaceTpSlOrderRequestContract;
|
||||
use Msr\LaravelBitunixApi\Support\CacheRateLimiter;
|
||||
use Spatie\LaravelPackageTools\Package;
|
||||
use Spatie\LaravelPackageTools\PackageServiceProvider;
|
||||
use Illuminate\Contracts\Cache\Factory as CacheFactory;
|
||||
|
||||
class LaravelBitunixApiServiceProvider extends PackageServiceProvider
|
||||
{
|
||||
|
|
@ -47,5 +50,11 @@ class LaravelBitunixApiServiceProvider extends PackageServiceProvider
|
|||
$this->app->bind(PlaceTpSlOrderRequestContract::class, LaravelBitunixApi::class);
|
||||
$this->app->bind(PlacePositionTpSlOrderRequestContract::class, LaravelBitunixApi::class);
|
||||
$this->app->bind(GetTradingPairsRequestContract::class, LaravelBitunixApi::class);
|
||||
|
||||
$this->app->bind(RateLimiterContract::class, function ($app) {
|
||||
return new CacheRateLimiter(
|
||||
$app->make(CacheFactory::class)->store()
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Msr\LaravelBitunixApi\Support;
|
||||
|
||||
use Illuminate\Contracts\Cache\Repository;
|
||||
use Msr\LaravelBitunixApi\Contracts\RateLimiterContract;
|
||||
|
||||
class CacheRateLimiter implements RateLimiterContract
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository $cache,
|
||||
) {
|
||||
}
|
||||
|
||||
public function throttle(
|
||||
string $bucket,
|
||||
string $identity,
|
||||
int $maxRequests,
|
||||
float $perSeconds
|
||||
): void {
|
||||
if ($maxRequests <= 0 || $perSeconds <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cacheKey = $this->makeCacheKey($bucket, $identity);
|
||||
$minIntervalMicroseconds = (int) (($perSeconds / $maxRequests) * 1_000_000);
|
||||
|
||||
$lastRequestAt = $this->cache->get($cacheKey);
|
||||
|
||||
if (is_numeric($lastRequestAt)) {
|
||||
$elapsedMicroseconds = (int) ((microtime(true) - (float) $lastRequestAt) * 1_000_000);
|
||||
|
||||
if ($elapsedMicroseconds < $minIntervalMicroseconds) {
|
||||
$sleepMicroseconds = $minIntervalMicroseconds - $elapsedMicroseconds;
|
||||
|
||||
if ($sleepMicroseconds > 0) {
|
||||
usleep($sleepMicroseconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->cache->put(
|
||||
$cacheKey,
|
||||
microtime(true),
|
||||
now()->addSeconds((int) max(1, ceil($perSeconds * 2)))
|
||||
);
|
||||
}
|
||||
|
||||
protected function makeCacheKey(string $bucket, string $identity): string
|
||||
{
|
||||
return sprintf(
|
||||
'laravel_bitunix_api:rate_limit:%s:%s',
|
||||
$bucket,
|
||||
sha1($identity)
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue