Compare commits
8
Commits
1.1.0
...
bf9bcd4f7e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf9bcd4f7e | ||
|
|
a58ece7076 | ||
|
|
289101215d | ||
|
|
a24caddba5 | ||
|
|
96ee83d67a | ||
|
|
71c2d17fa3 | ||
|
|
a4a5280bab | ||
|
|
a1048d5f5c |
No files matched your search
@@ -14,7 +14,7 @@ jobs:
|
||||
|
||||
- name: Dependabot metadata
|
||||
id: metadata
|
||||
uses: dependabot/fetch-metadata@v2.4.0
|
||||
uses: dependabot/fetch-metadata@v2.5.0
|
||||
with:
|
||||
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
||||
|
||||
|
||||
+8
-3
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "mahdimsr/laravel-bitunix-api",
|
||||
"name": "moltox/laravel-bitunix-api",
|
||||
"description": "composer package for using bitunix api trading",
|
||||
"keywords": [
|
||||
"msr",
|
||||
@@ -13,12 +13,17 @@
|
||||
"name": "mahdi mansouri",
|
||||
"email": "mahdi.msr4@gmail.com",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "Maik Mueller ",
|
||||
"email": "mueller.maik1@gmail.com",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^8.4",
|
||||
"spatie/laravel-package-tools": "^1.16",
|
||||
"illuminate/contracts": "^11.0||^12.0||^10.0"
|
||||
"illuminate/contracts": "^10.0||^11.0||^12.0||^13.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"laravel/pint": "^1.14",
|
||||
@@ -69,7 +74,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.2",
|
||||
"minimum-stability": "stable",
|
||||
"prefer-stable": true
|
||||
}
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user