Skip to content

Commit f5acb81

Browse files
ahilgerfacebook-github-bot
authored andcommitted
bind ServerModule
Summary: A basic cython bindings, which makes it possible to install thrift `ServerModule` into `ThriftServer` from Python. This is an easy pre-req for adding `ServiceInterceptor` to `ThriftServer` from Python. See next diff Reviewed By: prakashgayasen Differential Revision: D75273370 fbshipit-source-id: e0d7d3347a3cae4814eb00103f6b188e8ce8f782
1 parent bee4dac commit f5acb81

File tree

8 files changed

+139
-0
lines changed

8 files changed

+139
-0
lines changed

third-party/thrift/src/thrift/lib/py3/server.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ from thrift.python.server_impl.async_processor cimport (
3333
cAsyncProcessorFactory,
3434
AsyncProcessorFactory as Py3AsyncProcessorFactory,
3535
)
36+
from thrift.python.server_impl.interceptor.server_module cimport cServerModule
3637

3738

3839
cdef extern from "thrift/lib/cpp2/server/StatusServerInterface.h" \
@@ -94,6 +95,7 @@ cdef extern from "thrift/lib/cpp2/server/ThriftServer.h" \
9495
void requireResourcePools() nogil noexcept
9596
void setTaskExpireTime(milliseconds timeout)
9697
void setUseClientTimeout(cbool useClientTimeout)
98+
void addModule(unique_ptr[cServerModule] module)
9799

98100
cdef extern from "folly/ssl/OpenSSLCertUtils.h" \
99101
namespace "folly::ssl":

third-party/thrift/src/thrift/lib/py3/server.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ from typing import (
3535
)
3636

3737
from thrift.py3.common import Headers, Priority
38+
from thrift.python.server_impl.interceptor.server_module import PythonServerModule
3839
from thrift.python.types import ServiceInterface as PythonServiceInterface
3940

4041
# pyre-ignore[33]: the callable can be any function
@@ -127,6 +128,7 @@ class ThriftServer:
127128
def is_resource_pool_enabled(self) -> bool: ...
128129
def set_task_expire_time(self, seconds: float) -> None: ...
129130
def set_use_client_timeout(self, use_client_timeout: bool) -> None: ...
131+
def add_server_module(self, module: PythonServerModule) -> None: ...
130132

131133
class ReadHeaders(Headers): ...
132134
class WriteHeaders(Headers): ...

third-party/thrift/src/thrift/lib/py3/server.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ from thrift.python.server_impl.request_context import ( # noqa
5252
get_context,
5353
)
5454
from thrift.python.server_impl.request_context cimport handleAddressCallback
55+
from thrift.python.server_impl.interceptor.server_module cimport PythonServerModule
5556

5657
AsyncProcessorFactory = AsyncProcessorFactory_
5758

@@ -292,6 +293,13 @@ cdef class ThriftServer:
292293
def set_use_client_timeout(self, cbool use_client_timeout):
293294
self.server.get().setUseClientTimeout(use_client_timeout)
294295

296+
def add_server_module(self, PythonServerModule module):
297+
# this is a dumb hack around cython not understanding that
298+
# unique_ptr[Base] = unique_ptr[Derived] is valid and safe
299+
self.server.get().addModule(
300+
unique_ptr[[cServerModule]](module._cpp_module.release())
301+
)
302+
295303
@property
296304
def handler(self):
297305
if self.handler is not None:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <thrift/lib/cpp2/server/ServerModule.h>
20+
21+
namespace apache::thrift::python {
22+
23+
class PythonServerModule : public apache::thrift::ServerModule {
24+
public:
25+
explicit PythonServerModule(std::string name) : name_(std::move(name)) {}
26+
27+
std::string getName() const override { return name_; }
28+
29+
std::vector<std::shared_ptr<apache::thrift::ServiceInterceptorBase>>
30+
getServiceInterceptors() override {
31+
return interceptors_;
32+
}
33+
34+
void addServiceInterceptor(
35+
std::shared_ptr<apache::thrift::ServiceInterceptorBase> interceptor) {
36+
interceptors_.push_back(std::move(interceptor));
37+
}
38+
39+
private:
40+
std::string name_;
41+
std::vector<std::shared_ptr<apache::thrift::ServiceInterceptorBase>>
42+
interceptors_;
43+
};
44+
} // namespace apache::thrift::python
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from libcpp.string cimport string
17+
from libcpp.vector cimport vector
18+
from libcpp.memory cimport shared_ptr, unique_ptr
19+
20+
21+
cdef extern from "thrift/lib/cpp2/server/ServiceInterceptorBase.h" namespace "apache::thrift":
22+
cdef cppclass ServiceInterceptorBase:
23+
pass
24+
25+
cdef extern from "thrift/lib/cpp2/server/ServerModule.h" namespace "apache::thrift":
26+
cdef cppclass cServerModule "apache::thrift::ServerModule":
27+
ServerModule() except +
28+
string getName() const
29+
vector[shared_ptr[ServiceInterceptorBase]] getServiceInterceptors()
30+
31+
cdef extern from "thrift/lib/python/server/interceptor/PythonServerModule.h":
32+
cdef cppclass cPythonServerModule "apache::thrift::python::PythonServerModule"(cServerModule):
33+
PythonServerModule(string name)
34+
void addServiceInterceptor(shared_ptr[ServiceInterceptorBase])
35+
36+
37+
cdef class PythonServerModule:
38+
cdef unique_ptr[cPythonServerModule] _cpp_module
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
class PythonServerModule:
16+
def __init__(self, name: str) -> None: ...
17+
def add_service_interceptor(self, interceptor) -> None: ...
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from libcpp.memory cimport make_unique
16+
from libcpp.string cimport string
17+
from libcpp.utility cimport move as cmove
18+
19+
20+
21+
cdef class PythonServerModule:
22+
def __init__(self, str name):
23+
self._cpp_module = make_unique[cPythonServerModule](<string>name.encode("utf-8"))
24+
25+
def add_service_interceptor(self, interceptor):
26+
# impl added next diff
27+
pass

third-party/thrift/src/thrift/lib/python/server/server.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ from thrift.python.server_impl.python_async_processor import (
3434
PythonUserException,
3535
RpcKind,
3636
)
37+
from thrift.python.server_impl.interceptor.server_module import PythonServerModule

0 commit comments

Comments
 (0)