Skip to content

Commit

Permalink
Support concurrent execution with multiple users
Browse files Browse the repository at this point in the history
  • Loading branch information
owlinux1000 committed May 15, 2022
1 parent f8d5c3b commit b04335d
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,26 @@ type Result struct {
IfExistsResult int
}

func valid_user(username string) int {
func valid_users(usernames_conn chan string, valid_user_conn chan string) {
for username := range usernames_conn {
if valid_user(username) != "" {
valid_user_conn <- username
} else {
valid_user_conn <- ""
}
}
}

func valid_user(username string) string {
request := Request{Username: username}
json_request, err := json.Marshal(request)
jm, err := json.Marshal(request)
if err != nil {
panic(err)
}
response, err := http.Post(
ENDPOINT_URL,
CONTENT_TYPE,
bytes.NewBuffer(json_request),
bytes.NewBuffer(jm),
)
defer response.Body.Close()
if err != nil {
Expand All @@ -49,28 +59,54 @@ func valid_user(username string) int {
if json.Unmarshal(body, &r); err != nil {
panic(err)
}
return r.IfExistsResult
if r.IfExistsResult == 0 {
return username
} else {
return ""
}
}

func main() {
file := flag.String("f", "", "Specify a file")
username := flag.String("u", "", "Specify one username")
flag.Parse()

if *file != "" {
f, err := os.Open(*file)
if err != nil {
panic(err)
}
defer f.Close()

var line_count int
var usernames []string

scanner := bufio.NewScanner(f)
for scanner.Scan() {
username := scanner.Text()
if valid_user(username) == 0 {
usernames = append(usernames, scanner.Text())
line_count += 1
}

usernames_conn := make(chan string, line_count)
valid_user := make(chan string)
go func() {
for _, username := range usernames {
usernames_conn <- username
}
}()
for i := 0; i < line_count; i++ {
go valid_users(usernames_conn, valid_user)
}
for i := 0; i < line_count; i++ {
username := <-valid_user
if username != "" {
fmt.Println(username)
}
}
close(usernames_conn)
close(valid_user)
} else if *username != "" {
if valid_user(*username) == 0 {
if valid_user(*username) != "" {
fmt.Println(*username)
}
}
Expand Down

0 comments on commit b04335d

Please sign in to comment.