Skip to content

Commit

Permalink
Merge pull request #12 from olup/olup/limit-coroutines
Browse files Browse the repository at this point in the history
fix: limit coroutines to always leave a certain amount of ram
  • Loading branch information
olup authored Jul 27, 2023
2 parents c8da91c + f6fedca commit 93bdd6c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pierrec/lz4/v4 v4.1.14 // indirect
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE=
github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 h1:acNfDZXmm28D2Yg/c3ALnZStzNaZMSagpbr96vY6Zjc=
Expand Down
23 changes: 23 additions & 0 deletions pkg/lunii/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

cp "github.com/otiai10/copy"
"github.com/pbnjay/memory"
yaml "gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -132,9 +133,31 @@ func (device *Device) AddStudioPack(studioPack *StudioPack, updateChan *chan str
deviceAudioDirectory := filepath.Join(tempPath, "sf", "000")
os.MkdirAll(deviceAudioDirectory, 0700)

// for each audioindex start a goroutine to convert and write the audio
// throttle the number of goroutines depending on the ram available

waitingTime := 500 * time.Millisecond // 500 milliseconds
scheduleMaxRetries := 20 // 10 seconds (20 * 500ms)
minMemory := uint64(500000000) // 500MB

for i, audio := range *audioIndex {
// wait until there is enough memory to convert the audio
tryCount := 0
for memory.FreeMemory() < minMemory {
tryCount++
if tryCount > scheduleMaxRetries {
return errors.New("Timing out : Not enough memory to convert the audio files")
}
// wait 500 milliseconds before checking again
time.Sleep(waitingTime)
}

// add a coroutine to the wait-group
wg.Add(1)
go convertAndWriteAudio(*reader, deviceAudioDirectory, audio, i)

// wait 500 milliseconds so that coroutines can start
time.Sleep(waitingTime)
}

wg.Wait()
Expand Down

0 comments on commit 93bdd6c

Please sign in to comment.