44from __future__ import annotations
55
66import json
7+ import time
78from collections .abc import Mapping
89from typing import TYPE_CHECKING , Any , Optional
910
1213from .._client import Client , _REQUEST_ID_HEADER
1314from ..auth import Auth
1415from ..exceptions import VikingException
15- from .exceptions import VikingVectorException
16+ from .exceptions import VikingVectorException , VikingConnectionException
1617from ..request_options import RequestOptions , ensure_request_options
1718from ..version import __version__
1819from .models import CollectionMeta , IndexMeta
@@ -64,6 +65,12 @@ def __init__(
6465 scheme = scheme ,
6566 timeout = timeout ,
6667 )
68+ try :
69+ resp = self .session .get (f"{ scheme } ://{ host } /api/vikingdb/Ping" )
70+ if resp .status_code != 200 :
71+ raise VikingConnectionException (f"failed to ping { host } " , f"{ resp .status_code } " )
72+ except Exception as exp :
73+ raise VikingConnectionException (f"failed to ping { host } " , str (exp ))
6774
6875 def collection (
6976 self ,
@@ -112,6 +119,13 @@ def request(
112119 options : Optional [RequestOptions ] = None ,
113120 ) -> Mapping [str , object ]:
114121 request_options = ensure_request_options (options )
122+ max_attempts = (
123+ request_options .max_attempts
124+ if request_options .max_attempts and request_options .max_attempts > 0
125+ else 3
126+ )
127+ initial_delay_seconds = 0.5
128+ max_delay_seconds = 8.0
115129 headers = {
116130 "Accept" : "application/json" ,
117131 "Content-Type" : "application/json" ,
@@ -124,10 +138,26 @@ def request(
124138
125139 body = json .dumps (payload , ensure_ascii = False , separators = ("," , ":" ))
126140 params = dict (request_options .query ) if request_options .query else None
127- response_data = self .json_exception (api , params , body , headers = headers , timeout = request_options .timeout )
128- if not response_data :
129- return {}
130- return response_data
141+ for attempt in range (1 , max_attempts + 1 ):
142+ try :
143+ response_data = self .json_exception (
144+ api ,
145+ params ,
146+ body ,
147+ headers = headers ,
148+ timeout = request_options .timeout ,
149+ )
150+ if not response_data :
151+ return {}
152+ return response_data
153+ except Exception :
154+ if attempt >= max_attempts :
155+ raise
156+ delay = min (
157+ initial_delay_seconds * (2 ** (attempt - 1 )),
158+ max_delay_seconds ,
159+ )
160+ time .sleep (delay )
131161
132162 def json_exception (
133163 self ,
0 commit comments