forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eniattachment.go
106 lines (91 loc) · 3.42 KB
/
eniattachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package api
import (
"fmt"
"sync"
"time"
"github.com/aws/amazon-ecs-agent/agent/utils/ttime"
"github.com/cihub/seelog"
"github.com/pkg/errors"
)
// ENIAttachment contains the information of the eni attachment
type ENIAttachment struct {
// TaskARN is the task identifier from ecs
TaskARN string `json:"taskArn"`
// AttachmentARN is the identifier for the eni attachment
AttachmentARN string `json:"attachmentArn"`
// AttachStatusSent indicates whether the attached status has been sent to backend
AttachStatusSent bool `json:"attachSent"`
// MACAddress is the mac address of eni
MACAddress string `json:"macAddress"`
// Status is the status of the eni: none/attached/detached
Status ENIAttachmentStatus `json:"status"`
// 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.
ExpiresAt time.Time `json:"expiresAt"`
// 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
}
now := time.Now()
duration := eni.ExpiresAt.Sub(now)
if duration <= 0 {
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.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.guard.RLock()
defer eni.guard.RUnlock()
return eni.AttachStatusSent
}
// SetSentStatus marks the eni attached status has been sent
func (eni *ENIAttachment) SetSentStatus() {
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.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.TaskARN, eni.AttachmentARN, eni.AttachStatusSent, eni.MACAddress, eni.Status.String(), eni.ExpiresAt.String())
}