mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2024-06-08 08:52:27 +02:00
Close file watcher on shutdown to prevent goroutine leaks.
This commit is contained in:
parent
bf68a15943
commit
9adb762ccf
|
@ -66,6 +66,11 @@ func NewCertificateReloader(certFile string, keyFile string) (*CertificateReload
|
||||||
return reloader, nil
|
return reloader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *CertificateReloader) Close() {
|
||||||
|
r.keyWatcher.Close()
|
||||||
|
r.certWatcher.Close()
|
||||||
|
}
|
||||||
|
|
||||||
func (r *CertificateReloader) reload(filename string) {
|
func (r *CertificateReloader) reload(filename string) {
|
||||||
log.Printf("reloading certificate from %s with %s", r.certFile, r.keyFile)
|
log.Printf("reloading certificate from %s with %s", r.certFile, r.keyFile)
|
||||||
pair, err := tls.LoadX509KeyPair(r.certFile, r.keyFile)
|
pair, err := tls.LoadX509KeyPair(r.certFile, r.keyFile)
|
||||||
|
@ -135,6 +140,10 @@ func NewCertPoolReloader(certFile string) (*CertPoolReloader, error) {
|
||||||
return reloader, nil
|
return reloader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *CertPoolReloader) Close() {
|
||||||
|
r.certWatcher.Close()
|
||||||
|
}
|
||||||
|
|
||||||
func (r *CertPoolReloader) reload(filename string) {
|
func (r *CertPoolReloader) reload(filename string) {
|
||||||
log.Printf("reloading certificate pool from %s", r.certFile)
|
log.Printf("reloading certificate pool from %s", r.certFile)
|
||||||
pool, err := loadCertPool(r.certFile)
|
pool, err := loadCertPool(r.certFile)
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
package signaling
|
package signaling
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -55,6 +56,8 @@ type FileWatcher struct {
|
||||||
callback FileWatcherCallback
|
callback FileWatcherCallback
|
||||||
|
|
||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
|
closeCtx context.Context
|
||||||
|
closeFunc context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileWatcher(filename string, callback FileWatcherCallback) (*FileWatcher, error) {
|
func NewFileWatcher(filename string, callback FileWatcherCallback) (*FileWatcher, error) {
|
||||||
|
@ -78,17 +81,23 @@ func NewFileWatcher(filename string, callback FileWatcherCallback) (*FileWatcher
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
closeCtx, closeFunc := context.WithCancel(context.Background())
|
||||||
|
|
||||||
w := &FileWatcher{
|
w := &FileWatcher{
|
||||||
filename: filename,
|
filename: filename,
|
||||||
target: realFilename,
|
target: realFilename,
|
||||||
callback: callback,
|
callback: callback,
|
||||||
watcher: watcher,
|
watcher: watcher,
|
||||||
|
|
||||||
|
closeCtx: closeCtx,
|
||||||
|
closeFunc: closeFunc,
|
||||||
}
|
}
|
||||||
go w.run()
|
go w.run()
|
||||||
return w, nil
|
return w, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileWatcher) Close() error {
|
func (f *FileWatcher) Close() error {
|
||||||
|
f.closeFunc()
|
||||||
return f.watcher.Close()
|
return f.watcher.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,6 +161,8 @@ func (f *FileWatcher) run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Error watching %s: %s", f.filename, err)
|
log.Printf("Error watching %s: %s", f.filename, err)
|
||||||
|
case <-f.closeCtx.Done():
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ import (
|
||||||
clientv3 "go.etcd.io/etcd/client/v3"
|
clientv3 "go.etcd.io/etcd/client/v3"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
codes "google.golang.org/grpc/codes"
|
codes "google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
"google.golang.org/grpc/resolver"
|
"google.golang.org/grpc/resolver"
|
||||||
status "google.golang.org/grpc/status"
|
status "google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
@ -275,6 +276,7 @@ type GrpcClients struct {
|
||||||
targetPrefix string
|
targetPrefix string
|
||||||
targetInformation map[string]*GrpcTargetInformationEtcd
|
targetInformation map[string]*GrpcTargetInformationEtcd
|
||||||
dialOptions atomic.Value // []grpc.DialOption
|
dialOptions atomic.Value // []grpc.DialOption
|
||||||
|
creds credentials.TransportCredentials
|
||||||
|
|
||||||
initializedCtx context.Context
|
initializedCtx context.Context
|
||||||
initializedFunc context.CancelFunc
|
initializedFunc context.CancelFunc
|
||||||
|
@ -308,6 +310,13 @@ func (c *GrpcClients) load(config *goconf.ConfigFile, fromReload bool) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.creds != nil {
|
||||||
|
if cr, ok := c.creds.(*reloadableCredentials); ok {
|
||||||
|
cr.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.creds = creds
|
||||||
|
|
||||||
opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
|
opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
|
||||||
c.dialOptions.Store(opts)
|
c.dialOptions.Store(opts)
|
||||||
|
|
||||||
|
@ -795,6 +804,11 @@ func (c *GrpcClients) Close() {
|
||||||
if c.etcdClient != nil {
|
if c.etcdClient != nil {
|
||||||
c.etcdClient.RemoveListener(c)
|
c.etcdClient.RemoveListener(c)
|
||||||
}
|
}
|
||||||
|
if c.creds != nil {
|
||||||
|
if cr, ok := c.creds.(*reloadableCredentials); ok {
|
||||||
|
cr.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
c.closeFunc()
|
c.closeFunc()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -125,6 +125,15 @@ func (c *reloadableCredentials) OverrideServerName(serverName string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *reloadableCredentials) Close() {
|
||||||
|
if c.loader != nil {
|
||||||
|
c.loader.Close()
|
||||||
|
}
|
||||||
|
if c.pool != nil {
|
||||||
|
c.pool.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewReloadableCredentials(config *goconf.ConfigFile, server bool) (credentials.TransportCredentials, error) {
|
func NewReloadableCredentials(config *goconf.ConfigFile, server bool) (credentials.TransportCredentials, error) {
|
||||||
var prefix string
|
var prefix string
|
||||||
var caPrefix string
|
var caPrefix string
|
||||||
|
|
|
@ -108,6 +108,9 @@ func (s *GrpcServer) Run() error {
|
||||||
|
|
||||||
func (s *GrpcServer) Close() {
|
func (s *GrpcServer) Close() {
|
||||||
s.conn.GracefulStop()
|
s.conn.GracefulStop()
|
||||||
|
if cr, ok := s.creds.(*reloadableCredentials); ok {
|
||||||
|
cr.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GrpcServer) LookupSessionId(ctx context.Context, request *LookupSessionIdRequest) (*LookupSessionIdReply, error) {
|
func (s *GrpcServer) LookupSessionId(ctx context.Context, request *LookupSessionIdRequest) (*LookupSessionIdReply, error) {
|
||||||
|
|
Loading…
Reference in a new issue