-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement rate limiting for storage writers
Signed-off-by: Isaac Hier <[email protected]>
- Loading branch information
Isaac Hier
committed
Dec 5, 2018
1 parent
6237e2f
commit 891eef8
Showing
8 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,51 @@ | ||
// Copyright (c) 2018 Uber Technologies, Inc. | ||
// | ||
// 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 ratelimit | ||
|
||
import ( | ||
"errors" | ||
|
||
rlImpl "go.uber.org/ratelimit" | ||
|
||
"github.com/jaegertracing/jaeger/model" | ||
"github.com/jaegertracing/jaeger/storage/spanstore" | ||
) | ||
|
||
var errInvalidRate = errors.New("rate must be a positive integer") | ||
|
||
type rateLimitedWriter struct { | ||
writer spanstore.Writer | ||
limiter rlImpl.Limiter | ||
} | ||
|
||
// NewRateLimitedWriter decorates a Writer with a rate limiter in order to limit | ||
// the number of writes per second. | ||
func NewRateLimitedWriter(writer spanstore.Writer, rate int) (spanstore.Writer, error) { | ||
if rate <= 0 { | ||
return nil, errInvalidRate | ||
} | ||
|
||
return &rateLimitedWriter{ | ||
writer: writer, | ||
limiter: rlImpl.New(rate), | ||
}, nil | ||
} | ||
|
||
// WriteSpan wraps the write span method of the inner writer, but invokes the | ||
// rate limiter before each write. | ||
func (r *rateLimitedWriter) WriteSpan(s *model.Span) error { | ||
r.limiter.Take() | ||
return r.writer.WriteSpan(s) | ||
} |
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,76 @@ | ||
// Copyright (c) 2018 Uber Technologies, Inc. | ||
// | ||
// 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 ratelimit | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/jaegertracing/jaeger/model" | ||
) | ||
|
||
type mockWriter struct { | ||
expectedError error | ||
} | ||
|
||
func (m *mockWriter) WriteSpan(s *model.Span) error { | ||
return m.expectedError | ||
} | ||
|
||
type mockRateLimiter struct { | ||
calls int | ||
} | ||
|
||
func (m *mockRateLimiter) Take() time.Time { | ||
m.calls++ | ||
return time.Time{} | ||
} | ||
|
||
func TestRateLimitedWriter(t *testing.T) { | ||
writer := &mockWriter{} | ||
decoratedWriter, err := NewRateLimitedWriter(writer, 10) | ||
require.NoError(t, err) | ||
var rateLimiter mockRateLimiter | ||
decoratedWriter.(*rateLimitedWriter).limiter = &rateLimiter | ||
require.NotEqual(t, writer, decoratedWriter) | ||
err = decoratedWriter.WriteSpan(&model.Span{}) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, rateLimiter.calls) | ||
} | ||
|
||
func TestRateLimitedWriterInvalidWritesPerSecond(t *testing.T) { | ||
writer := &mockWriter{} | ||
decoratedWriter, err := NewRateLimitedWriter(writer, 0) | ||
require.Error(t, err) | ||
require.Nil(t, decoratedWriter) | ||
} | ||
|
||
func TestRateLimitedWriterWithWriteError(t *testing.T) { | ||
var fakeError = errors.New("test") | ||
writer := &mockWriter{ | ||
expectedError: fakeError, | ||
} | ||
decoratedWriter, err := NewRateLimitedWriter(writer, 5) | ||
require.NoError(t, err) | ||
err = decoratedWriter.WriteSpan(nil) | ||
require.Error(t, err) | ||
require.Equal(t, fakeError, err) | ||
writer.expectedError = nil | ||
err = decoratedWriter.WriteSpan(nil) | ||
require.NoError(t, err) | ||
} |