-
Notifications
You must be signed in to change notification settings - Fork 360
Improve concurrent package #2810
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9e195e3
feat(concurrent): add thread-safe methods and improve snapshot isolation
dgageot 964a3a8
feat: add LoadOrStore to concurrent.Map and use it in snapshot.Manager
dgageot 8fc2f08
fix: use concurrent.Buffer in boundedWriter to prevent data race
dgageot 359ae90
refactor: use concurrent.Map in conversationLockSet
dgageot db75beb
refactor: use concurrent.Map for running evals set
dgageot bb6d6e6
feat: add Clear method to concurrent.Map
dgageot b01b239
refactor: use concurrent.Map in hooks Registry
dgageot d72f65a
refactor: use concurrent.Map in model hook registry
dgageot 86c64a1
refactor: use concurrent.Map for markdown lexer cache
dgageot 9fd4be9
refactor: use concurrent.Map for chroma style cache
dgageot b6a827d
refactor: use concurrent.Map for editfile lexer cache
dgageot 9153372
refactor: use concurrent.Map for style sequence cache
dgageot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,90 @@ | ||
| package concurrent | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestBuffer_Write(t *testing.T) { | ||
| var b Buffer | ||
|
|
||
| n, err := b.Write([]byte("hello")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 5, n) | ||
|
|
||
| n, err = b.Write([]byte(" world")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 6, n) | ||
|
|
||
| assert.Equal(t, "hello world", b.String()) | ||
| } | ||
|
|
||
| func TestBuffer_Bytes(t *testing.T) { | ||
| var b Buffer | ||
| _, _ = b.Write([]byte("hello")) | ||
|
|
||
| got := b.Bytes() | ||
| assert.Equal(t, []byte("hello"), got) | ||
|
|
||
| // Mutating the returned slice must not affect the buffer. | ||
| got[0] = 'H' | ||
| assert.Equal(t, "hello", b.String()) | ||
| } | ||
|
|
||
| func TestBuffer_Len(t *testing.T) { | ||
| var b Buffer | ||
| assert.Equal(t, 0, b.Len()) | ||
|
|
||
| _, _ = b.Write([]byte("abc")) | ||
| assert.Equal(t, 3, b.Len()) | ||
|
|
||
| _, _ = b.Write([]byte("de")) | ||
| assert.Equal(t, 5, b.Len()) | ||
| } | ||
|
|
||
| func TestBuffer_Reset(t *testing.T) { | ||
| var b Buffer | ||
| _, _ = b.Write([]byte("hello")) | ||
|
|
||
| b.Reset() | ||
| assert.Equal(t, 0, b.Len()) | ||
| assert.Empty(t, b.String()) | ||
| } | ||
|
|
||
| func TestBuffer_Drain(t *testing.T) { | ||
| var b Buffer | ||
| _, _ = b.Write([]byte("hello")) | ||
|
|
||
| got := b.Drain() | ||
| assert.Equal(t, "hello", got) | ||
| assert.Equal(t, 0, b.Len()) | ||
| assert.Empty(t, b.String()) | ||
| } | ||
|
|
||
| func TestBuffer_Concurrent(t *testing.T) { | ||
| var b Buffer | ||
| var wg sync.WaitGroup | ||
|
|
||
| const writers = 100 | ||
| for i := range writers { | ||
| wg.Go(func() { | ||
| _, _ = b.Write(fmt.Appendf(nil, "%03d", i)) | ||
| }) | ||
| } | ||
|
|
||
| // Concurrent readers should not race with writers. | ||
| for range 50 { | ||
| wg.Go(func() { | ||
| _ = b.String() | ||
| _ = b.Len() | ||
| _ = b.Bytes() | ||
| }) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| assert.Equal(t, writers*3, b.Len()) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.