Skip to content

Commit

Permalink
Add NonBlockingSave option for background saving
Browse files Browse the repository at this point in the history
This feature introduces a new configuration option, `NonBlockingSave`, which allows background saving for large files. With this param the saving process will not block the main thread with all network sockets.
By default, it's enabled for the DosBox core.
  • Loading branch information
sergystepanov committed Aug 7, 2024
1 parent 1ff7be3 commit 726907e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pkg/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ emulator:
# Some cores allow binding multiple devices to a single port (DosBox), but typically,
# you should bind just one device to one port.
# - kbMouseSupport (bool) -- (temp) a flag if the core needs the keyboard and mouse on the client
# - nonBlockingSave (bool) -- write save file in a non-blocking way, needed for huge save files
# - vfr (bool)
# (experimental)
# Enable variable frame rate only for cores that can't produce a constant frame rate.
Expand Down Expand Up @@ -271,6 +272,7 @@ emulator:
roms: [ "zip", "cue" ]
folder: dos
kbMouseSupport: true
nonBlockingSave: true
hid:
0: [ 257, 513 ]
1: [ 257, 513 ]
Expand Down
1 change: 1 addition & 0 deletions pkg/config/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type LibretroCoreConfig struct {
IsGlAllowed bool
KbMouseSupport bool
Lib string
NonBlockingSave bool
Options map[string]string
Options4rom map[string]map[string]string // <(^_^)>
Roms []string
Expand Down
1 change: 1 addition & 0 deletions pkg/worker/caged/libretro/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (f *Frontend) LoadCore(emu string) {
scale = conf.Scale
f.log.Debug().Msgf("Scale: x%v", scale)
}
f.storage.SetNonBlocking(conf.NonBlockingSave)
f.scale = scale
f.nano.CoreLoad(meta)
f.mu.Unlock()
Expand Down
20 changes: 15 additions & 5 deletions pkg/worker/caged/libretro/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type (
GetSavePath() string
GetSRAMPath() string
SetMainSaveName(name string)
SetNonBlocking(v bool)
Load(path string) ([]byte, error)
Save(path string, data []byte) error
}
Expand All @@ -24,17 +25,26 @@ type (
// needed for Google Cloud save/restore which
// doesn't support multiple files
MainSave string
NonBlock bool
}
ZipStorage struct {
Storage
}
)

func (s *StateStorage) SetMainSaveName(name string) { s.MainSave = name }
func (s *StateStorage) GetSavePath() string { return filepath.Join(s.Path, s.MainSave+".dat") }
func (s *StateStorage) GetSRAMPath() string { return filepath.Join(s.Path, s.MainSave+".srm") }
func (s *StateStorage) Load(path string) ([]byte, error) { return os.ReadFile(path) }
func (s *StateStorage) Save(path string, dat []byte) error { return os.WriteFile(path, dat, 0644) }
func (s *StateStorage) SetMainSaveName(name string) { s.MainSave = name }
func (s *StateStorage) SetNonBlocking(v bool) { s.NonBlock = v }
func (s *StateStorage) GetSavePath() string { return filepath.Join(s.Path, s.MainSave+".dat") }
func (s *StateStorage) GetSRAMPath() string { return filepath.Join(s.Path, s.MainSave+".srm") }
func (s *StateStorage) Load(path string) ([]byte, error) { return os.ReadFile(path) }
func (s *StateStorage) Save(path string, dat []byte) error {
if s.NonBlock {
go func() { _ = os.WriteFile(path, dat, 0644) }()
return nil
}

return os.WriteFile(path, dat, 0644)
}

func (z *ZipStorage) GetSavePath() string { return z.Storage.GetSavePath() + zip.Ext }
func (z *ZipStorage) GetSRAMPath() string { return z.Storage.GetSRAMPath() + zip.Ext }
Expand Down

0 comments on commit 726907e

Please sign in to comment.