@@ -281,7 +281,28 @@ func GetFiles(
281281 if file_directory_name != "" {
282282 folder_location = path .Join (folder_location , file_directory_name )
283283 }
284- err := filepath .Walk (folder_location , func (
284+ wgDir := & sync.WaitGroup {}
285+ muDir := & sync.RWMutex {}
286+
287+ // Limited goroutines to 10000
288+ // buffer is set to high to avoid deadlock
289+ max_goroutines_dir := 10000
290+
291+ // this channel will limit the goroutine number
292+ guardDir := make (chan struct {}, max_goroutines_dir )
293+
294+ guardDir <- struct {}{}
295+ wgDir .Add (1 )
296+
297+ err := walkDirConcur (folder_location , folder_count , & files , is_git_initialized , wgDir , muDir , guardDir )
298+ wgDir .Wait ()
299+ return files , err
300+ }
301+
302+ func walkDirConcur (folder_location string , folder_count * int32 , files * []file_info , is_git_initialized * bool , wgDir * sync.WaitGroup , muDir * sync.RWMutex , guardDir chan struct {}) error {
303+ defer wgDir .Done ()
304+
305+ visitFolder := func (
285306 _path string ,
286307 f os.FileInfo ,
287308 err error ,
@@ -292,15 +313,26 @@ func GetFiles(
292313 return err
293314 }
294315 // if it is a folder, then increase the folder count
295- if f .IsDir () {
316+ if f .IsDir () && _path != folder_location {
296317
297318 // if folder name is '.git', then
298319 // set is_git_initialized to true
299- if _path == path .Join (folder_location , ".git" ) && * is_git_initialized == false {
300- * is_git_initialized = true
320+ if _path == path .Join (folder_location , ".git" ) {
321+ muDir .Lock ()
322+ if * is_git_initialized == false {
323+ * is_git_initialized = true
324+ }
325+ muDir .Unlock ()
301326 }
327+ muDir .Lock ()
302328 * folder_count ++
303- } else {
329+ muDir .Unlock ()
330+ guardDir <- struct {}{}
331+ wgDir .Add (1 )
332+ go walkDirConcur (_path , folder_count , files , is_git_initialized , wgDir , muDir , guardDir )
333+ return filepath .SkipDir
334+ }
335+ if f .Mode ().IsRegular () {
304336 ext := strings .Join (
305337 strings .Split (f .Name (), "." )[1 :],
306338 "." ,
@@ -309,13 +341,17 @@ func GetFiles(
309341 ext = f .Name ()
310342 }
311343 if _ , exists := lookup_map [ext ]; exists {
312- files = append (files , file_info {
344+ muDir .Lock ()
345+ * files = append (* files , file_info {
313346 path : _path ,
314347 ext : ext ,
315348 })
349+ muDir .Unlock ()
316350 }
317351 }
318352 return nil
319- })
320- return files , err
353+ }
354+ err := filepath .Walk (folder_location , visitFolder )
355+ <- guardDir
356+ return err
321357}
0 commit comments