-
Notifications
You must be signed in to change notification settings - Fork 816
Flush tokens to disk #1750
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
Flush tokens to disk #1750
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b31ec6e
Flush tokens to file
codesome 1d3eeaf
Read tokens from file on startup
codesome e8df9b6
Fix tests
codesome c2c43dc
Don't flush on empty directory flag
codesome 7e2ad44
Unit test
codesome 4fbd89d
Fix some bugs
codesome 8aa452f
Fix integration test
codesome aa67711
Fix review comments
codesome 7ab02ff
Add interface for tokens with a SimpleListTokens type
codesome c59cf80
Merge remote-tracking branch 'upstream/master' into tokens-file
codesome 5c33c1c
Remove Tokens interface
codesome 4b8a4c1
Merge remote-tracking branch 'upstream/master' into tokens-file
codesome d00ff97
Fix review comments
codesome f4f886c
Fix review comments
codesome 39a2485
Vendor fix
codesome 2e9e6f3
Fix review comments
codesome 7d73e07
Fix vendoring
codesome 58b32d0
Merge remote-tracking branch 'upstream/master' into tokens-file
codesome 7a16142
Fix Marco's comments
codesome 41d37d8
Fix Peter's comments
codesome 8838d97
Fix Goutham's and Peter's comment
codesome 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,80 @@ | ||
package ring | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
// Tokens is a simple list of tokens. | ||
type Tokens []uint32 | ||
|
||
// StoreToFile stores the tokens in the given directory. | ||
func (t Tokens) StoreToFile(tokenFilePath string) error { | ||
if tokenFilePath == "" { | ||
return errors.New("path is empty") | ||
} | ||
|
||
// If any operations failed further in the function, we keep the temporary | ||
// file hanging around for debugging. | ||
f, err := ioutil.TempFile(os.TempDir(), "tokens") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
// If the file was not closed, then there must already be an error, hence ignore | ||
// the error (if any) from f.Close(). If the file was already closed, then | ||
// we would ignore the error in that case too. | ||
f.Close() | ||
codesome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}() | ||
|
||
b, err := t.Marshal() | ||
if err != nil { | ||
return err | ||
} | ||
if _, err = f.Write(b); err != nil { | ||
return err | ||
} | ||
|
||
if err := f.Close(); err != nil { | ||
codesome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
|
||
// Tokens successfully written, replace the temporary file with the actual file path. | ||
return os.Rename(f.Name(), tokenFilePath) | ||
} | ||
|
||
// LoadTokensFromFile loads tokens from given file path. | ||
func LoadTokensFromFile(tokenFilePath string) (Tokens, error) { | ||
b, err := ioutil.ReadFile(tokenFilePath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
codesome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
var t Tokens | ||
err = t.Unmarshal(b) | ||
return t, err | ||
} | ||
|
||
// Marshal encodes the tokens into JSON. | ||
func (t Tokens) Marshal() ([]byte, error) { | ||
codesome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return json.Marshal(tokensJSON{Tokens: t}) | ||
} | ||
|
||
// Unmarshal reads the tokens from JSON byte stream. | ||
func (t *Tokens) Unmarshal(b []byte) error { | ||
tj := tokensJSON{} | ||
if err := json.Unmarshal(b, &tj); err != nil { | ||
return err | ||
} | ||
*t = Tokens(tj.Tokens) | ||
return nil | ||
} | ||
|
||
type tokensJSON struct { | ||
Tokens []uint32 `json:"tokens"` | ||
} |
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,22 @@ | ||
package ring | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTokenSerialization(t *testing.T) { | ||
tokens := make(Tokens, 512) | ||
for i := 0; i < 512; i++ { | ||
tokens = append(tokens, uint32(rand.Int31())) | ||
} | ||
|
||
b, err := tokens.Marshal() | ||
require.NoError(t, err) | ||
|
||
var unmarshaledTokens Tokens | ||
require.NoError(t, unmarshaledTokens.Unmarshal(b)) | ||
require.Equal(t, tokens, unmarshaledTokens) | ||
} |
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.