Skip to content

Commit 8019ab3

Browse files
committed
Add comments; send response when context canceled
1 parent 096d6a9 commit 8019ab3

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

internal/mode/static/nginx/agent/command.go

+11
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,19 @@ func (cs *commandService) Subscribe(in pb.CommandService_SubscribeServer) error
154154
defer broadcaster.CancelSubscription(channels.ID)
155155

156156
for {
157+
// When a message is received over the ListenCh, it is assumed and required that the
158+
// deployment object is already LOCKED. This lock is acquired by the event handler before calling
159+
// `updateNginxConfig`. The entire transaction (as described in above in the function comment)
160+
// must be locked to prevent the deployment files from changing during the transaction.
161+
// This means that the lock is held until we receive either an error or response from agent
162+
// (via msgr.Errors() or msgr.Mesages()) and respond back, finally returning to the event handler
163+
// which releases the lock.
157164
select {
158165
case <-ctx.Done():
166+
select {
167+
case channels.ResponseCh <- struct{}{}:
168+
default:
169+
}
159170
return grpcStatus.Error(codes.Canceled, context.Cause(ctx).Error())
160171
case msg := <-channels.ListenCh:
161172
var req *pb.ManagementPlaneRequest

internal/mode/static/nginx/agent/deployment.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ are locked by the caller, hence why the functions themselves do not set the lock
112112
*/
113113

114114
// GetFile gets the requested file for the deployment and returns its contents.
115-
// Caller MUST lock the deployment before calling this function.
115+
// The deployment MUST already be locked before calling this function.
116116
func (d *Deployment) GetFile(name, hash string) []byte {
117117
for _, file := range d.files {
118118
if name == file.Meta.GetName() && hash == file.Meta.GetHash() {
@@ -124,7 +124,7 @@ func (d *Deployment) GetFile(name, hash string) []byte {
124124
}
125125

126126
// SetFiles updates the nginx files and fileOverviews for the deployment and returns the message to send.
127-
// Caller MUST lock the deployment before calling this function.
127+
// The deployment MUST already be locked before calling this function.
128128
func (d *Deployment) SetFiles(files []File) broadcast.NginxAgentMessage {
129129
d.files = files
130130

@@ -158,32 +158,32 @@ func (d *Deployment) SetFiles(files []File) broadcast.NginxAgentMessage {
158158

159159
// SetNGINXPlusActions updates the deployment's latest NGINX Plus Actions to perform if using NGINX Plus.
160160
// Used by a Subscriber when it first connects.
161-
// Caller MUST lock the deployment before calling this function.
161+
// The deployment MUST already be locked before calling this function.
162162
func (d *Deployment) SetNGINXPlusActions(actions []*pb.NGINXPlusAction) {
163163
d.nginxPlusActions = actions
164164
}
165165

166166
// SetPodErrorStatus sets the error status of a Pod in this Deployment if applying the config failed.
167-
// Caller MUST lock the deployment before calling this function.
167+
// The deployment MUST already be locked before calling this function.
168168
func (d *Deployment) SetPodErrorStatus(pod string, err error) {
169169
d.podStatuses[pod] = err
170170
}
171171

172172
// SetLatestConfigError sets the latest config apply error for the deployment.
173-
// Caller MUST lock the deployment before calling this function.
173+
// The deployment MUST already be locked before calling this function.
174174
func (d *Deployment) SetLatestConfigError(err error) {
175175
d.latestConfigError = err
176176
}
177177

178178
// SetLatestUpstreamError sets the latest upstream update error for the deployment.
179-
// Caller MUST lock the deployment before calling this function.
179+
// The deployment MUST already be locked before calling this function.
180180
func (d *Deployment) SetLatestUpstreamError(err error) {
181181
d.latestUpstreamError = err
182182
}
183183

184184
// GetConfigurationStatus returns the current config status for this Deployment. It combines
185185
// the most recent errors (if they exist) for all Pods in the Deployment into a single error.
186-
// Caller MUST lock the deployment before calling this function.
186+
// The deployment MUST already be locked before calling this function.
187187
func (d *Deployment) GetConfigurationStatus() error {
188188
errs := make([]error, 0, len(d.podStatuses))
189189
for _, err := range d.podStatuses {

internal/mode/static/nginx/agent/file.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func (fs *fileService) Register(server *grpc.Server) {
4646
}
4747

4848
// GetFile is called by the agent when it needs to download a file for a ConfigApplyRequest.
49+
// The deployment object used to get the files is already LOCKED when this function is called,
50+
// before the ConfigApply transaction is started.
4951
func (fs *fileService) GetFile(
5052
ctx context.Context,
5153
req *pb.GetFileRequest,

0 commit comments

Comments
 (0)