2
0
mirror of https://github.com/Revertron/Alfis synced 2024-11-07 09:20:31 +00:00

Fixed high CPU usage, duplicate peers from exchange and tab change code.

This commit is contained in:
Revertron 2021-02-22 14:55:23 +01:00
parent 803b70fc25
commit df19021c62
3 changed files with 51 additions and 8 deletions

View File

@ -9,7 +9,7 @@ use std::env;
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;
use std::time::{Duration, Instant};
#[cfg(windows)]
use winapi::um::wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS};
@ -165,7 +165,7 @@ fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
let scripts = inline_script(include_str!("webview/scripts.js"));
let html = Content::Html(file_content.to_owned().replace("{styles}", &styles).replace("{scripts}", &scripts));
web_view::builder()
let mut interface = web_view::builder()
.title("ALFIS 0.1.0")
.content(html)
.size(1024, 720)
@ -282,8 +282,36 @@ fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
//dbg!(&signature);
Ok(())
})
.run()
.unwrap();
.build()
.expect("Error building GUI");
// We use this ugly loop to lower CPU usage a lot.
// If we use .run() or only .step() in a loop without sleeps it will try
// to support 60FPS and uses more CPU than it should.
let pause = Duration::from_millis(25);
let mut start = Instant::now();
loop {
match interface.step() {
None => {
info!("Interface closed, exiting");
break;
}
Some(result) => {
match result {
Ok(_) => {}
Err(_) => {
error!("Something wrong with webview, exiting");
break;
}
}
}
}
if start.elapsed().as_millis() > 1 {
thread::sleep(pause);
start = Instant::now();
}
}
interface.exit();
}
fn create_genesis<S: Into<String>>(miner: Arc<Mutex<Miner>>, name: S, keystore: &Keystore, difficulty: u16) {

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::net::{SocketAddr, Shutdown};
use mio::{Token, Interest, Registry};
use mio::net::TcpStream;
@ -54,12 +54,27 @@ impl Peers {
}
pub fn add_peers_from_exchange(&mut self, peers: Vec<String>) {
info!("Got peers: {:?}", &peers);
let peers: HashSet<String> = peers
.iter()
.fold(HashSet::new(), |mut peers, peer| {
peers.insert(peer.to_owned());
peers
});
debug!("Got {} peers: {:?}", peers.len(), &peers);
// TODO make it return error if these peers are wrong and seem like an attack
for peer in peers.iter() {
let addr: SocketAddr = peer.parse().expect(&format!("Error parsing peer {}", peer));
if self.peers
.iter()
.find(|(token, peer)| peer.get_addr() == addr)
.is_some() {
debug!("Skipping address from exchange: {}", &addr);
continue;
}
if skip_addr(&addr) {
info!("Skipping address from exchange: {}", &addr);
debug!("Skipping address from exchange: {}", &addr);
continue; // Return error in future
}
let mut found = false;

View File

@ -97,7 +97,7 @@ function openTab(element, tabName) {
// Get all elements with class="content" and hide them
tabContent = document.getElementsByClassName("content");
for (i = 0; i < tabContent.length; i++) {
tabContent[i].className = "context is-hidden";
tabContent[i].className = "content is-hidden";
}
// Get all elements with class="tablinks" and remove the class "active"