kasvith / simplelb

World's most dumbest Load Balancer

Home Page:https://kasvith.me/posts/lets-create-a-simple-lb-go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: Why time.After instead of time.Sleep ?

dev-drprasad opened this issue · comments

I learned lot from your article on load balancer. I see you used select with time.After to retry

simplelb/main.go

Lines 185 to 192 in fd45a98

if retries < 3 {
select {
case <-time.After(10 * time.Millisecond):
ctx := context.WithValue(request.Context(), Retry, retries+1)
proxy.ServeHTTP(writer, request.WithContext(ctx))
}
return
}

Why not just use time.Sleep ? Is there any difference between time.Sleep and select with time.After ?

Main purpose to add time.After to have a flexible structure to run other concurrent tasks in the error handler.

It's ok to use time.Sleep as well, in that case, we cant interrupt it