client: imp docs

This commit is contained in:
Stanislav Chzhen 2025-02-27 21:52:51 +03:00
parent e7d74931b1
commit f4b0caf5c8
2 changed files with 32 additions and 10 deletions

View file

@ -421,6 +421,7 @@ func (s *Storage) Add(ctx context.Context, p *Persistent) (err error) {
} }
s.index.add(p) s.index.add(p)
s.upstreamManager.updateCustomUpstreamConfig(p)
s.logger.DebugContext( s.logger.DebugContext(
ctx, ctx,
@ -430,8 +431,6 @@ func (s *Storage) Add(ctx context.Context, p *Persistent) (err error) {
"clients_count", s.index.size(), "clients_count", s.index.size(),
) )
s.upstreamManager.updateCustomUpstreamConfig(p)
return nil return nil
} }

View file

@ -1,6 +1,7 @@
package client package client
import ( import (
"fmt"
"log/slog" "log/slog"
"slices" "slices"
"time" "time"
@ -26,10 +27,26 @@ type CommonUpstreamConfig struct {
// customUpstreamConfig contains custom client upstream configuration and the // customUpstreamConfig contains custom client upstream configuration and the
// timestamp of the latest configuration update. // timestamp of the latest configuration update.
type customUpstreamConfig struct { type customUpstreamConfig struct {
proxyConf *proxy.CustomUpstreamConfig // proxyConf is the constructed upstream configuration for the [proxy],
commonConfUpdate time.Time // derived from the fields below. It is initialized on demand with
upstreams []string // [newCustomUpstreamConfig].
upstreamsCacheSize uint32 proxyConf *proxy.CustomUpstreamConfig
// commonConfUpdate is the timestamp of the latest configuration update,
// used to check against [upstreamManager.confUpdate] to determine if the
// configuration is up to date.
commonConfUpdate time.Time
// upstreams is the cached list of custom upstream DNS servers used for the
// configuration of proxyConf.
upstreams []string
// upstreamsCacheSize is the cached value of the cache size of the
// upstreams, used for the configuration of proxyConf.
upstreamsCacheSize uint32
// upstreamsCacheEnabled is the cached value indicating whether the cache of
// the upstreams is enabled for the configuration of proxyConf.
upstreamsCacheEnabled bool upstreamsCacheEnabled bool
// isChanged indicates whether the proxyConf needs to be updated. // isChanged indicates whether the proxyConf needs to be updated.
@ -135,15 +152,21 @@ func (m *upstreamManager) clearUpstreamCache() {
} }
} }
// remove deletes the custom client upstream configuration. // remove deletes the custom client upstream configuration and closes
// [customUpstreamConfig.proxyConf] if necessary.
func (m *upstreamManager) remove(uid UID) (err error) { func (m *upstreamManager) remove(uid UID) (err error) {
cliConf, ok := m.uidToCustomConf[uid] cliConf, ok := m.uidToCustomConf[uid]
if ok && cliConf.proxyConf != nil { if !ok {
return cliConf.proxyConf.Close() // TODO(s.chzhen): Consider panic.
return errors.Error("no associated custom client upstream config")
} }
delete(m.uidToCustomConf, uid) delete(m.uidToCustomConf, uid)
if cliConf.proxyConf != nil {
return cliConf.proxyConf.Close()
}
return nil return nil
} }
@ -184,7 +207,7 @@ func newCustomUpstreamConfig(
if err != nil { if err != nil {
// Should not happen because upstreams are already validated. See // Should not happen because upstreams are already validated. See
// [Persistent.validate]. // [Persistent.validate].
panic(err) panic(fmt.Errorf("creating custom upstream config: %w", err))
} }
return proxy.NewCustomUpstreamConfig( return proxy.NewCustomUpstreamConfig(