Skip to content

Commit 0ecf5d1

Browse files
committed
adding first part of resolving final comments
1 parent 9777319 commit 0ecf5d1

File tree

5 files changed

+44
-52
lines changed

5 files changed

+44
-52
lines changed

Diff for: internal/mode/static/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func StartManager(cfg config.Config) error {
144144
return fmt.Errorf("cannot clear NGINX configuration folders: %w", err)
145145
}
146146

147-
var processHandler = &ngxruntime.ProcessHandlerImpl{}
147+
processHandler := &ngxruntime.NewProcessHandlerImpl{}
148148

149149
// Ensure NGINX is running before registering metrics & starting the manager.
150150
if err := processHandler.EnsureNginxRunning(ctx); err != nil {

Diff for: internal/mode/static/nginx/runtime/manager.go

+17-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type (
3434

3535
var childProcPathFmt = "/proc/%[1]v/task/%[1]v/children"
3636

37-
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . nginxPlusClient
37+
//counterfeiter:generate . nginxPlusClient
3838

3939
type nginxPlusClient interface {
4040
UpdateHTTPServers(
@@ -49,21 +49,19 @@ type nginxPlusClient interface {
4949
GetUpstreams() (*ngxclient.Upstreams, error)
5050
}
5151

52-
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ProcessHandler
52+
//counterfeiter:generate . ProcessHandler
5353

5454
type ProcessHandler interface {
5555
FindMainProcess(
5656
ctx context.Context,
57-
checkFile CheckFileFunc,
58-
readFile ReadFileFunc,
5957
timeout time.Duration,
6058
) (int, error)
61-
ReadFile(file string) ([]byte, error)
59+
readFile(file string) ([]byte, error)
6260
Kill(pid int) error
6361
EnsureNginxRunning(ctx context.Context) error
6462
}
6563

66-
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Manager
64+
//counterfeiter:generate . Manager
6765

6866
// Manager manages the runtime of NGINX.
6967
type Manager interface {
@@ -79,9 +77,9 @@ type Manager interface {
7977
GetUpstreams() (ngxclient.Upstreams, error)
8078
}
8179

82-
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . MetricsCollector
83-
8480
// MetricsCollector is an interface for the metrics of the NGINX runtime manager.
81+
//
82+
//counterfeiter:generate . MetricsCollector
8583
type MetricsCollector interface {
8684
IncReloadCount()
8785
IncReloadErrors()
@@ -122,13 +120,13 @@ func (m *ManagerImpl) IsPlus() bool {
122120
func (m *ManagerImpl) Reload(ctx context.Context, configVersion int) error {
123121
start := time.Now()
124122
// 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)
126124
if err != nil {
127125
return fmt.Errorf("failed to find NGINX main process: %w", err)
128126
}
129127

130128
childProcFile := fmt.Sprintf(childProcPathFmt, pid)
131-
previousChildProcesses, err := m.processHandler.ReadFile(childProcFile)
129+
previousChildProcesses, err := m.processHandler.readFile(childProcFile)
132130
if err != nil {
133131
return err
134132
}
@@ -191,20 +189,18 @@ func (m *ManagerImpl) GetUpstreams() (ngxclient.Upstreams, error) {
191189
return *upstreams, nil
192190
}
193191

194-
type ProcessHandlerImpl struct{}
192+
type NewProcessHandlerImpl struct{}
195193

196194
// 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 {
199197
return fmt.Errorf("failed to find NGINX main process: %w", err)
200198
}
201199
return nil
202200
}
203201

204-
func (p *ProcessHandlerImpl) FindMainProcess(
202+
func (p *NewProcessHandlerImpl) FindMainProcess(
205203
ctx context.Context,
206-
checkFile CheckFileFunc,
207-
readFile ReadFileFunc,
208204
timeout time.Duration,
209205
) (int, error) {
210206
ctx, cancel := context.WithTimeout(ctx, timeout)
@@ -215,7 +211,7 @@ func (p *ProcessHandlerImpl) FindMainProcess(
215211
500*time.Millisecond,
216212
true, /* poll immediately */
217213
func(_ context.Context) (bool, error) {
218-
_, err := checkFile(PidFile)
214+
_, err := p.checkFile(PidFile)
219215
if err == nil {
220216
return true, nil
221217
}
@@ -228,7 +224,7 @@ func (p *ProcessHandlerImpl) FindMainProcess(
228224
return 0, err
229225
}
230226

231-
content, err := readFile(PidFile)
227+
content, err := p.readFile(PidFile)
232228
if err != nil {
233229
return 0, err
234230
}
@@ -241,10 +237,10 @@ func (p *ProcessHandlerImpl) FindMainProcess(
241237
return pid, nil
242238
}
243239

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)
246242
}
247243

248-
func (p *ProcessHandlerImpl) Kill(pid int) error {
244+
func (p *NewProcessHandlerImpl) Kill(pid int) error {
249245
return syscall.Kill(pid, syscall.SIGHUP)
250246
}

Diff for: internal/mode/static/nginx/runtime/manager_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ func TestFindMainProcess(t *testing.T) {
239239
for _, test := range tests {
240240
t.Run(test.name, func(t *testing.T) {
241241
g := NewWithT(t)
242-
p := runtime.ProcessHandlerImpl{}
243-
result, err := p.FindMainProcess(test.ctx, test.checkFile, test.readFile, 2*time.Millisecond)
242+
p := runtime.NewProcessHandlerImpl{}
243+
result, err := p.FindMainProcess(test.ctx, 2*time.Millisecond)
244244

245245
if test.expectError {
246246
g.Expect(err).To(HaveOccurred())

Diff for: internal/mode/static/nginx/runtime/runtimefakes/fake_process_handler.go

+17-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/mode/static/nginx/runtime/verify.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ type nginxConfigVerifier interface {
3333
EnsureConfigVersion(ctx context.Context, expectedVersion int) error
3434
}
3535

36-
// VerifyClientImpl is a client for verifying the config version.
37-
type VerifyClientImpl struct {
36+
// VerifyClient is a client for verifying the config version.
37+
type VerifyClient struct {
3838
client *http.Client
3939
timeout time.Duration
4040
}
4141

4242
// NewVerifyClient returns a new client pointed at the config version socket.
43-
func NewVerifyClient(timeout time.Duration) *VerifyClientImpl {
44-
return &VerifyClientImpl{
43+
func NewVerifyClient(timeout time.Duration) *VerifyClient {
44+
return &VerifyClient{
4545
client: &http.Client{
4646
Transport: &http.Transport{
4747
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
@@ -55,7 +55,7 @@ func NewVerifyClient(timeout time.Duration) *VerifyClientImpl {
5555

5656
// GetConfigVersion gets the version number that we put in the nginx config to verify that we're using
5757
// the correct config.
58-
func (c *VerifyClientImpl) GetConfigVersion() (int, error) {
58+
func (c *VerifyClient) GetConfigVersion() (int, error) {
5959
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
6060
defer cancel()
6161

@@ -88,7 +88,7 @@ func (c *VerifyClientImpl) GetConfigVersion() (int, error) {
8888
// WaitForCorrectVersion first ensures any new worker processes have been started, and then calls the config version
8989
// endpoint until it gets the expectedVersion, which ensures that a new worker process has been started for that config
9090
// version.
91-
func (c *VerifyClientImpl) WaitForCorrectVersion(
91+
func (c *VerifyClient) WaitForCorrectVersion(
9292
ctx context.Context,
9393
expectedVersion int,
9494
childProcFile string,
@@ -119,7 +119,7 @@ func (c *VerifyClientImpl) WaitForCorrectVersion(
119119
return nil
120120
}
121121

122-
func (c *VerifyClientImpl) EnsureConfigVersion(ctx context.Context, expectedVersion int) error {
122+
func (c *VerifyClient) EnsureConfigVersion(ctx context.Context, expectedVersion int) error {
123123
return wait.PollUntilContextCancel(
124124
ctx,
125125
25*time.Millisecond,

0 commit comments

Comments
 (0)