支持多开实例隔离并默认打开小店
This commit is contained in:
@@ -115,8 +115,8 @@ var registry = map[string]*Activity{
|
||||
xiaodian.ID: xiaodian,
|
||||
}
|
||||
|
||||
// DefaultActivityID 默认活动,新分支默认启用「电竞手册」。
|
||||
const DefaultActivityID = "peacematch"
|
||||
// DefaultActivityID 是新建配置时默认打开的活动。
|
||||
const DefaultActivityID = "xiaodian"
|
||||
|
||||
// List 返回所有注册活动,按注册顺序(精英手册在前)。
|
||||
func List() []*Activity {
|
||||
|
||||
+32
-5
@@ -30,10 +30,12 @@ type App struct {
|
||||
scheduler *seckill.Scheduler
|
||||
logStore *logger.MemoryLogStore
|
||||
|
||||
traceMu sync.RWMutex
|
||||
trace *trace.Recorder
|
||||
paths types.AppPaths
|
||||
font *truetype.Font
|
||||
traceMu sync.RWMutex
|
||||
trace *trace.Recorder
|
||||
paths types.AppPaths
|
||||
font *truetype.Font
|
||||
instanceLease *config.InstanceLease
|
||||
instanceName string
|
||||
|
||||
cfgMu sync.RWMutex
|
||||
cfg types.Config
|
||||
@@ -122,10 +124,23 @@ func New() *App {
|
||||
func (a *App) Startup(ctx context.Context) {
|
||||
a.ctx = ctx
|
||||
|
||||
a.paths = config.DefaultAppPaths()
|
||||
lease, instanceErr := config.AcquireInstancePaths()
|
||||
if instanceErr != nil {
|
||||
a.paths = config.DefaultAppPaths()
|
||||
a.instanceName = "默认实例"
|
||||
} else {
|
||||
a.paths = lease.Paths()
|
||||
a.instanceLease = lease
|
||||
a.instanceName = lease.Name()
|
||||
}
|
||||
configMigrated, configMigrationErr := config.MigrateLegacyFile(a.paths.ConfigFile, a.paths.LegacyConfigFile)
|
||||
a.logStore = logger.NewMemoryLogStore(800)
|
||||
a.closeLog = logger.InitLogging(a.logStore, a.paths.LogDir)
|
||||
if instanceErr != nil {
|
||||
logger.LogEvent("系统", "warn", "实例隔离初始化失败,已回退默认目录: %v", instanceErr)
|
||||
} else {
|
||||
logger.LogEvent("系统", "success", "已启动%s,数据目录: %s", a.instanceName, a.paths.DataDir)
|
||||
}
|
||||
if configMigrationErr != nil {
|
||||
logger.LogEvent("配置", "warn", "旧配置迁移失败: %s: %v", a.paths.LegacyConfigFile, configMigrationErr)
|
||||
} else if configMigrated {
|
||||
@@ -182,6 +197,10 @@ func (a *App) Shutdown(ctx context.Context) {
|
||||
if a.closeLog != nil {
|
||||
a.closeLog()
|
||||
}
|
||||
if a.instanceLease != nil {
|
||||
a.instanceLease.Release()
|
||||
a.instanceLease = nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetVersion 返回应用版本号。
|
||||
@@ -191,3 +210,11 @@ func (a *App) GetVersion() string {
|
||||
}
|
||||
return constants.Version
|
||||
}
|
||||
|
||||
// GetInstanceName 返回当前自动分配的本地实例名称。
|
||||
func (a *App) GetInstanceName() string {
|
||||
if a.instanceName == "" {
|
||||
return "默认实例"
|
||||
}
|
||||
return a.instanceName
|
||||
}
|
||||
|
||||
@@ -17,11 +17,22 @@ import (
|
||||
// DefaultAppPaths 收集运行时路径,支持环境变量覆盖。
|
||||
// 默认数据存放在当前用户的应用配置目录,而非工作目录,避免 macOS 应用包目录无写权限。
|
||||
func DefaultAppPaths() types.AppPaths {
|
||||
dataDir := resolvePath(envOrDefault("DOUYU_DATA_DIR", defaultAppDataDir()))
|
||||
return appPathsForDataDir(defaultDataDir(), true)
|
||||
}
|
||||
|
||||
func defaultDataDir() string {
|
||||
return resolvePath(envOrDefault("DOUYU_DATA_DIR", defaultAppDataDir()))
|
||||
}
|
||||
|
||||
func appPathsForDataDir(dataDir string, useLegacyFiles bool) types.AppPaths {
|
||||
configFile := filepath.Join(dataDir, constants.DefaultConfigFile)
|
||||
accountFile := filepath.Join(dataDir, constants.DefaultAccountFile)
|
||||
legacyConfigFile := resolvePath(constants.LegacyDefaultConfigFile)
|
||||
legacyAccountFile := resolvePath(constants.LegacyDefaultAccountFile)
|
||||
legacyConfigFile := ""
|
||||
legacyAccountFile := ""
|
||||
if useLegacyFiles {
|
||||
legacyConfigFile = resolvePath(constants.LegacyDefaultConfigFile)
|
||||
legacyAccountFile = resolvePath(constants.LegacyDefaultAccountFile)
|
||||
}
|
||||
|
||||
if value := os.Getenv("DOUYU_CONFIG"); value != "" {
|
||||
configFile = resolvePath(value)
|
||||
@@ -51,12 +62,16 @@ func DefaultAppPaths() types.AppPaths {
|
||||
// defaultLogDir 在 wails dev 中把日志放在项目工作目录,便于开发时直接查看;
|
||||
// 正式包始终使用用户应用数据目录,避免写入应用包或受限目录。
|
||||
func defaultLogDir(dataDir string) string {
|
||||
if os.Getenv("devserver") != "" || os.Getenv("frontenddevserverurl") != "" {
|
||||
if isWailsDev() {
|
||||
return resolvePath(constants.DefaultLogDir)
|
||||
}
|
||||
return filepath.Join(dataDir, constants.DefaultLogDir)
|
||||
}
|
||||
|
||||
func isWailsDev() bool {
|
||||
return os.Getenv("devserver") != "" || os.Getenv("frontenddevserverurl") != ""
|
||||
}
|
||||
|
||||
func defaultAppDataDir() string {
|
||||
if dir, err := os.UserConfigDir(); err == nil && dir != "" {
|
||||
return filepath.Join(dir, constants.DefaultAppDataDirName)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"douyu_go/internal/constants"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
const maxInstanceSlots = 256
|
||||
|
||||
// InstanceLease 代表当前进程持有的实例槽位;必须在程序退出时释放。
|
||||
type InstanceLease struct {
|
||||
paths types.AppPaths
|
||||
name string
|
||||
lock instanceLock
|
||||
releaseOnce sync.Once
|
||||
}
|
||||
|
||||
// AcquireInstancePaths 按顺序占用空闲实例。第一个进程使用默认实例,
|
||||
// 后续进程分别使用实例 2、实例 3,以隔离账号、配置与运行数据。
|
||||
func AcquireInstancePaths() (*InstanceLease, error) {
|
||||
baseDir := defaultDataDir()
|
||||
instanceRoot := filepath.Join(baseDir, "instances")
|
||||
lockDir := filepath.Join(instanceRoot, ".locks")
|
||||
if err := osMkdirAll(lockDir); err != nil {
|
||||
return nil, fmt.Errorf("创建实例锁目录失败: %w", err)
|
||||
}
|
||||
|
||||
for slot := 1; slot <= maxInstanceSlots; slot++ {
|
||||
lock, acquired, err := tryAcquireInstanceLock(filepath.Join(lockDir, instanceLockFileName(slot)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("锁定%s失败: %w", instanceName(slot), err)
|
||||
}
|
||||
if !acquired {
|
||||
continue
|
||||
}
|
||||
|
||||
dataDir := baseDir
|
||||
if slot > 1 {
|
||||
dataDir = filepath.Join(instanceRoot, fmt.Sprintf("instance-%d", slot))
|
||||
}
|
||||
paths := appPathsForDataDir(dataDir, slot == 1)
|
||||
if slot > 1 && isWailsDev() && os.Getenv("DOUYU_LOG_DIR") == "" {
|
||||
paths.LogDir = resolvePath(filepath.Join(constants.DefaultLogDir, "instances", fmt.Sprintf("instance-%d", slot)))
|
||||
}
|
||||
return &InstanceLease{
|
||||
paths: paths,
|
||||
name: instanceName(slot),
|
||||
lock: lock,
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("最多支持同时运行 %d 个实例", maxInstanceSlots)
|
||||
}
|
||||
|
||||
// Paths 返回当前实例独立的数据路径。
|
||||
func (l *InstanceLease) Paths() types.AppPaths {
|
||||
if l == nil {
|
||||
return types.AppPaths{}
|
||||
}
|
||||
return l.paths
|
||||
}
|
||||
|
||||
// Name 返回面向用户的实例名称。
|
||||
func (l *InstanceLease) Name() string {
|
||||
if l == nil {
|
||||
return ""
|
||||
}
|
||||
return l.name
|
||||
}
|
||||
|
||||
// Release 释放实例锁,允许新启动的程序复用该实例。
|
||||
func (l *InstanceLease) Release() {
|
||||
if l == nil || l.lock == nil {
|
||||
return
|
||||
}
|
||||
l.releaseOnce.Do(func() {
|
||||
_ = l.lock.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func instanceName(slot int) string {
|
||||
if slot <= 1 {
|
||||
return "默认实例"
|
||||
}
|
||||
return fmt.Sprintf("实例 %d", slot)
|
||||
}
|
||||
|
||||
func instanceLockFileName(slot int) string {
|
||||
if slot <= 1 {
|
||||
return "default.lock"
|
||||
}
|
||||
return fmt.Sprintf("instance-%d.lock", slot)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
import "os"
|
||||
|
||||
type instanceLock interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
func osMkdirAll(path string) error {
|
||||
return os.MkdirAll(path, 0700)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//go:build !windows
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type unixInstanceLock struct {
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func tryAcquireInstanceLock(path string) (instanceLock, bool, error) {
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
|
||||
_ = file.Close()
|
||||
if errors.Is(err, syscall.EWOULDBLOCK) || errors.Is(err, syscall.EAGAIN) {
|
||||
return nil, false, nil
|
||||
}
|
||||
return nil, false, err
|
||||
}
|
||||
return &unixInstanceLock{file: file}, true, nil
|
||||
}
|
||||
|
||||
func (l *unixInstanceLock) Close() error {
|
||||
if l == nil || l.file == nil {
|
||||
return nil
|
||||
}
|
||||
_ = syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN)
|
||||
return l.file.Close()
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//go:build windows
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
type windowsInstanceLock struct {
|
||||
handle windows.Handle
|
||||
}
|
||||
|
||||
func tryAcquireInstanceLock(path string) (instanceLock, bool, error) {
|
||||
handle, err := windows.CreateFile(
|
||||
windows.StringToUTF16Ptr(path),
|
||||
windows.GENERIC_READ|windows.GENERIC_WRITE,
|
||||
0,
|
||||
nil,
|
||||
windows.OPEN_ALWAYS,
|
||||
windows.FILE_ATTRIBUTE_NORMAL,
|
||||
0,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, windows.ERROR_SHARING_VIOLATION) || errors.Is(err, windows.ERROR_ACCESS_DENIED) {
|
||||
return nil, false, nil
|
||||
}
|
||||
return nil, false, err
|
||||
}
|
||||
return &windowsInstanceLock{handle: handle}, true, nil
|
||||
}
|
||||
|
||||
func (l *windowsInstanceLock) Close() error {
|
||||
if l == nil || l.handle == windows.InvalidHandle {
|
||||
return nil
|
||||
}
|
||||
err := windows.CloseHandle(l.handle)
|
||||
l.handle = windows.InvalidHandle
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAcquireInstancePathsSeparatesConcurrentInstances(t *testing.T) {
|
||||
baseDir := t.TempDir()
|
||||
t.Setenv("DOUYU_DATA_DIR", baseDir)
|
||||
t.Setenv("DOUYU_CONFIG", "")
|
||||
t.Setenv("DOUYU_USERS", "")
|
||||
t.Setenv("DOUYU_LOG_DIR", "")
|
||||
t.Setenv("DOUYU_SUCCESS_DIR", "")
|
||||
t.Setenv("devserver", "")
|
||||
t.Setenv("frontenddevserverurl", "")
|
||||
|
||||
first, err := AcquireInstancePaths()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer first.Release()
|
||||
if first.Name() != "默认实例" || first.Paths().DataDir != baseDir {
|
||||
t.Fatalf("first instance = %q, %q", first.Name(), first.Paths().DataDir)
|
||||
}
|
||||
|
||||
second, err := AcquireInstancePaths()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer second.Release()
|
||||
wantSecondDir := filepath.Join(baseDir, "instances", "instance-2")
|
||||
if second.Name() != "实例 2" || second.Paths().DataDir != wantSecondDir {
|
||||
t.Fatalf("second instance = %q, %q", second.Name(), second.Paths().DataDir)
|
||||
}
|
||||
if second.Paths().LegacyConfigFile != "" || second.Paths().LegacyAccountFile != "" {
|
||||
t.Fatal("附加实例不应迁移默认实例或旧工作目录数据")
|
||||
}
|
||||
|
||||
first.Release()
|
||||
reused, err := AcquireInstancePaths()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reused.Release()
|
||||
if reused.Name() != "默认实例" || reused.Paths().DataDir != baseDir {
|
||||
t.Fatalf("reused instance = %q, %q", reused.Name(), reused.Paths().DataDir)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package constants
|
||||
import "time"
|
||||
|
||||
// Version 应用版本号,与 wails.json version 同步。
|
||||
const Version = "0.1.7"
|
||||
const Version = "0.1.9"
|
||||
|
||||
// BuildTime 构建时间,由编译时 ldflags 注入。
|
||||
var BuildTime = "unknown"
|
||||
|
||||
Reference in New Issue
Block a user