Skip to content

Commit

Permalink
Make sure /tmp directory does not run out of scope before applicati…
Browse files Browse the repository at this point in the history
…on ends (#557)

* Make sure tmp dir does not run out of scope before application ends

* Add entry to CHANGELOG.md
  • Loading branch information
adzialocha authored Sep 8, 2023
1 parent 9bbcd08 commit 8791463
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Build a byte buffer over paginated pieces when assembling blobs [#547](https://github.com/p2panda/aquadoggo/pull/547)
- Stream blob data in chunks to files to not occupy too much memory [#551](https://github.com/p2panda/aquadoggo/pull/551)

## Fixed

- Make sure temporary directory does not run out of scope [#557](https://github.com/p2panda/aquadoggo/pull/557)

## [0.5.0]

### Added
Expand Down
18 changes: 13 additions & 5 deletions aquadoggo_cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::convert::TryFrom;
use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::OnceLock;

use anyhow::{anyhow, bail, Result};
use aquadoggo::{AllowList, Configuration as NodeConfiguration, NetworkConfiguration};
Expand All @@ -16,13 +17,16 @@ use libp2p::multiaddr::Protocol;
use libp2p::{Multiaddr, PeerId};
use p2panda_rs::schema::SchemaId;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use tempfile::TempDir;

use crate::utils::absolute_path;

const WILDCARD: &str = "*";

const CONFIG_FILE_NAME: &str = "config.toml";

static TMP_DIR: OnceLock<TempDir> = OnceLock::new();

type ConfigFilePath = Option<PathBuf>;

/// Get configuration from 1. .toml file, 2. environment variables and 3. command line arguments
Expand Down Expand Up @@ -352,11 +356,15 @@ impl TryFrom<Configuration> for NodeConfiguration {
// Create a temporary blobs directory when none was given
let blobs_base_path = match value.blobs_base_path {
Some(path) => path,
None => {
let tmp_dir = tempfile::TempDir::new()
.map_err(|_| anyhow!("Could not create temporary directory to store blobs"))?;
tmp_dir.path().to_path_buf()
}
None => TMP_DIR
.get_or_init(|| {
// Initialise a `TempDir` instance globally to make sure it does not run out of
// scope and gets deleted before the end of the application runtime
tempfile::TempDir::new()
.expect("Could not create temporary directory to store blobs")
})
.path()
.to_path_buf(),
};

Ok(NodeConfiguration {
Expand Down

0 comments on commit 8791463

Please sign in to comment.