package services import ( "context" "encoding/json" "io" "log" "math/rand" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/aws/aws-sdk-go-v2/service/sqs/types" "github.com/fgm/izidic" ) func ProducerService(dic *izidic.Container) (any, error) { cli := dic.MustService("sqs").(*sqs.Client) w := dic.MustParam(PWriter).(io.Writer) return func(ctx context.Context, qName string) { senderHandler(ctx, w, cli, qName) }, nil } func senderHandler(ctx context.Context, _ io.Writer, client *sqs.Client, qURL string) { pinger := time.NewTicker(2 * time.Second) for range pinger.C { sendOne(ctx, client, qURL) } } func sendOne(ctx context.Context, client *sqs.Client, qURL string) { data := map[string]int{"a": rand.Int()} data["b"] = data["a"] + 1 bs, _ := json.Marshal(data) body := string(bs) ma := map[string]types.MessageAttributeValue{ "x": { DataType: aws.String("String"), StringValue: aws.String("a string value"), }, "y": { DataType: aws.String("Number"), StringValue: aws.String("42"), }, "retry": { DataType: aws.String("String"), StringValue: aws.String("1"), }, } smr := sqs.SendMessageInput{ MessageBody: &body, QueueUrl: &qURL, DelaySeconds: 0, MessageAttributes: ma, MessageDeduplicationId: nil, MessageGroupId: nil, MessageSystemAttributes: nil, } smo, err := client.SendMessage(ctx, &smr) if err != nil { log.Printf("failed producing message: %v", err) } else if smo.MessageId == nil { log.Println("message produced with a nil ID") } else { log.Printf("message %s produced with %#v", *smo.MessageId, data) } }