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

View File

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

View File

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