Skip to content

Commit

Permalink
Fix unregisterService and add ssl=False
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Aug 25, 2024
1 parent e4850b4 commit 32dc2c7
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 20 deletions.
4 changes: 2 additions & 2 deletions javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hypha-rpc",
"version": "0.20.29",
"version": "0.20.30",
"description": "Hypha RPC client for connecting to Hypha server for data management and AI model serving.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
27 changes: 20 additions & 7 deletions javascript/src/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,21 +766,34 @@ export class RPC extends MessageEmitter {
}

async unregister_service(service, notify) {
if (service instanceof Object) {
service = service.id;
notify = notify === undefined ? true : notify;
let service_id;
if (typeof service === "string") {
service_id = service;
} else {
service_id = service.id;
}
assert(
service_id && typeof service_id === "string",
`Invalid service id: ${service_id}`,
);
if (service_id.includes(":")) {
service_id = service_id.split(":")[1];
}
if (service_id.includes("@")) {
service_id = service_id.split("@")[0];
}
if (!this._services[service]) {
throw new Error(`Service not found: ${service}`);
if (!this._services[service_id]) {
throw new Error(`Service not found: ${service_id}`);
}
const api = this._services[service];
delete this._services[service];
if (notify) {
const manager = await this.get_manager_service({
timeout: 10,
case_conversion: "camel",
});
await manager.registerService(api.id);
await manager.unregisterService(service_id);
}
delete this._services[service_id];
}

_ndarray(typedArray, shape, dtype) {
Expand Down
17 changes: 17 additions & 0 deletions javascript/tests/websocket_client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ describe("RPC", async () => {
});
expect(api.config.hypha_version).to.be.a("string");
expect(typeof api.log).to.equal("function");
const svc_info = await api.registerService({
id: "test-service",
config: {
visibility: "public",
},
type: "echo",
echo: (x) => x,
});

const svc = await api.getService("test-service");
expect(await svc.echo("hello")).to.equal("hello");
await api.unregisterService(svc_info.id);
try {
await api.getService("test-service");
} catch (e) {
expect(e.message).to.include("Service not found");
}
await api.disconnect();
}).timeout(20000);

Expand Down
2 changes: 1 addition & 1 deletion python/hypha_rpc/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.20.29"
"version": "0.20.30"
}
22 changes: 15 additions & 7 deletions python/hypha_rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import weakref
from collections import OrderedDict
from functools import partial, reduce
from typing import Union
from .utils import ObjectProxy, DefaultObjectProxy

import msgpack
Expand Down Expand Up @@ -734,18 +735,25 @@ async def register_service(
raise Exception(f"Failed to notify workspace manager: {exp}")
return service_info

async def unregister_service(self, service: dict, notify: bool = True):
async def unregister_service(self, service: Union[dict, str], notify: bool = True):
"""Register a service."""
if isinstance(service, str):
service = self._services.get(service)
if service["id"] not in self._services:
raise Exception(f"Service not found: {service.get('id')}")
del self._services[service["id"]]
if isinstance(service, dict):
service_id = service["id"]
else:
service_id = service
assert isinstance(service_id, str), f"Invalid service id: {service_id}"
if ":" in service_id:
service_id = service_id.split(":")[1]
if "@" in service_id:
service_id = service_id.split("@")[0]
if service_id not in self._services:
raise Exception(f"Service not found: {service_id}")
if notify:
manager = await self.get_manager_service(
{"timeout": 10, "case_conversion": "snake"}
)
await manager.unregister_service(service.id)
await manager.unregister_service(service_id)
del self._services[service_id]

def check_modules(self):
"""Check if all the modules exists."""
Expand Down
9 changes: 9 additions & 0 deletions python/hypha_rpc/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def __init__(
self._legacy_auth = None
self.connection_info = None
self._enable_reconnect = False
if ssl == False:
import ssl as ssl_module

ssl = ssl_module.create_default_context()
ssl.check_hostname = False
ssl.verify_mode = ssl_module.CERT_NONE
logger.warning(
"SSL is disabled, this is not recommended for production use."
)
self._ssl = ssl
self.manager_id = None

Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "hypha_rpc"
version = "0.20.29"
version = "0.20.30"
description = "Hypha RPC client for connecting to Hypha server for data management and AI model serving"
readme = "README.md"
requires-python = ">=3.6"
Expand Down
11 changes: 10 additions & 1 deletion python/tests/test_websocket_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def hello(name, key=12, context=None):
assert svc.hello.__doc__ == hello.__doc__
assert svc.hello.__name__ == hello.__name__

await ws.register_service(
svc_info = await ws.register_service(
{
"name": "Hello World",
"id": "hello-world",
Expand All @@ -276,6 +276,15 @@ def hello(name, key=12, context=None):
assert svc.hello.__doc__ == hello.__doc__
assert svc.hello.__name__ == hello.__name__

await ws.unregister_service(svc_info["id"])

try:
svc = await ws.get_service("hello-world")
except Exception as e:
assert "Service not found" in str(e)

await ws.disconnect()


@pytest.mark.asyncio
async def test_case_conversion(websocket_server):
Expand Down

0 comments on commit 32dc2c7

Please sign in to comment.