agent/api: switch to using a common lock for accessing all of api.ENIAttachment struct
@@ -35,7 +35,6 @@ type ENIAttachment struct {
MACAddress string `json:"macAddress"`
// Status is the status of the eni: none/attached/detached
Status ENIAttachmentStatus `json:"status"`
- sentStatusLock sync.RWMutex
// ExpiresAt is the timestamp past which the ENI Attachment is considered
// unsuccessful. The SubmitTaskStateChange API, with the attachment information
// should be invoked before this timestamp.
@@ -43,10 +42,15 @@ type ENIAttachment struct {
// ackTimer is used to register the expirtation timeout callback for unsuccessful
// ENI attachments
ackTimer ttime.Timer
+ // guard protects access to fields of this struct
+ guard sync.RWMutex
}
// StartTimer starts the ack timer to record the expiration of ENI attachment
func (eni *ENIAttachment) StartTimer(timeoutFunc func()) error {
+ eni.guard.Lock()
+ defer eni.guard.Unlock()
+
if eni.ackTimer != nil {
// The timer has already been initialized, do nothing
return nil
@@ -57,38 +61,46 @@ func (eni *ENIAttachment) StartTimer(timeoutFunc func()) error {
return errors.Errorf("eni attachment: timer expiration is in the past; expiration [%s] < now [%s]",
eni.ExpiresAt.String(), now.String())
}
- seelog.Infof("Starting ENI ack timer with duration=%s, %s", duration.String(), eni.String())
+ seelog.Infof("Starting ENI ack timer with duration=%s, %s", duration.String(), eni.stringUnsafe())
eni.ackTimer = time.AfterFunc(duration, timeoutFunc)
return nil
}
// IsSent checks if the eni attached status has been sent
func (eni *ENIAttachment) IsSent() bool {
- eni.sentStatusLock.RLock()
- defer eni.sentStatusLock.RUnlock()
+ eni.guard.RLock()
+ defer eni.guard.RUnlock()
return eni.AttachStatusSent
}
// SetSentStatus marks the eni attached status has been sent
func (eni *ENIAttachment) SetSentStatus() {
- eni.sentStatusLock.Lock()
- defer eni.sentStatusLock.Unlock()
+ eni.guard.Lock()
+ defer eni.guard.Unlock()
eni.AttachStatusSent = true
}
// StopAckTimer stops the ack timer set on the ENI attachment
func (eni *ENIAttachment) StopAckTimer() {
+ eni.guard.Lock()
+ defer eni.guard.Unlock()
+
eni.ackTimer.Stop()
}
// String returns a string representation of the ENI Attachment
func (eni *ENIAttachment) String() string {
- eni.sentStatusLock.RLock()
- defer eni.sentStatusLock.RUnlock()
+ eni.guard.RLock()
+ defer eni.guard.RUnlock()
+
+ return eni.stringUnsafe()
+}
+// stringUnsafe returns a string representation of the ENI Attachment
+func (eni *ENIAttachment) stringUnsafe() string {
return fmt.Sprintf(
- "ENI Attachment; task=%s;attachment=%s;attachmentSent=%t;mac=%s;status=%s;expiresAt=%s",
+ "ENI Attachment: task=%s;attachment=%s;attachmentSent=%t;mac=%s;status=%s;expiresAt=%s",
eni.TaskARN, eni.AttachmentARN, eni.AttachStatusSent, eni.MACAddress, eni.Status.String(), eni.ExpiresAt.String())
}
