Skip to content

Commit

Permalink
Use RPC health check instead of /health for is_connected (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
mp-freatic authored Nov 10, 2024
1 parent 890cbbb commit d1ae42f
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 34 deletions.
5 changes: 4 additions & 1 deletion src/solana/rpc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
GetFeeForMessageResp,
GetFirstAvailableBlockResp,
GetGenesisHashResp,
GetHealthResp,
GetIdentityResp,
GetInflationGovernorResp,
GetInflationRateResp,
Expand Down Expand Up @@ -112,7 +113,9 @@ def is_connected(self) -> bool:
Returns:
True if the client is connected.
"""
return self._provider.is_connected()
body = self._get_health_body()
response = self._provider.make_request(body, GetHealthResp)
return response.value == "ok"

def get_balance(self, pubkey: Pubkey, commitment: Optional[Commitment] = None) -> GetBalanceResp:
"""Returns the balance of the account of provided Pubkey.
Expand Down
5 changes: 4 additions & 1 deletion src/solana/rpc/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
GetFeeForMessageResp,
GetFirstAvailableBlockResp,
GetGenesisHashResp,
GetHealthResp,
GetIdentityResp,
GetInflationGovernorResp,
GetInflationRateResp,
Expand Down Expand Up @@ -120,7 +121,9 @@ async def is_connected(self) -> bool:
Returns:
True if the client is connected.
"""
return await self._provider.is_connected()
body = self._get_health_body()
response = await self._provider.make_request(body, GetHealthResp)
return response.value == "ok"

async def get_balance(self, pubkey: Pubkey, commitment: Optional[Commitment] = None) -> GetBalanceResp:
"""Returns the balance of the account of provided Pubkey.
Expand Down
7 changes: 5 additions & 2 deletions src/solana/rpc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
GetFeeForMessage,
GetFirstAvailableBlock,
GetGenesisHash,
GetHealth,
GetIdentity,
GetInflationGovernor,
GetInflationRate,
Expand Down Expand Up @@ -77,11 +78,10 @@
)
from solders.rpc.responses import GetLatestBlockhashResp, SendTransactionResp
from solders.signature import Signature
from solders.transaction import VersionedTransaction
from solders.transaction import Transaction, VersionedTransaction
from solders.transaction_status import UiTransactionEncoding

from solana.rpc import types
from solders.transaction import Transaction

from .commitment import Commitment, Confirmed, Finalized, Processed

Expand Down Expand Up @@ -157,6 +157,9 @@ def commitment(self) -> Commitment:
"""The default commitment used for requests."""
return self._commitment

def _get_health_body(self) -> GetHealth:
return GetHealth()

def _get_balance_body(self, pubkey: Pubkey, commitment: Optional[Commitment]) -> GetBalance:
commitment_to_use = _COMMITMENT_TO_SOLDERS[commitment or self._commitment]
return GetBalance(pubkey, RpcContextConfig(commitment=commitment_to_use))
Expand Down
4 changes: 0 additions & 4 deletions src/solana/rpc/providers/async_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,3 @@ class AsyncBaseProvider:
async def make_request(self, body: Body, parser: Type[T]) -> T:
"""Make a request ot the rpc endpoint."""
raise NotImplementedError("Providers must implement this method")

async def is_connected(self) -> bool:
"""Health check."""
raise NotImplementedError("Providers must implement this method")
11 changes: 0 additions & 11 deletions src/solana/rpc/providers/async_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ async def make_batch_request(self, reqs: Tuple[Body, ...], parsers: _Tuples) ->
raw = await self.make_batch_request_unparsed(reqs)
return _parse_raw_batch(raw, parsers)

async def is_connected(self) -> bool:
"""Health check."""
try:
response = await self.session.get(self.health_uri)
response.raise_for_status()
except (IOError, httpx.HTTPError) as err:
self.logger.error("Health check failed with error: %s", str(err))
return False

return response.status_code == httpx.codes.OK

async def __aenter__(self) -> "AsyncHTTPProvider":
"""Use as a context manager."""
await self.session.__aenter__()
Expand Down
4 changes: 0 additions & 4 deletions src/solana/rpc/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ class BaseProvider:
def make_request(self, body: Body, parser: Type[T]) -> T:
"""Make a request to the rpc endpoint."""
raise NotImplementedError("Providers must implement this method")

def is_connected(self) -> bool:
"""Health check."""
raise NotImplementedError("Providers must implement this method")
11 changes: 0 additions & 11 deletions src/solana/rpc/providers/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,3 @@ def make_batch_request(self, reqs: Tuple[Body, ...], parsers: _Tuples) -> Tuple[
"""
raw = self.make_batch_request_unparsed(reqs)
return _parse_raw_batch(raw, parsers)

def is_connected(self) -> bool:
"""Health check."""
try:
response = httpx.get(self.health_uri)
response.raise_for_status()
except (IOError, httpx.HTTPError) as err:
self.logger.error("Health check failed with error: %s", str(err))
return False

return response.status_code == httpx.codes.OK

0 comments on commit d1ae42f

Please sign in to comment.