Skip to content

Instantly share code, notes, and snippets.

View DannyMor's full-sized avatar
🎯
Focusing

Danny Mor DannyMor

🎯
Focusing
View GitHub Profile
@DannyMor
DannyMor / pod-killer-job-generator.sh
Created October 7, 2021 19:47
Pod Killer Job Generator SH
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
@DannyMor
DannyMor / pod_killer_job.yaml
Last active October 7, 2021 11:36
Pod Killer Job
apiVersion: v1
kind: ServiceAccount
metadata:
name: ${JOB_NAME}-sa
namespace: ${NAMESPACE}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
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')
import asyncio
import math
import time
from contextlib import asynccontextmanager
class RateLimiter:
def __init__(self,
rate_limit: int,
concurrency_limit: int) -> None:
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
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
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
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
@asynccontextmanager
async def throttle(self) -> None:
await self.semaphore.acquire()
await self.add_token()
try:
yield
finally:
self.semaphore.release()
async def throttle(self) ->
async with self.semaphore:
await self.produce_quota()