From: MatthieuCoder Date: Fri, 13 Jan 2023 18:35:46 +0000 (+0400) Subject: fix build and code cleanup X-Git-Tag: v0.1.1~38 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=4b55d5ff172f87bfa38ef2a21aceed40aacf9c54;p=matthieu%2Fnova.git fix build and code cleanup --- diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8130198..bb76fee 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -57,29 +57,14 @@ jobs: - uses: ATiltedTree/setup-rust@v1 with: rust-version: stable - - name: Build + - name: Build all run: | - cargo build --release - - name: Test - run: | - cargo test --release - - name: All in one - run: | - cd exes/all export CGO_LDFLAGS="-framework Security -framework CoreFoundation" - make build - - - name: Move artifacts - run: | - mkdir -p artifacts - cp target/release/{cache,gateway,webhook,ratelimit} ./artifacts - cp exes/all/build/all ./artifacts/nova - + make all - uses: actions/upload-artifact@v3 with: name: macos - path: artifacts/* - + path: build/* build_windows: name: 'Build for Windows' @@ -105,28 +90,14 @@ jobs: with: install: git mingw-w64-x86_64-go mingw-w64-x86_64-make mingw-w64-x86_64-protobuf mingw-w64-x86_64-rust mingw-w64-x86_64-gcc mingw-w64-x86_64-dlfcn - - name: Build + - name: Build all run: | - cargo build --release - - name: Test - run: | - cargo test --release - - name: All in one - run: | - cd exes/all export CGO_LDFLAGS='-lntdll -lWs2_32 -lcrypt32 -lSecur32 -luserenv -lNcrypt -lbcrypt' - mingw32-make.exe build - - - name: Move artifacts - run: | - mkdir -p artifacts - cp target/release/{cache,gateway,webhook,ratelimit}.exe ./artifacts - cp exes/all/build/all ./artifacts/nova.exe - + make all - uses: actions/upload-artifact@v3 with: - name: windows - path: artifacts/* + name: macos + path: build/* build_linux: name: 'Build for Linux' @@ -154,27 +125,14 @@ jobs: with: go-version: '1.18.4' - - name: Build - run: | - cargo build --release - - name: Test - run: | - cargo test --release - - name: All in one - run: | - cd exes/all - make build - - - name: Move artifacts + - name: Build all run: | - mkdir -p artifacts - cp target/release/{cache,gateway,webhook,ratelimit} ./artifacts - cp exes/all/build/all ./artifacts/nova - + export CGO_LDFLAGS='-lntdll -lWs2_32 -lcrypt32 -lSecur32 -luserenv -lNcrypt -lbcrypt' + make all - uses: actions/upload-artifact@v3 with: - name: linux_glibc - path: artifacts/* + name: macos + path: build/* release: if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') diff --git a/Makefile b/Makefile index 5008c89..f9352c6 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,17 @@ internal/pkg/all-in-one/all-in-one.h: build/lib/liball_in_one.a build/bin/nova: build/lib/liball_in_one.a internal/pkg/all-in-one/all-in-one.h go build -a -ldflags '-s' -o build/bin/nova cmd/nova/nova.go -all: build/{lib,bin}/nova +all: build/bin/{cache,gateway,ratelimit,rest,webhook} build/bin/nova -.PHONY: all \ No newline at end of file +docker-images: + docker-compose build + +docker-push: + docker-compose push + +rust-test: + cargo test + +test: rust-test + +.PHONY: all docker-images docker-push test rust-test diff --git a/exes/all-in-one/build.rs b/exes/all-in-one/build.rs index 83ca650..25a4b99 100644 --- a/exes/all-in-one/build.rs +++ b/exes/all-in-one/build.rs @@ -22,6 +22,6 @@ fn main() -> Result<(), Box> { }; cbindgen::generate_with_config(crate_dir, config)?.write_to_file(output_file); - + Ok(()) } diff --git a/exes/all-in-one/src/errors.rs b/exes/all-in-one/src/errors.rs index f169fb4..d676e8d 100644 --- a/exes/all-in-one/src/errors.rs +++ b/exes/all-in-one/src/errors.rs @@ -35,7 +35,6 @@ pub fn handle_error(error: anyhow::Error) { error!("Error emitted: {}", stacktrace); if let Some(func) = *val.borrow() { - // Call the error handler unsafe { func( diff --git a/exes/all-in-one/src/ffi.rs b/exes/all-in-one/src/ffi.rs index 49586cf..7a8821f 100644 --- a/exes/all-in-one/src/ffi.rs +++ b/exes/all-in-one/src/ffi.rs @@ -1,3 +1,4 @@ +#![allow(clippy::missing_safety_doc)] use std::{ ffi::{c_char, c_int, CString}, mem::take, @@ -44,16 +45,16 @@ pub extern "C" fn load_config() -> *mut c_char { } #[no_mangle] -pub extern "C" fn stop_instance(instance: *mut AllInOneInstance) { +pub unsafe extern "C" fn stop_instance(instance: *mut AllInOneInstance) { wrap_result(move || { - let mut instance = unsafe { Box::from_raw(instance) }; + let mut instance = Box::from_raw(instance); let handles = take(&mut instance.handles); instance.runtime.block_on(async move { for (name, sender, join) in handles { debug!("Halting component {}", name); let _ = sender .send(()) - .or_else(|_| Err(error!("Component {} is not online", name))); + .map_err(|_| error!("Component {} is not online", name)); match join.await { Ok(_) => {} Err(error) => error!("Task for component {} panic'ed {}", name, error), @@ -69,9 +70,9 @@ pub extern "C" fn stop_instance(instance: *mut AllInOneInstance) { } #[no_mangle] -pub extern "C" fn create_instance(config: *mut c_char) -> *mut AllInOneInstance { +pub unsafe extern "C" fn create_instance(config: *mut c_char) -> *mut AllInOneInstance { wrap_result(move || { - let value = unsafe { CString::from_raw(config) }; + let value = CString::from_raw(config); let json = value.to_str()?; // Main stop signal for this instance @@ -125,7 +126,7 @@ pub extern "C" fn create_instance(config: *mut c_char) -> *mut AllInOneInstance handles.push(start_component::( json, - error_sender.clone(), + error_sender, &runtime, )?); diff --git a/exes/all-in-one/src/lib.rs b/exes/all-in-one/src/lib.rs index 5a41344..74625c0 100644 --- a/exes/all-in-one/src/lib.rs +++ b/exes/all-in-one/src/lib.rs @@ -1,3 +1,3 @@ -pub mod utils; pub mod errors; pub mod ffi; +pub mod utils; diff --git a/exes/all-in-one/src/main.rs b/exes/all-in-one/src/main.rs index e05b853..1e65883 100644 --- a/exes/all-in-one/src/main.rs +++ b/exes/all-in-one/src/main.rs @@ -1,14 +1,13 @@ use all_in_one::ffi::{create_instance, load_config, stop_instance}; use std::sync::mpsc::channel; -use ctrlc; fn main() { let c = load_config(); - let comp = create_instance(c); + let comp = unsafe { create_instance(c) }; // wait for signal let (tx, rx) = channel(); - + ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel.")) .expect("Error setting Ctrl-C handler"); @@ -16,5 +15,5 @@ fn main() { println!("Exiting."); - stop_instance(comp); -} \ No newline at end of file + unsafe { stop_instance(comp) }; +} diff --git a/exes/ratelimit/src/buckets/atomic_instant.rs b/exes/ratelimit/src/buckets/atomic_instant.rs index ec31808..67dd0ee 100644 --- a/exes/ratelimit/src/buckets/atomic_instant.rs +++ b/exes/ratelimit/src/buckets/atomic_instant.rs @@ -16,7 +16,7 @@ impl AtomicInstant { SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() - .as_millis() as u64 as u64 + .as_millis() as u64 - self.0.load(Ordering::SeqCst), ) } diff --git a/exes/ratelimit/src/buckets/bucket.rs b/exes/ratelimit/src/buckets/bucket.rs index f4a9b43..0d92881 100644 --- a/exes/ratelimit/src/buckets/bucket.rs +++ b/exes/ratelimit/src/buckets/bucket.rs @@ -84,7 +84,9 @@ impl Bucket { } this.remaining.fetch_sub(1, Ordering::Relaxed); - let _ = element.send(()).map_err(|_| { debug!("response channel was closed.") }); + let _ = element + .send(()) + .map_err(|_| debug!("response channel was closed.")); } }); } @@ -123,7 +125,7 @@ impl Bucket { TimeRemaining::Some(Duration::from_millis(reset_after) - elapsed) } else { - return TimeRemaining::NotStarted; + TimeRemaining::NotStarted } } diff --git a/exes/ratelimit/src/buckets/mod.rs b/exes/ratelimit/src/buckets/mod.rs index e2f52c8..c86d623 100644 --- a/exes/ratelimit/src/buckets/mod.rs +++ b/exes/ratelimit/src/buckets/mod.rs @@ -1,4 +1,4 @@ +pub mod async_queue; +pub mod atomic_instant; pub mod bucket; pub mod redis_lock; -pub mod atomic_instant; -pub mod async_queue; \ No newline at end of file diff --git a/exes/ratelimit/src/grpc.rs b/exes/ratelimit/src/grpc.rs index 0819e46..09885c1 100644 --- a/exes/ratelimit/src/grpc.rs +++ b/exes/ratelimit/src/grpc.rs @@ -88,7 +88,10 @@ impl Ratelimiter for RLServer { // If we are globally ratelimited, we lock using the redis lock // This is using redis because a global ratelimit should be executed in all // ratelimit workers. - debug!("global ratelimit headers detected: {}", global.retry_after()); + debug!( + "global ratelimit headers detected: {}", + global.retry_after() + ); self.global .lock_for(Duration::from_secs(global.retry_after())) .await; diff --git a/exes/ratelimit/src/lib.rs b/exes/ratelimit/src/lib.rs index 5891b58..6653157 100644 --- a/exes/ratelimit/src/lib.rs +++ b/exes/ratelimit/src/lib.rs @@ -6,13 +6,13 @@ use proto::nova::ratelimit::ratelimiter::ratelimiter_server::RatelimiterServer; use redis::aio::MultiplexedConnection; use shared::config::Settings; use std::future::Future; -use std::{pin::Pin}; +use std::pin::Pin; use tokio::sync::oneshot; use tonic::transport::Server; +mod buckets; mod config; mod grpc; -mod buckets; pub struct RatelimiterServerComponent {} impl Component for RatelimiterServerComponent { diff --git a/exes/rest/src/lib.rs b/exes/rest/src/lib.rs index b2287b4..4ec8b51 100644 --- a/exes/rest/src/lib.rs +++ b/exes/rest/src/lib.rs @@ -7,7 +7,7 @@ use hyper::{ Body, Client, Request, Server, }; use leash::{AnyhowResultFuture, Component}; -use opentelemetry::{global}; +use opentelemetry::global; use opentelemetry_http::HeaderExtractor; use shared::config::Settings; use std::{convert::Infallible, sync::Arc}; diff --git a/exes/rest/src/ratelimit_client/mod.rs b/exes/rest/src/ratelimit_client/mod.rs index ecd489c..dc3e970 100644 --- a/exes/rest/src/ratelimit_client/mod.rs +++ b/exes/rest/src/ratelimit_client/mod.rs @@ -104,7 +104,7 @@ impl RemoteRatelimiter { .instrument(trace_span!("acquiring ring lock")) .await .get(&path) - .and_then(|node| Some(node.clone())) + .cloned() .ok_or_else(|| { anyhow!( "did not compute ratelimit because no ratelimiter nodes are detected" @@ -142,7 +142,7 @@ impl RemoteRatelimiter { .instrument(trace_span!("acquiring ring lock")) .await .get(&path) - .and_then(|node| Some(node.clone())) + .cloned() .ok_or_else(|| { anyhow!("did not compute ratelimit because no ratelimiter nodes are detected") })?; diff --git a/libs/shared/src/config.rs b/libs/shared/src/config.rs index 52f6c92..adcaf79 100644 --- a/libs/shared/src/config.rs +++ b/libs/shared/src/config.rs @@ -1,8 +1,8 @@ +use anyhow::Result; use config::{Config, Environment, File}; use serde::{de::DeserializeOwned, Deserialize}; use std::{env, ops::Deref}; use tracing::info; -use anyhow::Result; #[derive(Debug, Deserialize, Clone)] pub struct Settings {