summaryrefslogtreecommitdiff
path: root/engine/CoreObjects/Balancer.go
diff options
context:
space:
mode:
authorphyscik <mynameisgennadiy@vk.com>2026-04-06 18:29:34 +0500
committerphyscik <mynameisgennadiy@vk.com>2026-04-06 18:29:34 +0500
commite211798077de88fe39d1e1c0add7a0f460d1da5a (patch)
treede3163568818116a2ca43d31c66947d918cea2fd /engine/CoreObjects/Balancer.go
parente75d107899617446add49f00fb03837cb46d2543 (diff)
Experimental async update functionHEADmain
Diffstat (limited to 'engine/CoreObjects/Balancer.go')
-rw-r--r--engine/CoreObjects/Balancer.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/engine/CoreObjects/Balancer.go b/engine/CoreObjects/Balancer.go
new file mode 100644
index 0000000..0e80b10
--- /dev/null
+++ b/engine/CoreObjects/Balancer.go
@@ -0,0 +1,75 @@
+package coreobjects
+
+import (
+ "sync"
+
+ settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
+)
+
+type runner struct {
+ ExecChan chan UpdateFunction
+ CompleteChan chan any
+}
+
+func makeRunner(execChan chan UpdateFunction, completeChan chan any) *runner {
+ var outp = &runner {
+ ExecChan: execChan,
+ CompleteChan: completeChan,
+ }
+ outp.Init()
+ return outp
+}
+
+func (base *runner) Init() {
+ go func() {
+ for {
+ var task = <- base.ExecChan
+ task()
+ base.CompleteChan <- nil
+ }
+ }()
+}
+
+// Balances the updates so that the execution of WASM doesn't clog the system
+type balancer struct {
+ MaxRoutines int
+ // So that we don't use invloke balancer twice
+ mut *sync.Mutex
+ Runners []*runner
+ // balancer ->
+ ExecChan chan UpdateFunction
+ CompleteChan chan any
+}
+
+func makeBalancer() *balancer {
+ var maxRoutines = settings.Current.MaxUpdateRoutines
+ var outp = &balancer{
+ MaxRoutines: maxRoutines,
+ mut: &sync.Mutex{},
+ Runners: make([]*runner, maxRoutines),
+ ExecChan: make(chan UpdateFunction, maxRoutines),
+ CompleteChan: make(chan any, maxRoutines),
+ }
+ for i := range maxRoutines {
+ outp.Runners[i] = makeRunner(outp.ExecChan, outp.CompleteChan)
+ }
+ return outp
+}
+
+func (base *balancer) Execute(tasks []UpdateFunction) {
+ base.mut.Lock()
+ defer base.mut.Unlock()
+ for _, v := range tasks {
+ base.ExecChan <- v
+ }
+
+ // Sync
+ var results = 0
+ for {
+ <- base.CompleteChan
+ results++
+ if results == len(tasks) {
+ break
+ }
+ }
+}