Skip to content

Commit

Permalink
all for darabonba v2
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 authored and TsinghuaDream committed Dec 11, 2024
1 parent e3a66b0 commit 233a816
Show file tree
Hide file tree
Showing 41 changed files with 1,558 additions and 54 deletions.
30 changes: 0 additions & 30 deletions Tea/decorators.py

This file was deleted.

File renamed without changes.
15 changes: 8 additions & 7 deletions Tea/core.py → darabonba/core.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import asyncio
import aiohttp
import logging
import os
import ssl
import time
from typing import Any, Dict, Optional
from urllib.parse import urlencode, urlparse

import aiohttp
import certifi
from requests import PreparedRequest, adapters, status_codes
from requests import status_codes, adapters, PreparedRequest

from darabonba.exceptions import RequiredArgumentException, RetryError
from darabonba.model import TeaModel
from darabonba.request import TeaRequest
from darabonba.response import TeaResponse
from darabonba.stream import BaseStream

from Tea.exceptions import RequiredArgumentException, RetryError
from Tea.model import TeaModel
from Tea.request import TeaRequest
from Tea.response import TeaResponse
from Tea.stream import BaseStream

DEFAULT_CONNECT_TIMEOUT = 5000
DEFAULT_READ_TIMEOUT = 10000
Expand Down
62 changes: 62 additions & 0 deletions darabonba/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import warnings
import functools


def deprecated(reason):
"""This is a decorator which can be used to mark functions as deprecated.
It will result in a warning being emitted when the function is used.
Args:
reason (str): Explanation of why the function is deprecated.
"""

def decorator(func):
original_func = func.__func__ if isinstance(func, staticmethod) or isinstance(func, classmethod) else func

@functools.wraps(original_func)
def decorated_function(*args, **kwargs):
warnings.warn(f"Call to deprecated function {original_func.__name__}. {reason}",
category=DeprecationWarning,
stacklevel=2)
return original_func(*args, **kwargs)

if isinstance(func, staticmethod):
return staticmethod(decorated_function)
elif isinstance(func, classmethod):
return classmethod(decorated_function)
else:
return decorated_function

return decorator


def type_check(*arg_types, **kwarg_types):
"""This decorator is used to check whether the input parameter type meets the definition.
It will result in a warning being emitted when the function is used.
"""

def decorator(func):
original_func = func.__func__ if isinstance(func, staticmethod) or isinstance(func, classmethod) else func

@functools.wraps(original_func)
def wrapper(*args, **kwargs):
for i, (a, t) in enumerate(zip(args, arg_types)):
if not isinstance(a, t):
warnings.warn(f"Argument {i} is not of type {t}",
category=UserWarning,
stacklevel=2)
for k, t in kwarg_types.items():
if k in kwargs and not isinstance(kwargs[k], t):
warnings.warn(f"Argument {k} is not of type {t}",
category=UserWarning,
stacklevel=2)
return original_func(*args, **kwargs)

if isinstance(func, staticmethod):
return staticmethod(wrapper)
elif isinstance(func, classmethod):
return classmethod(wrapper)
else:
return wrapper

return decorator
2 changes: 1 addition & 1 deletion Tea/exceptions.py → darabonba/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .request import TeaRequest
from darabonba.request import TeaRequest


class TeaException(Exception):
Expand Down
2 changes: 1 addition & 1 deletion Tea/model.py → darabonba/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from .exceptions import RequiredArgumentException, ValidateException
from darabonba.exceptions import RequiredArgumentException, ValidateException


class TeaModel:
Expand Down
Empty file added darabonba/policy/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions darabonba/policy/backoff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import random
from .retry import RetryCondition


class BackoffStrategy(object):

def compute_delay_before_next_retry(self, retry_policy_context):
"""Compute delay for request need to be retried, in milliseconds"""
pass


class FixedDelayStrategy(BackoffStrategy):

def __init__(self, fixed_delay):
self.fixed_delay = fixed_delay

def compute_delay_before_next_retry(self, retry_policy_context):
return self.fixed_delay


class NoDelayStrategy(FixedDelayStrategy):

def __init__(self):
FixedDelayStrategy.__init__(self, 0)


class ExponentialBackoffStrategy(BackoffStrategy):

MAX_RETRY_LIMIT = 30 # to avoid integer overflow during delay calculation

def __init__(self, base_delay_in_milliseconds, max_delay_in_milliseconds):
self.base_delay_in_milliseconds = base_delay_in_milliseconds
self.max_delay_in_milliseconds = max_delay_in_milliseconds

def compute_delay_before_next_retry(self, retry_policy_context):
retries = min(self.MAX_RETRY_LIMIT, retry_policy_context.retries_attempted)
delay = min(self.max_delay_in_milliseconds, self.base_delay_in_milliseconds << retries)
return delay


class JitteredExponentialBackoffStrategy(ExponentialBackoffStrategy):

def compute_delay_before_next_retry(self, retry_policy_context):
delay = ExponentialBackoffStrategy.compute_delay_before_next_retry(self,
retry_policy_context)
return delay / 2 + random.randint(0, int(delay / 2))


class DefaultMixedBackoffStrategy(BackoffStrategy):

# in milliseconds
SDK_DEFAULT_BASE_DELAY = 100
SDK_DEFAULT_TROTTLED_BASE_DELAY = 500
SDK_DEFAULT_MAX_BACKOFF = 20 * 1000

def __init__(self):
self._default_backoff_strategy = ExponentialBackoffStrategy(
self.SDK_DEFAULT_BASE_DELAY,
self.SDK_DEFAULT_MAX_BACKOFF
)
self._default_throttled_backoff_strategy = JitteredExponentialBackoffStrategy(
self.SDK_DEFAULT_TROTTLED_BASE_DELAY,
self.SDK_DEFAULT_MAX_BACKOFF
)

def compute_delay_before_next_retry(self, retry_policy_context):
retryable = retry_policy_context.retryable
if retryable & RetryCondition.SHOULD_RETRY_WITH_THROTTLING_BACKOFF:
return self._default_throttled_backoff_strategy.compute_delay_before_next_retry(
retry_policy_context)
else:
return self._default_backoff_strategy.compute_delay_before_next_retry(
retry_policy_context)
Loading

0 comments on commit 233a816

Please sign in to comment.