Skip to content

Commit

Permalink
Make the singlethreaded code simpler by using boolean guard (knative#…
Browse files Browse the repository at this point in the history
…10624)

* Make the singlethreaded code simpler by using boolean guard

No need an int for that. Just checking whther there's something
holding the semaphore is enough.

Change-Id: I455ff55a661b680290f58eb7e4a0f0d5ef5085a9

* return

Change-Id: I661bd1b95fe6f621100ba901c2f5ca6bd9b503ac
  • Loading branch information
vagababov authored Jan 26, 2021
1 parent 55a7edf commit c081a82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion test/e2e/subroutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestSubrouteLocalSTS(t *testing.T) { // We can't use a longer more descript
t.Log("Creating a Service for the helloworld test app.")
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: "helloworld",
Image: test.HelloWorld,
}

test.EnsureTearDown(t, clients, &names)
Expand Down
15 changes: 7 additions & 8 deletions test/test_images/singlethreaded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ import (
"knative.dev/serving/test"
)

var lockedFlag = atomic.NewInt32(-1)
var isLocked = atomic.NewBool(false)

func handler(w http.ResponseWriter, r *http.Request) {
v := lockedFlag.Inc() // Returns the new value.
defer lockedFlag.Dec()
if v > 0 {
// Return HTTP 500 if more than 1 request at a time gets in
w.WriteHeader(http.StatusInternalServerError)
if isLocked.CAS(false /*was unlocked*/, true /*lock*/) {
defer isLocked.Store(false)
time.Sleep(500 * time.Millisecond)
fmt.Fprintf(w, "One at a time")
return
}
time.Sleep(500 * time.Millisecond)
fmt.Fprintf(w, "One at a time")
// Return HTTP 500 if more than 1 request at a time gets in
w.WriteHeader(http.StatusInternalServerError)
}

func main() {
Expand Down

0 comments on commit c081a82

Please sign in to comment.