summaryrefslogtreecommitdiffhomepage
path: root/util/syspolicy/caching_handler.go
blob: fff96de1c84c803a3e8c16db6fb52a95b7acd2a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

package syspolicy

import (
	"errors"
	"sync"
)

// CachingHandler is a handler that reads policies from an underlying handler the first time each key is requested
// and permanently caches the result unless there is an error. If there is an ErrNoSuchKey error, that result is cached,
// otherwise the actual error is returned and the next read for that key will retry using the handler.
type CachingHandler struct {
	mu       sync.Mutex
	strings  map[string]string
	uint64s  map[string]uint64
	bools    map[string]bool
	notFound map[string]bool
	handler  Handler
}

// NewCachingHandler creates a CachingHandler given a handler.
func NewCachingHandler(handler Handler) *CachingHandler {
	return &CachingHandler{
		handler:  handler,
		strings:  make(map[string]string),
		uint64s:  make(map[string]uint64),
		bools:    make(map[string]bool),
		notFound: make(map[string]bool),
	}
}

// ReadString reads the policy settings value string given the key.
// ReadString first reads from the handler's cache before resorting to using the handler.
func (ch *CachingHandler) ReadString(key string) (string, error) {
	ch.mu.Lock()
	defer ch.mu.Unlock()
	if val, ok := ch.strings[key]; ok {
		return val, nil
	}
	if notFound := ch.notFound[key]; notFound {
		return "", ErrNoSuchKey
	}
	val, err := ch.handler.ReadString(key)
	if errors.Is(err, ErrNoSuchKey) {
		ch.notFound[key] = true
		return "", err
	} else if err != nil {
		return "", err
	}
	ch.strings[key] = val
	return val, nil
}

// ReadUInt64 reads the policy settings uint64 value given the key.
// ReadUInt64 first reads from the handler's cache before resorting to using the handler.
func (ch *CachingHandler) ReadUInt64(key string) (uint64, error) {
	ch.mu.Lock()
	defer ch.mu.Unlock()
	if val, ok := ch.uint64s[key]; ok {
		return val, nil
	}
	if notFound := ch.notFound[key]; notFound {
		return 0, ErrNoSuchKey
	}
	val, err := ch.handler.ReadUInt64(key)
	if errors.Is(err, ErrNoSuchKey) {
		ch.notFound[key] = true
		return 0, err
	} else if err != nil {
		return 0, err
	}
	ch.uint64s[key] = val
	return val, nil
}

// ReadBoolean reads the policy settings boolean value given the key.
// ReadBoolean first reads from the handler's cache before resorting to using the handler.
func (ch *CachingHandler) ReadBoolean(key string) (bool, error) {
	ch.mu.Lock()
	defer ch.mu.Unlock()
	if val, ok := ch.bools[key]; ok {
		return val, nil
	}
	if notFound := ch.notFound[key]; notFound {
		return false, ErrNoSuchKey
	}
	val, err := ch.handler.ReadBoolean(key)
	if errors.Is(err, ErrNoSuchKey) {
		ch.notFound[key] = true
		return false, err
	} else if err != nil {
		return false, err
	}
	ch.bools[key] = val
	return val, nil
}