Skip to content

Commit 3a9e546

Browse files
authored
Allow disabling auto-reload (#155)
1 parent 1ec730d commit 3a9e546

1 file changed

Lines changed: 39 additions & 21 deletions

File tree

certs/manager.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ import (
4343
//
4444
// Manager will automatically reload certificates if the corresponding file changes.
4545
type Manager struct {
46-
lock sync.RWMutex
47-
certificates map[pair]*tls.Certificate // Mapping: certificate file name => TLS certificates
48-
defaultCert pair
49-
duration time.Duration
46+
lock sync.RWMutex
47+
certificates map[pair]*tls.Certificate // Mapping: certificate file name => TLS certificates
48+
defaultCert pair
49+
duration time.Duration
50+
disableAutoReload bool
5051

5152
loadX509KeyPair LoadX509KeyPairFunc
5253
done <-chan struct{}
@@ -68,7 +69,7 @@ type pair struct {
6869
// The certificate loaded from certFile is considered the default certificate.
6970
// If a client does not send the TLS SNI extension then Manager will return
7071
// this certificate.
71-
func NewManager(ctx context.Context, certFile, keyFile string, loadX509KeyPair LoadX509KeyPairFunc) (manager *Manager, err error) {
72+
func NewManager(ctx context.Context, certFile, keyFile string, loadX509KeyPair LoadX509KeyPairFunc, opts ...func(*Manager)) (manager *Manager, err error) {
7273
certFile, err = filepath.Abs(certFile)
7374
if err != nil {
7475
return nil, err
@@ -88,12 +89,27 @@ func NewManager(ctx context.Context, certFile, keyFile string, loadX509KeyPair L
8889
done: ctx.Done(),
8990
duration: 1 * time.Minute,
9091
}
92+
for _, opt := range opts {
93+
opt(manager)
94+
}
9195
if err := manager.AddCertificate(certFile, keyFile); err != nil {
9296
return nil, err
9397
}
9498
return manager, nil
9599
}
96100

101+
// WithDisableAutoReload disables automatic reloading
102+
func WithDisableAutoReload() func(*Manager) {
103+
return func(m *Manager) {
104+
m.disableAutoReload = true
105+
}
106+
}
107+
108+
// DisableAutoReload returns if automatic reloading is disabled
109+
func (m *Manager) DisableAutoReload() bool {
110+
return m.disableAutoReload
111+
}
112+
97113
// UpdateReloadDuration set custom symlink reload duration
98114
func (m *Manager) UpdateReloadDuration(t time.Duration) {
99115
if m == nil {
@@ -171,22 +187,24 @@ func (m *Manager) AddCertificate(certFile, keyFile string) (err error) {
171187
}
172188
m.certificates[p] = &certificate
173189

174-
if certFileIsLink && keyFileIsLink || isk8s {
175-
go m.watchSymlinks(p, m.reloader())
176-
} else {
177-
// Windows doesn't allow for watching file changes but instead allows
178-
// for directory changes only, while we can still watch for changes
179-
// on files on other platforms. Watch parent directory on all platforms
180-
// for simplicity.
181-
events := make(chan notify.EventInfo, 1)
182-
183-
if err = notify.Watch(filepath.Dir(certFile), events, eventWrite...); err != nil {
184-
return err
185-
}
186-
if err = notify.Watch(filepath.Dir(keyFile), events, eventWrite...); err != nil {
187-
return err
190+
if !m.DisableAutoReload() {
191+
if certFileIsLink && keyFileIsLink || isk8s {
192+
go m.watchSymlinks(p, m.reloader())
193+
} else {
194+
// Windows doesn't allow for watching file changes but instead allows
195+
// for directory changes only, while we can still watch for changes
196+
// on files on other platforms. Watch parent directory on all platforms
197+
// for simplicity.
198+
events := make(chan notify.EventInfo, 1)
199+
200+
if err = notify.Watch(filepath.Dir(certFile), events, eventWrite...); err != nil {
201+
return err
202+
}
203+
if err = notify.Watch(filepath.Dir(keyFile), events, eventWrite...); err != nil {
204+
return err
205+
}
206+
go m.watchFileEvents(p, events, m.reloader())
188207
}
189-
go m.watchFileEvents(p, events, m.reloader())
190208
}
191209
return nil
192210
}
@@ -202,7 +220,7 @@ func (m *Manager) reloader() <-chan struct{} {
202220
// ReloadOnSignal specifies one or more signals that will trigger certificates reloading.
203221
// If called multiple times with the same signal certificates
204222
func (m *Manager) ReloadOnSignal(sig ...os.Signal) {
205-
if m == nil {
223+
if m == nil || m.DisableAutoReload() {
206224
return
207225
}
208226

0 commit comments

Comments
 (0)