Intoroduced concurrncy in filereading

This commit is contained in:
Simon Martens
2024-11-11 22:13:47 +01:00
parent 0aec995441
commit ce7795361a
3 changed files with 31 additions and 24 deletions

View File

@@ -1,7 +1,6 @@
package app
import (
"fmt"
"os"
"path/filepath"
"sync"
@@ -31,11 +30,17 @@ type Library struct {
type KGPZ struct {
lmu sync.Mutex
gmu sync.Mutex
Config *providers.ConfigProvider
Repo *providers.GitProvider
Library
}
func (k *KGPZ) Init() {
go k.initRepo()
k.Serialize()
}
func NewKGPZ(config *providers.ConfigProvider) *KGPZ {
if config == nil {
panic("ConfigProvider is nil")
@@ -55,7 +60,9 @@ func (k *KGPZ) IsDebug() bool {
func (k *KGPZ) Pull() {
// TODO: what happens if the application quits mid-pull?
// We need to make sure to exit gracefully
go func(k *KGPZ) {
go func() {
k.gmu.Lock()
defer k.gmu.Unlock()
if k.Repo == nil {
return
}
@@ -72,18 +79,19 @@ func (k *KGPZ) Pull() {
// Locking is handled in Serialize()
k.Serialize()
}
}(k)
}()
}
func (k *KGPZ) InitRepo() {
func (k *KGPZ) initRepo() {
gp, err := providers.NewGitProvider(k.Config.Config.GitURL, k.Config.Config.FolderPath, k.Config.Config.GitBranch)
if err != nil {
helpers.LogOnErr(&gp, err, "Error creating GitProvider")
return
}
fmt.Println("InitRepo")
k.gmu.Lock()
k.Repo = gp
k.gmu.Unlock()
k.Pull()
if k.IsDebug() {
@@ -93,9 +101,6 @@ func (k *KGPZ) InitRepo() {
// This panics if the data cant be read, and there is no data read
func (k *KGPZ) Serialize() {
// TODO: maybe dont panic.
// We need to check the requirements only when starting the server
// We can serve an error page
new := Library{}
wg := sync.WaitGroup{}

View File

@@ -35,17 +35,12 @@ func main() {
}
kgpz := app.NewKGPZ(cfg)
Bootstrap(kgpz)
kgpz.Init()
server := server.Start(kgpz, cfg)
Start(kgpz, server)
}
func Bootstrap(k *app.KGPZ) {
k.InitRepo()
k.Serialize()
}
func Start(k *app.KGPZ, s *server.Server) {
s.Start()
@@ -83,8 +78,7 @@ func Start(k *app.KGPZ, s *server.Server) {
fmt.Println("Pulling repo.")
k.Pull()
} else if input == "q" {
s := os.Signal(syscall.SIGTERM)
sigs <- s
sigs <- os.Signal(syscall.SIGTERM)
}
}
}()

View File

@@ -20,16 +20,24 @@ type XMLProvider[T KGPZXML[T]] struct {
}
func (p *XMLProvider[T]) Load() error {
// Introduce goroutine for every path, locking on append:
var wg sync.WaitGroup
for _, path := range p.paths {
wg.Add(1)
go func(path string) {
defer wg.Done()
var data T
if err := UnmarshalFile(path, &data); err != nil {
fmt.Println(err)
return err
return
}
p.mu.Lock()
defer p.mu.Unlock()
p.Items = p.Items.Append(data)
p.mu.Unlock()
}(path)
}
wg.Wait()
return nil
}