]> git.puffer.fish Git - matthieu/nova.git/commitdiff
fix build and code cleanup
authorMatthieuCoder <matthieu@matthieu-dev.xyz>
Fri, 13 Jan 2023 18:35:46 +0000 (22:35 +0400)
committerMatthieuCoder <matthieu@matthieu-dev.xyz>
Fri, 13 Jan 2023 18:35:46 +0000 (22:35 +0400)
15 files changed:
.github/workflows/build.yml
Makefile
exes/all-in-one/build.rs
exes/all-in-one/src/errors.rs
exes/all-in-one/src/ffi.rs
exes/all-in-one/src/lib.rs
exes/all-in-one/src/main.rs
exes/ratelimit/src/buckets/atomic_instant.rs
exes/ratelimit/src/buckets/bucket.rs
exes/ratelimit/src/buckets/mod.rs
exes/ratelimit/src/grpc.rs
exes/ratelimit/src/lib.rs
exes/rest/src/lib.rs
exes/rest/src/ratelimit_client/mod.rs
libs/shared/src/config.rs

index 813019871e8675be30e060ddab15188bffd1bf58..bb76fee5d40ae5bd9ad857e98b370e8621155a9e 100644 (file)
@@ -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/')
index 5008c89d43853b41fe53662c2610159c8c581e56..f9352c64b4903309ea4f2d4e5fd723776f0742a9 100644 (file)
--- 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
index 83ca65059b1da8142fe3ad8dc218a581664e64a4..25a4b99855c6a2e0b60eac55f12976e9c79518cd 100644 (file)
@@ -22,6 +22,6 @@ fn main() -> Result<(), Box<dyn Error>> {
     };
 
     cbindgen::generate_with_config(crate_dir, config)?.write_to_file(output_file);
-    
+
     Ok(())
 }
index f169fb48e45e71e6bc50b43127bb6a1084f0b9e4..d676e8d3837852dca7951f2f57b86fe12c3eda39 100644 (file)
@@ -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(
index 49586cf4c06addf2490e8f8de167e640b1da093e..7a8821f82bc62f450b2540b33564d41517d3c8cd 100644 (file)
@@ -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::<WebhookServer>(
             json,
-            error_sender.clone(),
+            error_sender,
             &runtime,
         )?);
 
index 5a413446d1cef8d690e2507cdd5d5108ca85e176..74625c02e04c533c26b7041b4730209a7f4f22a6 100644 (file)
@@ -1,3 +1,3 @@
-pub mod utils;
 pub mod errors;
 pub mod ffi;
+pub mod utils;
index e05b853c3c31d9468b573894eb52280cda181257..1e6588395f9a4f823e652d8a7e7a1917e00d148d 100644 (file)
@@ -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) };
+}
index ec31808b504dfabc222c5d576a25ad6a6cd1382e..67dd0ee758a2a7177077c74d0f8d4f916a3a6a14 100644 (file)
@@ -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),
         )
     }
index f4a9b435f7ca0176783a7f16c3cee38744543a01..0d92881a5fdcde365133e3ba8e32260bed6479ff 100644 (file)
@@ -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
         }
     }
 
index e2f52c8e2835e374be2e88abb200401ed2cf5d00..c86d623f4fd6c4870998adaf3e9e9968863fcb2a 100644 (file)
@@ -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
index 0819e4615202ce326ade20dcfa4c90f31a5322ab..09885c1a2f267dc0ead9a61803f16d9431962060 100644 (file)
@@ -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;
index 5891b5853b54ef13e848c3b3405a068b49c6548b..6653157d9ac15f2951f798cf326a22b1bd73d568 100644 (file)
@@ -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 {
index b2287b4b8db9c552d40835e59f9397a80689e695..4ec8b51e70287802eb9accc733365cc78ebfda11 100644 (file)
@@ -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};
index ecd489c552b803c474e1b5ab213ca9331198bb02..dc3e970d45f6fe56701b0eaea1e11731d865b391 100644 (file)
@@ -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")
                 })?;
index 52f6c9228a6fdc839d724c6225a8a27e68f34b87..adcaf797de3aa5f57e8b7e28e85090c70c690cbf 100644 (file)
@@ -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<T: Clone + DeserializeOwned + Default> {