Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devkelley/standardize extension model #43

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ members = [
"core/invehicle-digital-twin",

# extension
"core/extension",
"core/module/managed_subscribe",

# digital twin model
"digital-twin-model",
Expand Down
5 changes: 4 additions & 1 deletion core/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ license = "MIT"

[dependencies]
bytes = { workspace = true }
config = { workspace = true }
core-protobuf-data-access = { path = "../protobuf_data_access" }
dyn-clone = { workspace = true }
futures = { workspace = true }
futures-core = { workspace = true }
Expand All @@ -20,7 +22,8 @@ hyper = { workspace = true }
log = { workspace = true }
parking_lot = { workspace = true }
prost = { workspace = true }
core-protobuf-data-access = { path = "../protobuf_data_access" }
serde = { workspace = true, features = ["derive"] }
serde_derive = { workspace = true }
regex = {workspace = true }
tokio = { workspace = true }
tonic = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions core/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ These documents/code were very helpful in developing this solution:
<li> https://github.com/linkerd/linkerd2-proxy/blob/0814a154ba8c8cc7af394ac3fa6f940bd01755ae/linkerd/stack/src/fail_on_error.rs#LL30-L69C2
</ul>

## gRPC Service
## gRPC Module

gRPC Service is a concept that allows for a service that implements it to add its gRPC services to a builder which is used to construct a gRPC server that hosts the services.
gRPC Module is a concept that allows for an object that implements it to add its gRPC services to a builder which is used to construct a gRPC server that hosts the services.

gRPC Service relies on the Tonic crate's RoutesBuilder construct to add the gRPC services when building the gRPC server instance.
gRPC Module relies on the Tonic crate's RoutesBuilder construct to add the gRPC services when building the gRPC server instance.

## Sample gRPC Interceptor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use tonic::transport::server::RoutesBuilder;

/// Trait that must be implemented for an extension to add a grpc service to the hosted server.
/// Note: This trait may be renamed in the future.
pub trait GrpcExtension {
/// Trait that must be implemented for a module to add one or more grpc services to the hosted
/// server.
pub trait GrpcModule {
/// Function to add necessary services to the server builder.
///
/// # Arguments
Expand Down
59 changes: 59 additions & 0 deletions core/common/src/grpc_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::net::SocketAddr;

use tonic::transport::server::Router;
use tonic::transport::{server::RoutesBuilder, Server};
use tower::layer::util::{Identity, Stack};
use tower::ServiceBuilder;

use crate::grpc_module::GrpcModule;

/// Grpc Server struct that builds multiple services and layers.
pub struct GrpcServer<L> {
address: SocketAddr,
pub modules: RoutesBuilder,
pub middleware: ServiceBuilder<L>,
}

impl GrpcServer<Identity> {
/// Creates a new GrpcServer
///
/// # Arguments
/// * `address` - The address the server will be hosted on.
pub fn new(address: SocketAddr) -> Self {
GrpcServer { address, modules: RoutesBuilder::default(), middleware: ServiceBuilder::new() }
}
}

impl<L> GrpcServer<L> {
/// Adds a module (collection of grpc services and interceptors) for the server to host.
/// Returns a newly decorated GrpcServer with the added module.
///
/// # Arguments
/// * `middleware` - The middleware from the current server + any interceptors for the module
/// added with `.layer()`.
/// * `module` - Boxed GrpcModule to be added to the server.
pub fn add_module<S>(
&mut self,
middleware: ServiceBuilder<S>,
module: Box<dyn GrpcModule>,
) -> GrpcServer<S> {
module.add_grpc_services(&mut self.modules);

GrpcServer { address: self.address, modules: self.modules.clone(), middleware }
}

/// Constructs the added modules and layers into a server to host.
pub fn construct_server(&self) -> Router<Stack<L, Identity>>
where
L: Clone,
{
// Construct the server.
Server::builder()
.layer(self.middleware.clone().into_inner())
.add_routes(self.modules.clone().routes())
}
}
3 changes: 2 additions & 1 deletion core/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

pub mod grpc_extension;
pub mod grpc_interceptor;
pub mod grpc_module;
pub mod grpc_server;
pub mod sample_grpc_interceptor;
pub mod utils;
20 changes: 18 additions & 2 deletions core/common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::future::Future;

use config::{Config, File, FileFormat};
use log::debug;
use std::future::Future;
use tokio::time::{sleep, Duration};

/// Load the settings.
///
/// # Arguments
/// * `config_filename` - Name of the config file to load settings from.
pub fn load_settings<T>(config_filename: &str) -> T
where
T: for<'de> serde::Deserialize<'de>,
{
let config =
Config::builder().add_source(File::new(config_filename, FileFormat::Yaml)).build().unwrap();

let settings: T = config.try_deserialize().unwrap();

settings
}

/// Retry a function that returns an error.
///
/// # Arguments
Expand Down
88 changes: 0 additions & 88 deletions core/extension/src/extension.rs

This file was deleted.

21 changes: 0 additions & 21 deletions core/extension/src/extension_config.rs

This file was deleted.

15 changes: 0 additions & 15 deletions core/extension/src/lib.rs

This file was deleted.

6 changes: 3 additions & 3 deletions core/invehicle-digital-twin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ http = { workspace = true }
iref = { workspace = true }
log = { workspace = true }
common = { path = "../common" }
core-extension = { path = "../extension", features = ["managed_subscribe"], optional = true }
managed_subscribe = { path = "../module/managed_subscribe", optional = true }
parking_lot = { workspace = true }
prost = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand All @@ -37,6 +37,6 @@ yaml-rust = { workspace = true, optional = true }
tonic-build = { workspace = true }

[features]
default = ["extension", "yaml"]
extension = ["core-extension"]
default = ["managed_subscribe", "yaml"]
managed_subscribe = ["dep:managed_subscribe"]
yaml = ["yaml-rust"]
Loading