wick_invocation_server/
lib.rs

1//! Wick RPC SDK
2
3// !!START_LINTS
4// Wick lints
5// Do not change anything between the START_LINTS and END_LINTS line.
6// This is automatically generated. Add exceptions after this section.
7#![allow(unknown_lints)]
8#![deny(
9  clippy::await_holding_lock,
10  clippy::borrow_as_ptr,
11  clippy::branches_sharing_code,
12  clippy::cast_lossless,
13  clippy::clippy::collection_is_never_read,
14  clippy::cloned_instead_of_copied,
15  clippy::cognitive_complexity,
16  clippy::create_dir,
17  clippy::deref_by_slicing,
18  clippy::derivable_impls,
19  clippy::derive_partial_eq_without_eq,
20  clippy::equatable_if_let,
21  clippy::exhaustive_structs,
22  clippy::expect_used,
23  clippy::expl_impl_clone_on_copy,
24  clippy::explicit_deref_methods,
25  clippy::explicit_into_iter_loop,
26  clippy::explicit_iter_loop,
27  clippy::filetype_is_file,
28  clippy::flat_map_option,
29  clippy::format_push_string,
30  clippy::fn_params_excessive_bools,
31  clippy::future_not_send,
32  clippy::get_unwrap,
33  clippy::implicit_clone,
34  clippy::if_then_some_else_none,
35  clippy::impl_trait_in_params,
36  clippy::implicit_clone,
37  clippy::inefficient_to_string,
38  clippy::inherent_to_string,
39  clippy::iter_not_returning_iterator,
40  clippy::large_types_passed_by_value,
41  clippy::large_include_file,
42  clippy::let_and_return,
43  clippy::manual_assert,
44  clippy::manual_ok_or,
45  clippy::manual_split_once,
46  clippy::manual_let_else,
47  clippy::manual_string_new,
48  clippy::map_flatten,
49  clippy::map_unwrap_or,
50  clippy::missing_enforced_import_renames,
51  clippy::missing_assert_message,
52  clippy::missing_const_for_fn,
53  clippy::must_use_candidate,
54  clippy::mut_mut,
55  clippy::needless_for_each,
56  clippy::needless_option_as_deref,
57  clippy::needless_pass_by_value,
58  clippy::needless_collect,
59  clippy::needless_continue,
60  clippy::non_send_fields_in_send_ty,
61  clippy::nonstandard_macro_braces,
62  clippy::option_if_let_else,
63  clippy::option_option,
64  clippy::rc_mutex,
65  clippy::redundant_else,
66  clippy::same_name_method,
67  clippy::semicolon_if_nothing_returned,
68  clippy::str_to_string,
69  clippy::string_to_string,
70  clippy::too_many_lines,
71  clippy::trivially_copy_pass_by_ref,
72  clippy::trivial_regex,
73  clippy::try_err,
74  clippy::unnested_or_patterns,
75  clippy::unused_async,
76  clippy::unwrap_or_else_default,
77  clippy::useless_let_if_seq,
78  bad_style,
79  clashing_extern_declarations,
80  dead_code,
81  deprecated,
82  explicit_outlives_requirements,
83  improper_ctypes,
84  invalid_value,
85  missing_copy_implementations,
86  missing_debug_implementations,
87  mutable_transmutes,
88  no_mangle_generic_items,
89  non_shorthand_field_patterns,
90  overflowing_literals,
91  path_statements,
92  patterns_in_fns_without_body,
93  private_in_public,
94  trivial_bounds,
95  trivial_casts,
96  trivial_numeric_casts,
97  type_alias_bounds,
98  unconditional_recursion,
99  unreachable_pub,
100  unsafe_code,
101  unstable_features,
102  unused,
103  unused_allocation,
104  unused_comparisons,
105  unused_import_braces,
106  unused_parens,
107  unused_qualifications,
108  while_true,
109  missing_docs
110)]
111#![warn(clippy::exhaustive_enums)]
112#![allow(unused_attributes, clippy::derive_partial_eq_without_eq, clippy::box_default)]
113// !!END_LINTS
114// Add exceptions here
115#![allow()]
116
117/// Error module.
118pub mod error;
119
120mod invocation_server;
121
122use flow_component::SharedComponent;
123pub use invocation_server::InvocationServer;
124use tokio::task::JoinHandle;
125use tonic::transport::{Channel, Server, Uri};
126use wick_rpc::rpc::invocation_service_client::InvocationServiceClient;
127use wick_rpc::rpc::invocation_service_server::InvocationServiceServer;
128
129pub(crate) type Result<T> = std::result::Result<T, Error>;
130
131/// The crate's error type.
132pub use crate::error::Error;
133
134/// Exported type so consumers don't need to depend on tonic directly.
135pub type InvocationClient = InvocationServiceClient<Channel>;
136
137#[macro_use]
138extern crate tracing;
139
140/// Build and spawn an RPC server for the passed collection.
141#[must_use]
142pub fn make_rpc_server(
143  socket: tokio::net::TcpSocket,
144  collection: SharedComponent,
145) -> JoinHandle<std::result::Result<(), tonic::transport::Error>> {
146  let component_service = InvocationServer::new(collection);
147
148  let svc = InvocationServiceServer::new(component_service);
149
150  let listener = tokio_stream::wrappers::TcpListenerStream::new(socket.listen(1).unwrap());
151
152  tokio::spawn(Server::builder().add_service(svc).serve_with_incoming(listener))
153}
154
155/// Create an RPC client.
156pub async fn connect_rpc_client(uri: Uri) -> Result<InvocationServiceClient<Channel>> {
157  Ok(InvocationServiceClient::connect(uri).await?)
158}
159
160#[doc(hidden)]
161pub fn bind_new_socket() -> Result<tokio::net::TcpSocket> {
162  let socket = tokio::net::TcpSocket::new_v4()?;
163  socket.bind("127.0.0.1:0".parse().unwrap())?;
164  Ok(socket)
165}