Use a workerpool for configuration file updates

Co-Authored-By: Jakob <schrej@users.noreply.github.com>
This commit is contained in:
Dane Everitt 2020-07-31 21:25:57 -07:00
parent 5e8425ad6a
commit 5366d0f652
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -1,34 +1,32 @@
package server package server
import ( import (
"github.com/pterodactyl/wings/parser" "github.com/gammazero/workerpool"
"sync" "runtime"
) )
// Parent function that will update all of the defined configuration files for a server // Parent function that will update all of the defined configuration files for a server
// automatically to ensure that they always use the specified values. // automatically to ensure that they always use the specified values.
func (s *Server) UpdateConfigurationFiles() { func (s *Server) UpdateConfigurationFiles() {
wg := new(sync.WaitGroup) pool := workerpool.New(runtime.GOMAXPROCS(0))
files := s.ProcessConfiguration().ConfigurationFiles files := s.ProcessConfiguration().ConfigurationFiles
for _, v := range files { for _, cf := range files {
wg.Add(1) f := cf
go func(f parser.ConfigurationFile, server *Server) { pool.Submit(func() {
defer wg.Done() p, err := s.Filesystem.SafePath(f.FileName)
p, err := server.Filesystem.SafePath(f.FileName)
if err != nil { if err != nil {
server.Log().WithField("error", err).Error("failed to generate safe path for configuration file") s.Log().WithField("error", err).Error("failed to generate safe path for configuration file")
return return
} }
if err := f.Parse(p, false); err != nil { if err := f.Parse(p, false); err != nil {
server.Log().WithField("error", err).Error("failed to parse and update server configuration file") s.Log().WithField("error", err).Error("failed to parse and update server configuration file")
} }
}(v, s) })
} }
wg.Wait() pool.StopWait()
} }