Skip to content

Commit 77dffa9

Browse files
authored
Merge pull request #25 from mainak55512/enhancement
Enhancement
2 parents ffa0cfd + 2c6d9f8 commit 77dffa9

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

main.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func main() {
2121
// Limiting os threads to available cpu
2222
runtime.GOMAXPROCS(runtime.NumCPU())
2323

24-
// Limited goroutines to 1000
25-
max_goroutines := 1000
24+
// Limited goroutines to 20
25+
max_goroutines := 20
2626

2727
// this channel will limit the goroutine number
2828
guard := make(chan struct{}, max_goroutines)
@@ -103,24 +103,23 @@ func main() {
103103
fmt.Sprint(total_comments),
104104
fmt.Sprint(total_code),
105105
})
106-
table.Render()
107106

108107
pwd, e := os.Getwd()
108+
fmt.Printf(
109+
"\nGit initialized:\t%t\nTotal sub-directories:\t%5d\n",
110+
is_git_initialized,
111+
folder_count,
112+
)
113+
fmt.Println("Target directory: ", path.Join(pwd, folder_name))
114+
fmt.Println()
115+
116+
table.Render()
109117

110118
if e != nil {
111119
fmt.Println(e)
112120
os.Exit(1)
113121
}
114122

115-
fmt.Println("Target directory: ", path.Join(pwd, folder_name))
116-
117-
// total subdirectories are folder_count-1,
118-
// as present working directory is not a subdirectory
119-
fmt.Printf(
120-
"Total sub-directories:\t%5d\nGit initialized:\t%t\n",
121-
folder_count-1,
122-
is_git_initialized,
123-
)
124123
} else {
125124

126125
// will be set to true if atleast one file

utils/utils.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)