2017-10-06 00:08:03 +03:00
|
|
|
package endpoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
"github.com/streadway/amqp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errCreateQueue = errors.New("Error while creating queue")
|
|
|
|
|
|
|
|
const (
|
2018-04-19 19:25:39 +03:00
|
|
|
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
|
|
|
|
channel *amqp.Channel
|
|
|
|
ex bool
|
|
|
|
t time.Time
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
func (conn *SQSConn) generateSQSURL() string {
|
2017-10-06 00:08:03 +03:00
|
|
|
return "https://sqs." + conn.ep.SQS.Region + "amazonaws.com/" + conn.ep.SQS.QueueID + "/" + conn.ep.SQS.QueueName
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-04-19 19:25:39 +03:00
|
|
|
if time.Now().Sub(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 {
|
|
|
|
credPath := conn.ep.SQS.CredPath
|
|
|
|
credProfile := conn.ep.SQS.CredProfile
|
|
|
|
var sess *session.Session
|
|
|
|
if credPath != "" && credProfile != "" {
|
|
|
|
sess = session.Must(session.NewSession(&aws.Config{
|
|
|
|
Region: aws.String(conn.ep.SQS.Region),
|
|
|
|
Credentials: credentials.NewSharedCredentials(credPath, credProfile),
|
|
|
|
MaxRetries: aws.Int(5),
|
|
|
|
}))
|
|
|
|
} else if credPath != "" {
|
|
|
|
sess = session.Must(session.NewSession(&aws.Config{
|
|
|
|
Region: aws.String(conn.ep.SQS.Region),
|
|
|
|
Credentials: credentials.NewSharedCredentials(credPath, "default"),
|
|
|
|
MaxRetries: aws.Int(5),
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
sess = session.Must(session.NewSession(&aws.Config{
|
|
|
|
Region: aws.String(conn.ep.SQS.Region),
|
|
|
|
MaxRetries: aws.Int(5),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
// Create a SQS service client.
|
|
|
|
svc := sqs.New(sess)
|
|
|
|
|
|
|
|
svc.CreateQueue(&sqs.CreateQueueInput{
|
|
|
|
QueueName: aws.String(conn.ep.SQS.QueueName),
|
|
|
|
Attributes: map[string]*string{
|
|
|
|
"DelaySeconds": aws.String("60"),
|
|
|
|
"MessageRetentionPeriod": aws.String("86400"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|