Skip to content

Commit 55cc746

Browse files
committed
refactor: update_container_stat combine is_alive()
1 parent b596916 commit 55cc746

File tree

2 files changed

+57
-61
lines changed

2 files changed

+57
-61
lines changed

Cargo.lock

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/docker_data/mod.rs

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -117,62 +117,58 @@ impl DockerData {
117117
spawn_id: SpawnId,
118118
spawns: Arc<Mutex<HashMap<SpawnId, JoinHandle<()>>>>,
119119
) {
120-
if state.is_alive() {
121-
let id = spawn_id.get_id();
122-
let mut stream = docker
123-
.stats(
124-
id.get(),
125-
Some(StatsOptions {
126-
stream: false,
127-
one_shot: false,
128-
}),
129-
)
130-
.take(1);
131-
132-
while let Some(Ok(stats)) = stream.next().await {
133-
// Memory stats are only collected if the container is alive - is this the behaviour we want?
134-
let mem_stat = if state.is_alive() {
135-
let mem_cache = stats.memory_stats.stats.map_or(0, |i| match i {
136-
MemoryStatsStats::V1(x) => x.inactive_file,
137-
MemoryStatsStats::V2(x) => x.inactive_file,
138-
});
120+
let id = spawn_id.get_id();
121+
let mut stream = docker
122+
.stats(
123+
id.get(),
124+
Some(StatsOptions {
125+
stream: false,
126+
one_shot: false,
127+
}),
128+
)
129+
.take(1);
130+
131+
while let Some(Ok(stats)) = stream.next().await {
132+
// Memory stats are only collected if the container is alive - is this the behaviour we want?
133+
let (mem_stat, cpu_stats) = if state.is_alive() {
134+
let mem_cache = stats.memory_stats.stats.map_or(0, |i| match i {
135+
MemoryStatsStats::V1(x) => x.inactive_file,
136+
MemoryStatsStats::V2(x) => x.inactive_file,
137+
});
138+
(
139139
Some(
140140
stats
141141
.memory_stats
142142
.usage
143143
.unwrap_or_default()
144144
.saturating_sub(mem_cache),
145-
)
146-
} else {
147-
None
148-
};
145+
),
146+
Some(Self::calculate_usage(&stats)),
147+
)
148+
} else {
149+
(None, None)
150+
};
151+
152+
let mem_limit = stats.memory_stats.limit.unwrap_or_default();
149153

150-
let mem_limit = stats.memory_stats.limit.unwrap_or_default();
154+
let op_key = stats
155+
.networks
156+
.as_ref()
157+
.and_then(|networks| networks.keys().next().cloned());
151158

152-
let op_key = stats
159+
let (rx, tx) = if let Some(key) = op_key {
160+
stats
153161
.networks
154-
.as_ref()
155-
.and_then(|networks| networks.keys().next().cloned());
156-
157-
let cpu_stats = if state.is_alive() {
158-
Some(Self::calculate_usage(&stats))
159-
} else {
160-
None
161-
};
162-
let (rx, tx) = if let Some(key) = op_key {
163-
stats
164-
.networks
165-
.unwrap_or_default()
166-
.get(&key)
167-
.map_or((0, 0), |f| (f.rx_bytes, f.tx_bytes))
168-
} else {
169-
(0, 0)
170-
};
171-
172-
app_data
173-
.lock()
174-
.update_stats_by_id(id, cpu_stats, mem_stat, mem_limit, rx, tx);
175-
}
162+
.unwrap_or_default()
163+
.get(&key)
164+
.map_or((0, 0), |f| (f.rx_bytes, f.tx_bytes))
165+
} else {
166+
(0, 0)
167+
};
168+
169+
app_data
170+
.lock()
171+
.update_stats_by_id(id, cpu_stats, mem_stat, mem_limit, rx, tx);
176172
}
177173
spawns.lock().remove(&spawn_id);
178174
}

0 commit comments

Comments
 (0)