Skip to content

Commit

Permalink
refactor: Use async-mutext to lock
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jan 16, 2025
1 parent ea9f797 commit be71455
Show file tree
Hide file tree
Showing 32 changed files with 102 additions and 679 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"optifine",
"resourcepack",
"vite",
"vuex",
"windicss",
"xmcl"
],
Expand Down
33 changes: 13 additions & 20 deletions pnpm-lock.yaml

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

5 changes: 2 additions & 3 deletions xmcl-keystone-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
"@xmcl/runtime-api": "workspace:*",
"@xmcl/text-component": "workspace:*",
"@xmcl/user": "workspace:*",
"async-mutex": "^0.5.0",
"driver.js": "^1.3.1",
"fuzzy": "^0.1.3",
"lodash.debounce": "^4.0.8",
"markdown-it": "^13.0.1",
"markdown-it-link-attributes": "^4.0.1",
"rfc6902": "^5.0.1",
"semver": "^7.5.8",
"skinview3d": "^3.0.1",
"swrv": "v2-latest",
Expand All @@ -54,8 +54,7 @@
"vue-i18n": "^8.28.2",
"vue-i18n-bridge": "9.13.1",
"vue-router": "^3.6.5",
"vuetify": "^2.6.13",
"vuex": "^3.6.2"
"vuetify": "^2.6.13"
},
"devDependencies": {
"@intlify/unplugin-vue-i18n": "^0.13.0",
Expand Down
2 changes: 1 addition & 1 deletion xmcl-keystone-ui/src/composables/ftb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function useFeedTheBeastModpackInstall() {
installFiles(path, files)

const lock = getInstanceLock(path)
lock.write(async () => {
lock.runExclusive(async () => {
const resolved = existed ? await getResolvedVersion(existed) : undefined
const instruction = await getInstallInstruction(path, config.runtime, options.version || '', resolved, all.value)
await handleInstallInstruction(instruction)
Expand Down
11 changes: 6 additions & 5 deletions xmcl-keystone-ui/src/composables/instanceVersionInstall.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getSWRV } from '@/util/swrvGet'
import type { AssetIndexIssue, AssetIssue, JavaVersion, LibraryIssue, MinecraftJarIssue, ResolvedVersion } from '@xmcl/core'
import type { InstallProfileIssueReport } from '@xmcl/installer'
import { DiagnoseServiceKey, InstallServiceKey, Instance, InstanceServiceKey, JavaRecord, JavaServiceKey, ReadWriteLock, RuntimeVersions, ServerVersionHeader, VersionHeader, VersionServiceKey, parseOptifineVersion } from '@xmcl/runtime-api'
import { Mutex } from 'async-mutex'
import { DiagnoseServiceKey, InstallServiceKey, Instance, InstanceServiceKey, JavaRecord, JavaServiceKey, RuntimeVersions, ServerVersionHeader, VersionHeader, VersionServiceKey, parseOptifineVersion } from '@xmcl/runtime-api'
import { InjectionKey, Ref, ShallowRef } from 'vue'
import { InstanceResolveVersion } from './instanceVersion'
import { useService } from './service'
Expand Down Expand Up @@ -245,7 +246,7 @@ export function useInstanceVersionInstallInstruction(path: Ref<string>, instance
const loading = ref(0)
const config = inject(kSWRVConfig)

const instanceLock: Record<string, ReadWriteLock> = {}
const instanceLock: Record<string, Mutex> = {}

async function update(version: InstanceResolveVersion | undefined) {
if (!version) return
Expand All @@ -255,7 +256,7 @@ export function useInstanceVersionInstallInstruction(path: Ref<string>, instance
loading.value += 1
const lock = getInstanceLock(path.value)
console.time('[getInstallInstruction]')
await lock.write(async () => {
await lock.runExclusive(async () => {
try {
const _path = version.instance
const _selectedVersion = version.version
Expand Down Expand Up @@ -287,7 +288,7 @@ export function useInstanceVersionInstallInstruction(path: Ref<string>, instance
if (lock) {
return lock
}
const newLock = new ReadWriteLock()
const newLock = new Mutex()
instanceLock[path] = newLock
return newLock
}
Expand Down Expand Up @@ -512,7 +513,7 @@ export function useInstanceVersionInstallInstruction(path: Ref<string>, instance
}
const last = resolvedVersion.value
const lock = getInstanceLock(path.value)
await lock.write(() => handleInstallInstruction(inst))
await lock.runExclusive(() => handleInstallInstruction(inst))
if (last === resolvedVersion.value) {
await update(last)
}
Expand Down
2 changes: 1 addition & 1 deletion xmcl-keystone-ui/src/composables/modpackInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function useModpackInstaller() {
}

const lock = getInstanceLock(instancePath)
lock.write(async () => {
lock.runExclusive(async () => {
const resolved = version ? await resolveLocalVersion(version) : undefined
const instruction = await getInstallInstruction(instancePath, runtime, '', resolved, all.value)
await handleInstallInstruction(instruction)
Expand Down
3 changes: 1 addition & 2 deletions xmcl-runtime-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ export * from './src/task'
export * from './src/util/authority'
export * from './src/util/mavenVersion'
export * from './src/util/SharedState'
export * from './src/util/mutex'
export { default as packFormatVersionRange } from './src/util/packFormatVersionRange'
export * from './src/util/promiseSignal'
export { default as protocolToMinecraft } from './src/util/protocolToMinecraft'
export * from './src/util/sdp'
export * from './src/util/semaphore'
export * from './src/util/LockKey'
export * from './src/util/versionRange'
3 changes: 0 additions & 3 deletions xmcl-runtime-api/src/services/InstanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export class InstanceState {
instances: Instance[] = []

instanceAdd(instance: Instance) {
/**
* Prevent the case that hot reload keep the vuex state
*/
if (!this.all[instance.path]) {
// TODO: remove in vue3
// set(this.all, instance.path, { ...instance, serverStatus: UNKNOWN_STATUS })
Expand Down
11 changes: 11 additions & 0 deletions xmcl-runtime-api/src/util/LockKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

export const LockKey = {
versions: 'versions',
libraries: 'libraries',
assets: 'assets',
version: (v: string) => `versions/${v}`,
instance: (p: string) => `instances/${p}`,
instanceRemove: (p: string) => `instances/${p}/remove`,
shaderpacks: (p: string) => `shaderpacks/${p}`,
resourcepacks: (p: string) => `resourcepacks/${p}`,
}
146 changes: 0 additions & 146 deletions xmcl-runtime-api/src/util/mutex.ts

This file was deleted.

Loading

0 comments on commit be71455

Please sign in to comment.