mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-04 23:45:34 +00:00
0323dd01b2
This turns the checklocks workflow into a real check, and adds annotations to a few basic packages as a starting point. Updates #12625 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: I2b0185bae05a843b5257980fc6bde732b1bdd93f
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package mem provides an in-memory ipn.StateStore implementation.
|
|
package mem
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"sync"
|
|
|
|
"tailscale.com/ipn"
|
|
"tailscale.com/types/logger"
|
|
)
|
|
|
|
// New returns a new Store.
|
|
func New(logger.Logf, string) (ipn.StateStore, error) {
|
|
return new(Store), nil
|
|
}
|
|
|
|
// Store is an ipn.StateStore that keeps state in memory only.
|
|
type Store struct {
|
|
mu sync.Mutex
|
|
// +checklocks:mu
|
|
cache map[ipn.StateKey][]byte
|
|
}
|
|
|
|
func (s *Store) String() string { return "mem.Store" }
|
|
|
|
// ReadState implements the StateStore interface.
|
|
func (s *Store) ReadState(id ipn.StateKey) ([]byte, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
bs, ok := s.cache[id]
|
|
if !ok {
|
|
return nil, ipn.ErrStateNotExist
|
|
}
|
|
return bs, nil
|
|
}
|
|
|
|
// WriteState implements the StateStore interface.
|
|
func (s *Store) WriteState(id ipn.StateKey, bs []byte) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.cache == nil {
|
|
s.cache = map[ipn.StateKey][]byte{}
|
|
}
|
|
s.cache[id] = bytes.Clone(bs)
|
|
return nil
|
|
}
|
|
|
|
// LoadFromJSON attempts to unmarshal json content into the
|
|
// in-memory cache.
|
|
func (s *Store) LoadFromJSON(data []byte) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return json.Unmarshal(data, &s.cache)
|
|
}
|
|
|
|
// ExportToJSON exports the content of the cache to
|
|
// JSON formatted []byte.
|
|
func (s *Store) ExportToJSON() ([]byte, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if len(s.cache) == 0 {
|
|
// Avoid "null" serialization.
|
|
return []byte("{}"), nil
|
|
}
|
|
return json.MarshalIndent(s.cache, "", " ")
|
|
}
|