34
34
35
35
var childProcPathFmt = "/proc/%[1]v/task/%[1]v/children"
36
36
37
- //go :generate go run github.com/maxbrunsfeld/counterfeiter/v6 . nginxPlusClient
37
+ //counterfeiter :generate . nginxPlusClient
38
38
39
39
type nginxPlusClient interface {
40
40
UpdateHTTPServers (
@@ -49,21 +49,19 @@ type nginxPlusClient interface {
49
49
GetUpstreams () (* ngxclient.Upstreams , error )
50
50
}
51
51
52
- //go :generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ProcessHandler
52
+ //counterfeiter :generate . ProcessHandler
53
53
54
54
type ProcessHandler interface {
55
55
FindMainProcess (
56
56
ctx context.Context ,
57
- checkFile CheckFileFunc ,
58
- readFile ReadFileFunc ,
59
57
timeout time.Duration ,
60
58
) (int , error )
61
- ReadFile (file string ) ([]byte , error )
59
+ readFile (file string ) ([]byte , error )
62
60
Kill (pid int ) error
63
61
EnsureNginxRunning (ctx context.Context ) error
64
62
}
65
63
66
- //go :generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Manager
64
+ //counterfeiter :generate . Manager
67
65
68
66
// Manager manages the runtime of NGINX.
69
67
type Manager interface {
@@ -79,9 +77,9 @@ type Manager interface {
79
77
GetUpstreams () (ngxclient.Upstreams , error )
80
78
}
81
79
82
- //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . MetricsCollector
83
-
84
80
// MetricsCollector is an interface for the metrics of the NGINX runtime manager.
81
+ //
82
+ //counterfeiter:generate . MetricsCollector
85
83
type MetricsCollector interface {
86
84
IncReloadCount ()
87
85
IncReloadErrors ()
@@ -122,13 +120,13 @@ func (m *ManagerImpl) IsPlus() bool {
122
120
func (m * ManagerImpl ) Reload (ctx context.Context , configVersion int ) error {
123
121
start := time .Now ()
124
122
// We find the main NGINX PID on every reload because it will change if the NGINX container is restarted.
125
- pid , err := m .processHandler .FindMainProcess (ctx , os . Stat , os . ReadFile , pidFileTimeout )
123
+ pid , err := m .processHandler .FindMainProcess (ctx , pidFileTimeout )
126
124
if err != nil {
127
125
return fmt .Errorf ("failed to find NGINX main process: %w" , err )
128
126
}
129
127
130
128
childProcFile := fmt .Sprintf (childProcPathFmt , pid )
131
- previousChildProcesses , err := m .processHandler .ReadFile (childProcFile )
129
+ previousChildProcesses , err := m .processHandler .readFile (childProcFile )
132
130
if err != nil {
133
131
return err
134
132
}
@@ -191,20 +189,18 @@ func (m *ManagerImpl) GetUpstreams() (ngxclient.Upstreams, error) {
191
189
return * upstreams , nil
192
190
}
193
191
194
- type ProcessHandlerImpl struct {}
192
+ type NewProcessHandlerImpl struct {}
195
193
196
194
// EnsureNginxRunning ensures NGINX is running by locating the main process.
197
- func (p * ProcessHandlerImpl ) EnsureNginxRunning (ctx context.Context ) error {
198
- if _ , err := p .FindMainProcess (ctx , os . Stat , os . ReadFile , pidFileTimeout ); err != nil {
195
+ func (p * NewProcessHandlerImpl ) EnsureNginxRunning (ctx context.Context ) error {
196
+ if _ , err := p .FindMainProcess (ctx , pidFileTimeout ); err != nil {
199
197
return fmt .Errorf ("failed to find NGINX main process: %w" , err )
200
198
}
201
199
return nil
202
200
}
203
201
204
- func (p * ProcessHandlerImpl ) FindMainProcess (
202
+ func (p * NewProcessHandlerImpl ) FindMainProcess (
205
203
ctx context.Context ,
206
- checkFile CheckFileFunc ,
207
- readFile ReadFileFunc ,
208
204
timeout time.Duration ,
209
205
) (int , error ) {
210
206
ctx , cancel := context .WithTimeout (ctx , timeout )
@@ -215,7 +211,7 @@ func (p *ProcessHandlerImpl) FindMainProcess(
215
211
500 * time .Millisecond ,
216
212
true , /* poll immediately */
217
213
func (_ context.Context ) (bool , error ) {
218
- _ , err := checkFile (PidFile )
214
+ _ , err := p . checkFile (PidFile )
219
215
if err == nil {
220
216
return true , nil
221
217
}
@@ -228,7 +224,7 @@ func (p *ProcessHandlerImpl) FindMainProcess(
228
224
return 0 , err
229
225
}
230
226
231
- content , err := readFile (PidFile )
227
+ content , err := p . readFile (PidFile )
232
228
if err != nil {
233
229
return 0 , err
234
230
}
@@ -241,10 +237,10 @@ func (p *ProcessHandlerImpl) FindMainProcess(
241
237
return pid , nil
242
238
}
243
239
244
- func (p * ProcessHandlerImpl ) ReadFile (file string ) ([]byte , error ) {
245
- return os . ReadFile (file )
240
+ func (p * NewProcessHandlerImpl ) readFile (file string ) ([]byte , error ) {
241
+ return p . readFile (file )
246
242
}
247
243
248
- func (p * ProcessHandlerImpl ) Kill (pid int ) error {
244
+ func (p * NewProcessHandlerImpl ) Kill (pid int ) error {
249
245
return syscall .Kill (pid , syscall .SIGHUP )
250
246
}
0 commit comments