Skip to content

Commit 204e206

Browse files
andrewlidel
andauthored
feat: disable/enable gc via settings menu (#1740)
* feat: disable/enable gc via settings menu Closes #1647 Very similar approach to #1735 * fix: ensure checkbox follows cli flag config - setting default state to on for new and pre-existing config - remove keysize (not needed since go-ipfs 0.7.0 uses ED25519 keys by default) Co-authored-by: Marcin Rataj <[email protected]>
1 parent cc4144f commit 204e206

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

assets/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
"launchOnStartup": "Launch at Login",
225225
"openWebUIAtLaunch": "Open Web UI at Launch",
226226
"pubsub": "Enable PubSub",
227+
"automaticGC": "Automatic Garbage Collection",
227228
"ipfsCommandLineTools": "Command Line Tools",
228229
"takeScreenshotShortcut": "Global Screenshot Shortcut",
229230
"downloadHashShortcut": "Global Download Shortcut",

src/automatic-gc.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const createToggler = require('./utils/create-toggler')
2+
const logger = require('./common/logger')
3+
const store = require('./common/store')
4+
const { ipcMain } = require('electron')
5+
6+
const CONFIG_KEY = 'automaticGC'
7+
const gcFlag = '--enable-gc'
8+
const isEnabled = flags => flags.some(f => f === gcFlag)
9+
10+
function enable () {
11+
const flags = store.get('ipfsConfig.flags', [])
12+
if (!isEnabled(flags)) {
13+
flags.push(gcFlag)
14+
applyConfig(flags)
15+
}
16+
}
17+
18+
function disable () {
19+
let flags = store.get('ipfsConfig.flags', [])
20+
if (isEnabled(flags)) {
21+
flags = flags.filter(item => item !== gcFlag) // remove flag
22+
applyConfig(flags)
23+
}
24+
}
25+
26+
function applyConfig (newFlags) {
27+
store.set('ipfsConfig.flags', newFlags)
28+
ipcMain.emit('ipfsConfigChanged') // trigger node restart
29+
}
30+
31+
module.exports = async function () {
32+
const activate = ({ newValue, oldValue }) => {
33+
if (newValue === oldValue) return
34+
35+
try {
36+
if (newValue === true) {
37+
enable()
38+
} else {
39+
disable()
40+
}
41+
42+
return true
43+
} catch (err) {
44+
logger.error(`[automatic gc] ${err.toString()}`)
45+
46+
return false
47+
}
48+
}
49+
activate({ newValue: store.get(CONFIG_KEY, true) })
50+
createToggler(CONFIG_KEY, activate)
51+
logger.info(`[automatic gc] ${store.get(CONFIG_KEY, true) ? 'enabled' : 'disabled'}`)
52+
}
53+
54+
module.exports.CONFIG_KEY = CONFIG_KEY

src/common/store.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const defaults = {
99
'--migrate',
1010
'--enable-gc',
1111
'--routing', 'dhtclient'
12-
],
13-
keysize: 2048
12+
]
1413
},
1514
language: (electron.app || electron.remote.app).getLocale(),
1615
experiments: {}
@@ -25,6 +24,14 @@ const migrations = {
2524
if (flags.includes('--migrate=true') || flags.includes('--enable-gc=true')) {
2625
store.set('ipfsConfig.flags', defaults.ipfsConfig.flags)
2726
}
27+
},
28+
'>0.13.2': store => {
29+
const flags = store.get('ipfsConfig.flags', [])
30+
const automaticGC = store.get('automaticGC', false)
31+
// ensure checkbox follows cli flag config
32+
if (flags.includes('--enable-gc') && !automaticGC) {
33+
store.set('automaticGC', true)
34+
}
2835
}
2936
}
3037

src/daemon/daemon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function writeIpfsBinaryPath (path) {
3535
)
3636
}
3737

38-
async function spawn ({ flags, path, keysize }) {
38+
async function spawn ({ flags, path }) {
3939
const ipfsBin = getIpfsBinPath()
4040
writeIpfsBinaryPath(ipfsBin)
4141

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const setupNpmOnIpfs = require('./npm-on-ipfs')
99
const setupDaemon = require('./daemon')
1010
const setupWebUI = require('./webui')
1111
const setupAutoLaunch = require('./auto-launch')
12+
const setupAutoGc = require('./automatic-gc')
1213
const setupPubsub = require('./enable-pubsub')
1314
const setupDownloadCid = require('./download-cid')
1415
const setupTakeScreenshot = require('./take-screenshot')
@@ -75,6 +76,7 @@ async function run () {
7576
await Promise.all([
7677
setupArgvFilesHandler(ctx),
7778
setupAutoLaunch(ctx),
79+
setupAutoGc(ctx),
7880
setupPubsub(ctx),
7981
setupSecondInstance(ctx),
8082
// Setup global shortcuts

src/tray.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ const { CONFIG_KEY: SCREENSHOT_KEY, SHORTCUT: SCREENSHOT_SHORTCUT, takeScreensho
1414
const { CONFIG_KEY: DOWNLOAD_KEY, SHORTCUT: DOWNLOAD_SHORTCUT, downloadCid } = require('./download-cid')
1515
const { CONFIG_KEY: AUTO_LAUNCH_KEY, isSupported: supportsLaunchAtLogin } = require('./auto-launch')
1616
const { CONFIG_KEY: PUBSUB_KEY } = require('./enable-pubsub')
17+
const { CONFIG_KEY: AUTO_GC_KEY } = require('./automatic-gc')
1718
const { CONFIG_KEY: IPFS_PATH_KEY } = require('./ipfs-on-path')
1819
const { CONFIG_KEY: NPM_IPFS_KEY } = require('./npm-on-ipfs')
1920
const { CONFIG_KEY: AUTO_LAUNCH_WEBUI_KEY } = require('./webui')
2021

2122
const CONFIG_KEYS = [
2223
AUTO_LAUNCH_KEY,
2324
AUTO_LAUNCH_WEBUI_KEY,
25+
AUTO_GC_KEY,
2426
IPFS_PATH_KEY,
2527
NPM_IPFS_KEY,
2628
SCREENSHOT_KEY,
@@ -124,6 +126,7 @@ function buildMenu (ctx) {
124126
},
125127
buildCheckbox(AUTO_LAUNCH_KEY, 'settings.launchOnStartup'),
126128
buildCheckbox(AUTO_LAUNCH_WEBUI_KEY, 'settings.openWebUIAtLaunch'),
129+
buildCheckbox(AUTO_GC_KEY, 'settings.automaticGC'),
127130
buildCheckbox(IPFS_PATH_KEY, 'settings.ipfsCommandLineTools'),
128131
buildCheckbox(SCREENSHOT_KEY, 'settings.takeScreenshotShortcut'),
129132
buildCheckbox(DOWNLOAD_KEY, 'settings.downloadHashShortcut'),

0 commit comments

Comments
 (0)