-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (139 loc) · 3.76 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/google/uuid"
"github.com/kelseyhightower/envconfig"
"github.com/okzk/ticker"
"log"
"math/rand"
"net/http"
"strconv"
"time"
)
type Config struct {
Table string `required:"true"`
MinID int `split_words:"true" default:"0"`
MaxID int `split_words:"true" default:"1023"`
TTL int `default:"600"`
}
var conf Config
func main() {
if err := envconfig.Process("", &conf); err != nil {
log.Fatalf("[FATAL] %v", err)
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
if *sess.Config.Region == "" {
region, err := ec2metadata.New(sess).Region()
if err == nil {
sess.Config.Region = aws.String(region)
}
}
svc := dynamodb.New(sess)
u := uuid.New()
log.Printf("[INFO] uuid: %v", u)
bID := u[:]
nID, err := obtainID(svc, bID)
if err != nil {
log.Fatalf("[FATAL] %v", err)
}
log.Printf("[INFO] id: %d", nID)
tick := ticker.New(time.Duration(conf.TTL)*time.Second*2/5, func(_ time.Time) {
updateTTL(svc, nID, bID)
})
defer tick.Stop()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "test/plain")
fmt.Fprintf(w, "%d", nID)
})
http.ListenAndServe(":8000", nil)
}
func obtainID(svc *dynamodb.DynamoDB, bID []byte) (int, error) {
res, err := svc.Scan(&dynamodb.ScanInput{
AttributesToGet: []*string{aws.String("i")},
TableName: aws.String(conf.Table),
})
if err != nil {
return 0, err
}
existsIDs := make(map[int]bool)
for _, item := range res.Items {
nID, err := strconv.Atoi(aws.StringValue(item["i"].N))
if err != nil {
existsIDs[nID] = true
}
}
for _, n := range permutation(conf.MaxID - conf.MinID + 1) {
nID := conf.MinID + n
if existsIDs[nID] {
continue
}
ok, err := put(svc, nID, bID)
if err != nil {
return 0, err
}
if ok {
return nID, nil
}
}
return 0, errors.New("no available ID")
}
func put(svc *dynamodb.DynamoDB, nID int, bID []byte) (bool, error) {
t := int(time.Now().Add(time.Duration(conf.TTL) * time.Second).Unix())
_, err := svc.PutItem(&dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
"i": {N: aws.String(strconv.Itoa(nID))},
"b": {B: bID},
"t": {N: aws.String(strconv.Itoa(t))},
},
ConditionExpression: aws.String("attribute_not_exists(i)"),
TableName: aws.String(conf.Table),
})
if err != nil {
if e, ok := err.(awserr.Error); ok {
if e.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
return false, nil
}
}
return false, err
}
return true, nil
}
func updateTTL(svc *dynamodb.DynamoDB, nID int, bID []byte) {
log.Println("[INFO] updating TTL...")
t := int(time.Now().Add(time.Duration(conf.TTL) * time.Second).Unix())
_, err := svc.UpdateItem(&dynamodb.UpdateItemInput{
Key: map[string]*dynamodb.AttributeValue{
"i": {N: aws.String(strconv.Itoa(nID))},
},
UpdateExpression: aws.String("set t = :t"),
ConditionExpression: aws.String("b = :b"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":b": {B: bID},
":t": {N: aws.String(strconv.Itoa(t))},
},
TableName: aws.String(conf.Table),
})
if err != nil {
if e, ok := err.(awserr.Error); ok {
switch e.Code() {
case dynamodb.ErrCodeProvisionedThroughputExceededException, dynamodb.ErrCodeInternalServerError:
log.Printf("[WARN] fail to update TTL temporally: %v", err)
return
}
}
log.Fatalf("[FATAL] aborting: %v", err)
}
}
func permutation(n int) []int {
t := time.Now()
s := rand.NewSource(t.UnixNano())
return rand.New(s).Perm(n)
}