This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JOB_NAME=job-killer && \ | |
NAMESPACE=my-namespace && \ | |
wget -qO - https://tinyurl.com/pod-killer-job > pod-killer-job.yaml && \ | |
sed -ie "s|\${JOB_NAME}|${JOB_NAME}|g" pod-killer-job.yaml && sed -ie "s|\${NAMESPACE}|${NAMESPACE}|g" pod-killer-job.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: ${JOB_NAME}-sa | |
namespace: ${NAMESPACE} | |
--- | |
apiVersion: rbac.authorization.k8s.io/v1 | |
kind: Role |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
import aiohttp | |
async def send_request(client_session: aiohttp.ClientSession, url: str, rate_limiter: RateLimiter): | |
async with rate_limiter.throttle(): | |
print(f'sending url: {url}') | |
response = await client_session.get(url) | |
print(f'releasing throttler') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import math | |
import time | |
from contextlib import asynccontextmanager | |
class RateLimiter: | |
def __init__(self, | |
rate_limit: int, | |
concurrency_limit: int) -> None: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def close(self) -> None: | |
if self.quota_consumer_task and not self.quota_consumer_task.cancelled(): | |
try: | |
self.quota_consumer_task.cancel() | |
await self.quota_consumer_task | |
except asyncio.CancelledError: | |
# ignore the error here but log to inform the task was cancelled | |
pass | |
except Exception as e: | |
# log and deal with the error here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def close(self) -> None: | |
if self.quota_consumer_task and not self.quota_consumer_task.cancelled(): | |
try: | |
self.quota_consumer_task.cancel() | |
await self.quota_consumer_task | |
except asyncio.CancelledError: | |
# ignore the error here but log to inform the task was cancelled | |
pass | |
except Exception as e: | |
# log and deal with the error here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def close(self) -> None: | |
if self.quota_consumer_task and not self.quota_consumer_task.cancelled(): | |
try: | |
self.quota_consumer_task.cancel() | |
await self.quota_consumer_task | |
except asyncio.CancelledError: | |
# ignore the error here but log to inform the task was cancelled | |
pass | |
except Exception as e: | |
# log and deal with the error here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def close(self) -> None: | |
if self.quota_consumer_task and not self.quota_consumer_task.cancelled(): | |
try: | |
self.quota_consumer_task.cancel() | |
await self.quota_consumer_task | |
except asyncio.CancelledError: | |
# ignore this exception and log here to inform the consumer task was cancelled | |
pass | |
except Exception as e: | |
# log and deal with the error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@asynccontextmanager | |
async def throttle(self) -> None: | |
await self.semaphore.acquire() | |
await self.add_token() | |
try: | |
yield | |
finally: | |
self.semaphore.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def throttle(self) -> | |
async with self.semaphore: | |
await self.produce_quota() |
NewerOlder