Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Faster Resposne from email-verifier when checking list of emails. #111

Open
shihabmi7 opened this issue Jan 30, 2024 · 2 comments

Comments

@shihabmi7
Copy link

shihabmi7 commented Jan 30, 2024

well,
First thanks for contributors for awesome library.

I already created a Go application using this library. I created a post endpoint that takes only a list of emails. Then Using this library I generate a custom validation response. But for a list of 10 emails, it took 32~40 seconds. I want to reduce that one. Note that my internet speed is well. Sample code for lists of email verifications.

Thanks in Advance.


func ProcessEmailList(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

	w.Header().Set("Content-Type", "application/json")

	errorResponse := ErrorResponse{
		Code:    400,
		Message: "Error",
	}

	// Read the request body
	body, err := io.ReadAll(r.Body)
	if err != nil {
		errorResponse.Message = err.Error()
		error_json, _ := json.Marshal(errorResponse)
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprint(w, string(error_json))

		return
	}

	// Unmarshal the JSON request body into EmailListRequest struct
	var request EmailListRequest
	if err := json.Unmarshal(body, &request); err != nil {
		errorResponse.Message = "Error parsing JSON"
		error_json, _ := json.Marshal(errorResponse)
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprint(w, string(error_json))
		return
	}

	// Split the comma-separated string into a slice of email addresses
	emails := strings.Split(request.Emails, ",")
	responseList := []EmailRespose{}

	// Process the list of emails
	for _, email := range emails {
		responseList = append(responseList, SimplyVerifyEmail(email))
	}

	// Respond to the client
	w.WriteHeader(http.StatusOK)
	jsonResponse, err := json.Marshal(responseList)
	fmt.Fprint(w, string(jsonResponse))
}

@guide-giangnt
Copy link

guide-giangnt commented Mar 27, 2024

I tried applying multithread with Goroutines
Performance improved a bit

	var wg sync.WaitGroup
	wg.Add(n)
	resultsCh := make(chan emailverifier.Result)

	for _, email := range emails {
		println(email)
		go worker(email, resultsCh, &wg)
	}

	// Start a goroutine to collect results from workers
	go func() {
		wg.Wait()
		close(resultsCh)
	}()

	var results []emailverifier.Result
	for result := range resultsCh {
		fmt.Println(result)
		results = append(results, result)
	}
func worker(email string, results chan<- emailverifier.Result, wg *sync.WaitGroup) {
	defer wg.Done()
	result, err := verifier.Verify(email)
	if err != nil {
		fmt.Println("verify email address failed, error is: ", err)
	}
	results <- *result
}

@shihabmi7
Copy link
Author

@guide-giangnt thanks for sharing. I will update you, after testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants