Compare commits

..
8 Commits
Author SHA1 Message Date
mis_mueller bf9bcd4f7e Add CacheRateLimiter 2026-04-12 19:43:56 +02:00
moltox a58ece7076 Merge pull request '1.2.2' (#1) from 1.2.2 into main
Reviewed-on: #1
2026-04-11 21:44:13 +00:00
mis_mueller 289101215d change version to 1.2.2 2026-04-11 23:43:37 +02:00
mis_mueller a24caddba5 change version to 1.2.1 2026-04-11 23:41:23 +02:00
mis_mueller 96ee83d67a change version to 1.2.0 2026-04-11 23:40:26 +02:00
mis_mueller 71c2d17fa3 Change structure. 2026-04-11 16:20:35 +02:00
github-actions[bot] a4a5280bab Merge pull request #13 from mahdimsr/dependabot/github_actions/dependabot/fetch-metadata-2.5.0
build(deps): bump dependabot/fetch-metadata from 2.4.0 to 2.5.0
2026-01-05 21:36:58 +00:00
dependabot[bot] a1048d5f5c build(deps): bump dependabot/fetch-metadata from 2.4.0 to 2.5.0
Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 2.4.0 to 2.5.0.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](https://github.com/dependabot/fetch-metadata/compare/v2.4.0...v2.5.0)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-version: 2.5.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-01-05 21:36:45 +00:00
6 changed files with 89 additions and 7 deletions

No files matched your search

+1 -1
View File
@@ -14,7 +14,7 @@ jobs:
- name: Dependabot metadata - name: Dependabot metadata
id: metadata id: metadata
uses: dependabot/fetch-metadata@v2.4.0 uses: dependabot/fetch-metadata@v2.5.0
with: with:
github-token: "${{ secrets.GITHUB_TOKEN }}" github-token: "${{ secrets.GITHUB_TOKEN }}"
+8 -3
View File
@@ -1,5 +1,5 @@
{ {
"name": "mahdimsr/laravel-bitunix-api", "name": "moltox/laravel-bitunix-api",
"description": "composer package for using bitunix api trading", "description": "composer package for using bitunix api trading",
"keywords": [ "keywords": [
"msr", "msr",
@@ -13,12 +13,17 @@
"name": "mahdi mansouri", "name": "mahdi mansouri",
"email": "mahdi.msr4@gmail.com", "email": "mahdi.msr4@gmail.com",
"role": "Developer" "role": "Developer"
},
{
"name": "Maik Mueller ",
"email": "mueller.maik1@gmail.com",
"role": "Developer"
} }
], ],
"require": { "require": {
"php": "^8.4", "php": "^8.4",
"spatie/laravel-package-tools": "^1.16", "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": { "require-dev": {
"laravel/pint": "^1.14", "laravel/pint": "^1.14",
@@ -69,7 +74,7 @@
} }
} }
}, },
"version": "1.1.0", "version": "1.2.2",
"minimum-stability": "stable", "minimum-stability": "stable",
"prefer-stable": true "prefer-stable": true
} }
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Msr\LaravelBitunixApi\Contracts;
interface RateLimiterContract
{
public function throttle(
string $bucket,
string $identity,
int $maxRequests,
float $perSeconds
): void;
}
+1 -3
View File
@@ -305,10 +305,8 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
$body['slStopType'] = $slStopType; $body['slStopType'] = $slStopType;
} }
$response = $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [ return $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
'json' => $body, 'json' => $body,
]); ]);
return $response;
} }
} }
+9
View File
@@ -3,6 +3,7 @@
namespace Msr\LaravelBitunixApi; namespace Msr\LaravelBitunixApi;
use Msr\LaravelBitunixApi\Commands\LaravelBitunixApiCommand; use Msr\LaravelBitunixApi\Commands\LaravelBitunixApiCommand;
use Msr\LaravelBitunixApi\Contracts\RateLimiterContract;
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract; use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract; use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract;
use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract; use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract;
@@ -13,8 +14,10 @@ use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
use Msr\LaravelBitunixApi\Requests\PlaceOrderRequestContract; use Msr\LaravelBitunixApi\Requests\PlaceOrderRequestContract;
use Msr\LaravelBitunixApi\Requests\PlacePositionTpSlOrderRequestContract; use Msr\LaravelBitunixApi\Requests\PlacePositionTpSlOrderRequestContract;
use Msr\LaravelBitunixApi\Requests\PlaceTpSlOrderRequestContract; use Msr\LaravelBitunixApi\Requests\PlaceTpSlOrderRequestContract;
use Msr\LaravelBitunixApi\Support\CacheRateLimiter;
use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider; use Spatie\LaravelPackageTools\PackageServiceProvider;
use Illuminate\Contracts\Cache\Factory as CacheFactory;
class LaravelBitunixApiServiceProvider extends PackageServiceProvider class LaravelBitunixApiServiceProvider extends PackageServiceProvider
{ {
@@ -47,5 +50,11 @@ class LaravelBitunixApiServiceProvider extends PackageServiceProvider
$this->app->bind(PlaceTpSlOrderRequestContract::class, LaravelBitunixApi::class); $this->app->bind(PlaceTpSlOrderRequestContract::class, LaravelBitunixApi::class);
$this->app->bind(PlacePositionTpSlOrderRequestContract::class, LaravelBitunixApi::class); $this->app->bind(PlacePositionTpSlOrderRequestContract::class, LaravelBitunixApi::class);
$this->app->bind(GetTradingPairsRequestContract::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()
);
});
} }
} }
+57
View File
@@ -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)
);
}
}