Skip to content

Commit 7948cb4

Browse files
authored
fix: use shared fsnotify watcher to avoid 'too many open files' error (#236)
1 parent 5d222ac commit 7948cb4

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

src/apiserver/pkg/apiserver/apiserver.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package apiserver
1919
import (
2020
"fmt"
2121

22+
"github.com/fsnotify/fsnotify"
2223
istiov1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
2324
admregv1 "k8s.io/api/admissionregistration/v1"
2425
authzv1 "k8s.io/api/authorization/v1"
@@ -169,6 +170,15 @@ func (c completedConfig) New() (*HigressServer, error) {
169170
}
170171
}
171172

173+
// Create shared watcher for file storage mode to avoid "too many open files"
174+
var sharedWatcher *fsnotify.Watcher
175+
if storageMode == options.Storage_File {
176+
sharedWatcher, err = fsnotify.NewWatcher()
177+
if err != nil {
178+
return nil, fmt.Errorf("failed to create shared file watcher: %v", err)
179+
}
180+
}
181+
172182
storageCreateFunc := func(
173183
groupResource schema.GroupResource,
174184
runtimeCodec runtime.Codec,
@@ -185,7 +195,7 @@ func (c completedConfig) New() (*HigressServer, error) {
185195
switch storageMode {
186196
case options.Storage_File:
187197
runtimeCodec = codec.NewFlatAwareCodec(groupResource, runtimeCodec)
188-
return registry.NewFileREST(groupResource, runtimeCodec, storageOptions.FileOptions.RootDir, extension, isNamespaced, singularName, newFunc, newListFunc, attrFunc)
198+
return registry.NewFileREST(groupResource, runtimeCodec, storageOptions.FileOptions.RootDir, extension, isNamespaced, singularName, newFunc, newListFunc, attrFunc, sharedWatcher)
189199
case options.Storage_Nacos:
190200
var encryptionKey []byte = nil
191201
if sensitive {

src/apiserver/pkg/registry/file_rest.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func NewFileREST(
5454
newFunc func() runtime.Object,
5555
newListFunc func() runtime.Object,
5656
attrFunc storage.AttrFunc,
57+
sharedWatcher *fsnotify.Watcher,
5758
) (REST, error) {
5859
if attrFunc == nil {
5960
if isNamespaced {
@@ -62,10 +63,6 @@ func NewFileREST(
6263
attrFunc = storage.DefaultClusterScopedAttr
6364
}
6465
}
65-
watcher, err := fsnotify.NewWatcher()
66-
if err != nil {
67-
return nil, err
68-
}
6966
// file REST
7067
f := &fileREST{
7168
TableConvertor: rest.NewDefaultTableConvertor(groupResource),
@@ -78,7 +75,7 @@ func NewFileREST(
7875
newFunc: newFunc,
7976
newListFunc: newListFunc,
8077
attrFunc: attrFunc,
81-
dirWatcher: watcher,
78+
dirWatcher: sharedWatcher,
8279
fileWatchers: make(map[string]*fileWatch, 10),
8380
}
8481
err = f.startDirWatcher()
@@ -115,7 +112,8 @@ func (f *fileREST) GetSingularName() string {
115112
}
116113

117114
func (f *fileREST) Destroy() {
118-
_ = f.dirWatcher.Close()
115+
// Don't close the watcher as it's shared across all fileREST instances
116+
// The watcher will be closed when the server shuts down
119117
}
120118

121119
func (f *fileREST) startDirWatcher() error {

0 commit comments

Comments
 (0)