Skip to content

Commit

Permalink
Update welo and enable bootstrap groups hang test.
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Apr 3, 2024
1 parent 62c5575 commit 93a221d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"streaming-iterables": "^8.0.1",
"ts-event-target": "^0.1.5",
"uint8arrays": "^5.0.2",
"welo": "^4.1.1",
"welo": "^4.2.0",
"yargs": "^17.7.2",
"zod": "^3.22.4"
}
Expand Down
35 changes: 35 additions & 0 deletions packages/daemon/src/common/bootstrap-groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CID } from 'multiformats/cid'
import { Address } from 'welo'
import { type Components, Config } from './interface.js'
import type { Loggers } from '@/logger.js'

type BootstrapComponents = Pick<Components, 'groups' | 'parseConfig' | 'welo'> & { logger: Loggers }

export default async ({ groups, parseConfig, welo, logger }: BootstrapComponents): Promise<{ stop (): void }> => {
const config = parseConfig(Config)
const controller = new AbortController()

Promise.allSettled(config.groups.map(async group => {
if (groups.get(CID.parse(group)) != null) {
return
}

try {
const manifest = await welo.fetch(Address.fromString(`/hldb/${group}`), {
signal: controller.signal
})

await groups.add(manifest)
} catch (error) {
logger.warn(`Failed to bootstrap group '${group}'`)
}
})).catch(() => {
// Ignore
})

return {
stop () {
controller.abort()
}
}
}
32 changes: 5 additions & 27 deletions packages/daemon/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { FsBlockstore } from 'blockstore-fs'
import { MemoryDatastore } from 'datastore-core'
import { FsDatastore } from 'datastore-fs'
import { createHelia } from 'helia'
import { CID } from 'multiformats/cid'
import { type Manifest, Address, createWelo, pubsubReplicator, bootstrapReplicator } from 'welo'
import { createWelo, pubsubReplicator, bootstrapReplicator } from 'welo'
import { type z } from 'zod'
import bootstrapGroups from './bootstrap-groups.js'
import { createDownloader } from './downloader/index.js'
import { EntryTracker } from './entry-tracker.js'
import { createGroups } from './groups.js'
Expand Down Expand Up @@ -85,31 +85,7 @@ export default async (settings: Partial<Settings> = {}): Promise<Components> =>
welo
})

// The following bootstraps groups but if the welo.fetch cannot get the
// manifest it will hang the program due to welo.fetch not being abortable.
// This means that it will hang the program for a long time after it is
// shutdown.
Promise.allSettled(config.groups.map(async group => {
if (groups.get(CID.parse(group)) != null) {
return
}

try {
const manifest = await new Promise<Manifest>((resolve, reject) => {
setTimeout(() => {
reject(new Error('timeout'))
}, 10000)

welo.fetch(Address.fromString(`/hldb/${group}`)).then(resolve).catch(reject)
})

await groups.add(manifest)
} catch (error) {
logger.warn(`Failed to bootstrap group '${group}'`)
}
})).catch(() => {
// Ignore
})
const bootstrappingGroups = await bootstrapGroups({ welo, groups, parseConfig, logger })

const sneakernet = new Sneakernet({
welo,
Expand Down Expand Up @@ -138,6 +114,8 @@ export default async (settings: Partial<Settings> = {}): Promise<Components> =>
const stop = async (): Promise<void> => {
logger.info('cleaning up...')

bootstrappingGroups.stop()

const closeDatastore = async (): Promise<void> => {
if (datastore instanceof FsDatastore) {
await datastore.close()
Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/test/modules/groups.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('groups', () => {
])
})

it.skip('bootstrapping a group from config does not hang startup', async () => {
it('bootstrapping a group from config does not hang startup', async () => {
const { components } = await new Promise<{ components: Components }>((resolve, reject) => {
setTimeout(() => { reject(new Error('timeout')) }, 5000)

Expand Down

0 comments on commit 93a221d

Please sign in to comment.