Skip to content

Commit 84328cc

Browse files
committed
Optimization
1 parent 62c63cb commit 84328cc

File tree

5 files changed

+39
-49
lines changed

5 files changed

+39
-49
lines changed

src/epsilon/queue/epsilon_queue.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
use crate::epsilon::queue::queue_provider::Group;
2-
use crate::epsilon::server::templates::template::Template;
32

43
use std::collections::{HashSet, VecDeque};
54

65
pub struct Queue {
7-
template: Template,
86
queue: VecDeque<Group>,
97
in_queue: HashSet<String>,
108
}
119

1210
impl Queue {
13-
pub fn new(template: Template) -> Self {
11+
pub fn new() -> Self {
1412
Self {
15-
template,
1613
queue: VecDeque::new(),
1714
in_queue: HashSet::new(),
1815
}
1916
}
2017

21-
pub fn get_template(&self) -> &Template {
22-
&self.template
23-
}
24-
2518
pub fn push(&mut self, group: Group) {
2619
for player in &group.players {
2720
if self.in_queue.contains(player) {

src/epsilon/queue/queue_provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl QueueProvider {
1919
let mut map = HashMap::new();
2020

2121
for template in instance_provider.get_templates().await? {
22-
map.insert(String::from(&template.name), Queue::new(template));
22+
map.insert(String::from(&template.name), Queue::new());
2323
}
2424

2525
Ok(Arc::new(Mutex::new(QueueProvider { queue_map: map })))

src/epsilon/server/instance.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use crate::epsilon::server::instance_type::InstanceType;
44
use crate::k8s::label::Label;
55
use crate::{EResult, Kube};
66
use anyhow::format_err;
7-
use async_minecraft_ping::ServerDescription::Plain;
8-
use async_minecraft_ping::{ConnectionConfig, ServerPlayers, ServerVersion, StatusResponse};
7+
use async_minecraft_ping::{ConnectionConfig, StatusResponse};
98
use k8s_openapi::api::core::v1::Pod;
109
use std::sync::Arc;
1110
use std::time::Duration;
@@ -103,27 +102,20 @@ impl Instance {
103102
let timeout = Duration::from_millis(500);
104103
let config = ConnectionConfig::build(address).with_timeout(timeout);
105104

106-
match config.connect().await {
107-
Ok(connection) => Ok(connection.status().await?.status),
108-
Err(_) => Ok(StatusResponse {
109-
version: ServerVersion {
110-
name: "".to_string(),
111-
protocol: 0,
112-
},
113-
players: ServerPlayers {
114-
max: 10,
115-
online: 0,
116-
sample: None,
117-
},
118-
description: Plain("Description".to_string()),
119-
favicon: None,
120-
}),
121-
}
105+
Ok(config.connect().await.unwrap().status().await?.status)
122106
} else {
123107
Err(format_err!("No address found"))
124108
}
125109
}
126110

111+
pub async fn get_online_count(&self) -> i32 {
112+
self.get_info().await.unwrap().players.online as i32
113+
}
114+
115+
pub async fn get_available_slots(&self) -> i32 {
116+
self.get_instance_slots() - self.get_online_count().await
117+
}
118+
127119
pub fn need_close(&self) -> bool {
128120
let phase = self.pod.status.as_ref().unwrap().phase.as_ref().unwrap();
129121

@@ -143,7 +135,7 @@ impl Instance {
143135
.any(|condition| condition.type_ == "Ready" && condition.status == "True")
144136
&& status.phase.as_ref().unwrap() == "Running";
145137

146-
let label = &labels.get("epsilon.fr/in-game");
138+
let label = &labels.get(Label::IN_GAME_LABEL);
147139
let is_in_game = label.is_some() && label.unwrap() == "true";
148140

149141
let is_stopping = metadata.deletion_timestamp.is_some() || self.need_close();
@@ -165,7 +157,7 @@ impl Instance {
165157
.labels
166158
.as_ref()
167159
.unwrap()
168-
.contains_key("epsilon.fr/main")
160+
.contains_key(Label::DEFAULT_LABEL)
169161
}
170162

171163
pub async fn to_json(&self) -> InstanceJson {
@@ -186,16 +178,27 @@ impl Instance {
186178

187179
#[async_trait]
188180
pub trait VectorOfInstance {
189-
async fn get_online_count(&self) -> EResult<u32>;
181+
async fn get_available_slots(&self) -> EResult<i32>;
182+
async fn get_online_count(&self) -> EResult<i32>;
190183
}
191184

192185
#[async_trait]
193186
impl VectorOfInstance for Vec<Instance> {
194-
async fn get_online_count(&self) -> EResult<u32> {
187+
async fn get_available_slots(&self) -> EResult<i32> {
188+
let mut available_slots = 0;
189+
190+
for instance in self {
191+
available_slots += instance.get_available_slots().await;
192+
}
193+
194+
Ok(available_slots)
195+
}
196+
197+
async fn get_online_count(&self) -> EResult<i32> {
195198
let mut number = 0;
196199

197200
for instance in self {
198-
number += instance.get_info().await?.players.online;
201+
number += instance.get_online_count().await
199202
}
200203

201204
Ok(number)

src/k8s/label.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub struct Label {
77
}
88

99
impl Label {
10+
pub const DEFAULT_LABEL: &'static str = "epsilon.fr/default";
11+
1012
pub const INSTANCE_TYPE_LABEL: &'static str = "epsilon.fr/instance";
1113
pub const TEMPLATE_LABEL: &'static str = "epsilon.fr/template";
1214
pub const SLOTS_LABEL: &'static str = "epsilon.fr/slots";
@@ -43,7 +45,7 @@ impl Label {
4345
}
4446

4547
pub fn get_default_label() -> Label {
46-
Label::new("epsilon.fr/default", "true")
48+
Label::new(Self::DEFAULT_LABEL, "true")
4749
}
4850

4951
pub fn get_instance_type_label(instance_type: &InstanceType) -> Label {

src/tasks/queue_task.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ impl Task for QueueTask {
3232
async fn run(&mut self) -> EResult<()> {
3333
for (template_name, queue) in self.queue_provider.lock().await.get_queues() {
3434
if !queue.is_empty() {
35-
let template = queue.get_template();
36-
3735
let instances_starting = self
3836
.instance_provider
3937
.get_instances(
@@ -57,23 +55,17 @@ impl Task for QueueTask {
5755
}
5856

5957
if let Some(instance) = instances_ready.first() {
60-
let info_result = instance.get_info().await;
61-
62-
if let Ok(info) = info_result {
63-
let mut available_slots = template.slots as u32 - info.players.online;
58+
let mut available_slots = instance.get_available_slots().await;
6459

65-
while !queue.is_empty() && available_slots != 0 {
66-
if let Some(group) = queue.pop() {
67-
let group_size = group.players.len() as u32;
60+
while !queue.is_empty() && available_slots != 0 {
61+
if let Some(group) = queue.pop() {
62+
let group_size = group.players.len() as i32;
6863

69-
if group_size <= available_slots {
70-
available_slots -= group_size;
64+
if group_size <= available_slots {
65+
available_slots -= group_size;
7166

72-
self.epsilon_api.send(SendToServer(
73-
group,
74-
String::from(instance.get_name()),
75-
));
76-
}
67+
self.epsilon_api
68+
.send(SendToServer(group, String::from(instance.get_name())));
7769
}
7870
}
7971
}

0 commit comments

Comments
 (0)