pull/10138/merge
Joe Tsai 1 day ago committed by GitHub
commit e7f00416ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -89,8 +89,9 @@ func CurrentProfileKey(userID string) StateKey {
// StateStore persists state, and produces it back on request. // StateStore persists state, and produces it back on request.
// Implementations of StateStore are expected to be safe for concurrent use. // Implementations of StateStore are expected to be safe for concurrent use.
type StateStore interface { type StateStore interface {
// ReadState returns the bytes associated with ID. Returns (nil, // ReadState returns the bytes associated with ID.
// ErrStateNotExist) if the ID doesn't have associated state. // It returns (nil, ErrStateNotExist) if the ID doesn't have associated state.
// The returned value must not be mutated.
ReadState(id StateKey) ([]byte, error) ReadState(id StateKey) ([]byte, error)
// WriteState saves bs as the state associated with ID. // WriteState saves bs as the state associated with ID.
// //

@ -208,18 +208,24 @@ func (s *FileStore) ReadState(id ipn.StateKey) ([]byte, error) {
} }
// WriteState implements the StateStore interface. // WriteState implements the StateStore interface.
func (s *FileStore) WriteState(id ipn.StateKey, bs []byte) error { func (s *FileStore) WriteState(id ipn.StateKey, bs []byte) (err error) {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
if bytes.Equal(s.cache[id], bs) { bs0 := s.cache[id]
if bytes.Equal(bs0, bs) {
return nil return nil
} }
defer func() {
if err != nil {
s.cache[id] = bs0
}
}()
s.cache[id] = bytes.Clone(bs) s.cache[id] = bytes.Clone(bs)
bs, err := json.MarshalIndent(s.cache, "", " ") b, err := json.MarshalIndent(s.cache, "", " ")
if err != nil { if err != nil {
return err return err
} }
return atomicfile.WriteFile(s.path, bs, 0600) return atomicfile.WriteFile(s.path, b, 0600)
} }
func (s *FileStore) All() iter.Seq2[ipn.StateKey, []byte] { func (s *FileStore) All() iter.Seq2[ipn.StateKey, []byte] {

Loading…
Cancel
Save