-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16161 from serathius/robustness-limit-non-unique-…
…writes tests/robustness: Prevent to many concurrent non-unique writes which are causing linearization to timeout
- Loading branch information
Showing
5 changed files
with
170 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2023 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package traffic | ||
|
||
func NewConcurrencyLimiter(size int) ConcurrencyLimiter { | ||
return &concurrencyLimiter{ | ||
ch: make(chan struct{}, size), | ||
} | ||
} | ||
|
||
type ConcurrencyLimiter interface { | ||
Take() bool | ||
Return() | ||
} | ||
|
||
type concurrencyLimiter struct { | ||
ch chan struct{} | ||
} | ||
|
||
func (c *concurrencyLimiter) Take() bool { | ||
select { | ||
case c.ch <- struct{}{}: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func (c *concurrencyLimiter) Return() { | ||
select { | ||
case _ = <-c.ch: | ||
default: | ||
panic("Call to Return() without a successful Take") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2023 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package traffic | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func TestLimiter(t *testing.T) { | ||
limiter := NewConcurrencyLimiter(3) | ||
counter := &atomic.Int64{} | ||
g := errgroup.Group{} | ||
for i := 0; i < 10; i++ { | ||
g.Go(func() error { | ||
if limiter.Take() { | ||
counter.Add(1) | ||
} | ||
return nil | ||
}) | ||
} | ||
g.Wait() | ||
assert.Equal(t, 3, int(counter.Load())) | ||
assert.False(t, limiter.Take()) | ||
|
||
limiter.Return() | ||
counter.Store(0) | ||
for i := 0; i < 10; i++ { | ||
g.Go(func() error { | ||
if limiter.Take() { | ||
counter.Add(1) | ||
} | ||
return nil | ||
}) | ||
} | ||
g.Wait() | ||
assert.Equal(t, 1, int(counter.Load())) | ||
assert.False(t, limiter.Take()) | ||
|
||
limiter.Return() | ||
limiter.Return() | ||
limiter.Return() | ||
assert.Panics(t, func() { | ||
limiter.Return() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters