2017-10-06 00:08:03 +03:00
|
|
|
package endpoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-03-14 01:41:49 +03:00
|
|
|
"strings"
|
2017-10-06 00:08:03 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/sqs"
|
2019-03-18 04:40:34 +03:00
|
|
|
"github.com/tidwall/tile38/internal/log"
|
2017-10-06 00:08:03 +03:00
|
|
|
)
|
|
|
|
|
2019-10-30 20:17:59 +03:00
|
|
|
const sqsExpiresAfter = time.Second * 30
|
2017-10-06 00:08:03 +03:00
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// SQSConn is an endpoint connection
|
|
|
|
type SQSConn struct {
|
2017-10-06 00:08:03 +03:00
|
|
|
mu sync.Mutex
|
|
|
|
ep Endpoint
|
|
|
|
session *session.Session
|
|
|
|
svc *sqs.SQS
|
|
|
|
ex bool
|
|
|
|
t time.Time
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
func (conn *SQSConn) generateSQSURL() string {
|
2019-03-14 01:41:49 +03:00
|
|
|
if conn.ep.SQS.PlainURL != "" {
|
|
|
|
return conn.ep.SQS.PlainURL
|
|
|
|
}
|
|
|
|
return "https://sqs." + conn.ep.SQS.Region + ".amazonaws.com/" +
|
|
|
|
conn.ep.SQS.QueueID + "/" + conn.ep.SQS.QueueName
|
2017-10-06 00:08:03 +03:00
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Expired returns true if the connection has expired
|
|
|
|
func (conn *SQSConn) Expired() bool {
|
2017-10-06 00:08:03 +03:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
if !conn.ex {
|
2021-03-31 18:13:44 +03:00
|
|
|
if time.Since(conn.t) > sqsExpiresAfter {
|
2017-10-06 00:08:03 +03:00
|
|
|
conn.ex = true
|
|
|
|
conn.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conn.ex
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
func (conn *SQSConn) close() {
|
2017-10-06 00:08:03 +03:00
|
|
|
if conn.svc != nil {
|
|
|
|
conn.svc = nil
|
|
|
|
conn.session = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Send sends a message
|
|
|
|
func (conn *SQSConn) Send(msg string) error {
|
2017-10-06 00:08:03 +03:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
|
|
|
|
if conn.ex {
|
|
|
|
return errExpired
|
|
|
|
}
|
|
|
|
conn.t = time.Now()
|
|
|
|
|
|
|
|
if conn.svc == nil && conn.session == nil {
|
2019-03-11 20:49:25 +03:00
|
|
|
var creds *credentials.Credentials
|
2017-10-06 00:08:03 +03:00
|
|
|
credPath := conn.ep.SQS.CredPath
|
2019-03-11 20:49:25 +03:00
|
|
|
if credPath != "" {
|
|
|
|
credProfile := conn.ep.SQS.CredProfile
|
|
|
|
if credProfile == "" {
|
|
|
|
credProfile = "default"
|
|
|
|
}
|
|
|
|
creds = credentials.NewSharedCredentials(credPath, credProfile)
|
2017-10-06 00:08:03 +03:00
|
|
|
}
|
2019-03-14 01:41:49 +03:00
|
|
|
var region string
|
|
|
|
if conn.ep.SQS.Region != "" {
|
|
|
|
region = conn.ep.SQS.Region
|
|
|
|
} else {
|
|
|
|
region = sqsRegionFromPlainURL(conn.ep.SQS.PlainURL)
|
|
|
|
}
|
2019-03-11 20:49:25 +03:00
|
|
|
sess := session.Must(session.NewSession(&aws.Config{
|
2019-03-18 04:40:34 +03:00
|
|
|
Region: ®ion,
|
|
|
|
Credentials: creds,
|
|
|
|
CredentialsChainVerboseErrors: aws.Bool(log.Level >= 3),
|
|
|
|
MaxRetries: aws.Int(5),
|
2019-03-11 20:49:25 +03:00
|
|
|
}))
|
2017-10-06 00:08:03 +03:00
|
|
|
svc := sqs.New(sess)
|
2019-03-11 20:49:25 +03:00
|
|
|
if conn.ep.SQS.CreateQueue {
|
|
|
|
svc.CreateQueue(&sqs.CreateQueueInput{
|
|
|
|
QueueName: aws.String(conn.ep.SQS.QueueName),
|
|
|
|
Attributes: map[string]*string{
|
|
|
|
"DelaySeconds": aws.String("60"),
|
|
|
|
"MessageRetentionPeriod": aws.String("86400"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-10-06 00:08:03 +03:00
|
|
|
conn.session = sess
|
|
|
|
conn.svc = svc
|
|
|
|
}
|
|
|
|
|
|
|
|
queueURL := conn.generateSQSURL()
|
|
|
|
// Send message
|
|
|
|
sendParams := &sqs.SendMessageInput{
|
|
|
|
MessageBody: aws.String(msg),
|
|
|
|
QueueUrl: aws.String(queueURL),
|
|
|
|
}
|
|
|
|
_, err := conn.svc.SendMessage(sendParams)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
func newSQSConn(ep Endpoint) *SQSConn {
|
|
|
|
return &SQSConn{
|
2017-10-06 00:08:03 +03:00
|
|
|
ep: ep,
|
|
|
|
t: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 01:41:49 +03:00
|
|
|
|
|
|
|
func probeSQS(s string) bool {
|
|
|
|
// https://sqs.eu-central-1.amazonaws.com/123456789/myqueue
|
|
|
|
return strings.HasPrefix(s, "https://sqs.") &&
|
|
|
|
strings.Contains(s, ".amazonaws.com/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqsRegionFromPlainURL(s string) string {
|
|
|
|
parts := strings.Split(s, "https://sqs.")
|
|
|
|
if len(parts) > 1 {
|
|
|
|
parts = strings.Split(parts[1], ".amazonaws.com/")
|
|
|
|
if len(parts) > 1 {
|
|
|
|
return parts[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|