-
Notifications
You must be signed in to change notification settings - Fork 0
/
usernames.go
89 lines (73 loc) · 2.23 KB
/
usernames.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package usernames
import (
"github.com/denis-kilchichakov/usernames/network"
"github.com/denis-kilchichakov/usernames/services"
)
type CheckResult struct {
Service string
Err error
Found bool
}
func SupportedServices() []string {
return services.GetSupportedServiceNames()
}
func SupportedTags() []string {
return services.GetSupportedServiceTags()
}
func ServicesByTag(tag string) []string {
return services.GetSupportedServiceNamesByTag(tag)
}
func CheckAll(username string, parallelism int) []CheckResult {
return checkInternal(SupportedServices(), username, parallelism)
}
func Check(services []string, username string, parallelism int) []CheckResult {
return checkInternal(services, username, parallelism)
}
func CheckExcluding(services []string, username string, parallelism int) []CheckResult {
excludedServices := make(map[string]bool)
for _, service := range services {
excludedServices[service] = true
}
allServices := SupportedServices()
services = make([]string, 0)
for _, service := range allServices {
if !excludedServices[service] {
services = append(services, service)
}
}
return checkInternal(services, username, parallelism)
}
func CheckByTags(tags []string, username string, parallelism int) []CheckResult {
services := make([]string, 0)
for _, tag := range tags {
services = append(services, ServicesByTag(tag)...)
}
return checkInternal(services, username, parallelism)
}
type checkTask struct {
service string
username string
}
func checkInternal(services []string, username string, parallelism int) []CheckResult {
client := &network.DefaultRESTClient{}
tasks := make(chan checkTask, parallelism)
results := make(chan CheckResult, len(services))
for w := 0; w < parallelism; w++ {
go worker(tasks, results, client)
}
for _, service := range services {
tasks <- checkTask{service, username}
}
close(tasks)
result := make([]CheckResult, 0, len(services))
for i := 0; i < len(services); i++ {
result = append(result, <-results)
}
return result
}
func worker(tasks chan checkTask, results chan CheckResult, client network.RESTClient) {
for task := range tasks {
found, err := services.Check(task.service, task.username, client)
results <- CheckResult{task.service, err, found}
}
}