
Update the GO tutorials with PublishWithContext instead of Publish Publish is deprecated. As context, I used: `context.WithTimeout(context.Background(), 5*time.Second)` update the Golang Version to 1.7 Signed-off-by: Gabriele Santomaggio G.santomaggio@gmail.com
115 lines
2.2 KiB
Go
115 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"math/rand"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
func failOnError(err error, msg string) {
|
|
if err != nil {
|
|
log.Panicf("%s: %s", msg, err)
|
|
}
|
|
}
|
|
|
|
func randomString(l int) string {
|
|
bytes := make([]byte, l)
|
|
for i := 0; i < l; i++ {
|
|
bytes[i] = byte(randInt(65, 90))
|
|
}
|
|
return string(bytes)
|
|
}
|
|
|
|
func randInt(min int, max int) int {
|
|
return min + rand.Intn(max-min)
|
|
}
|
|
|
|
func fibonacciRPC(n int) (res int, err error) {
|
|
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
|
|
failOnError(err, "Failed to connect to RabbitMQ")
|
|
defer conn.Close()
|
|
|
|
ch, err := conn.Channel()
|
|
failOnError(err, "Failed to open a channel")
|
|
defer ch.Close()
|
|
|
|
q, err := ch.QueueDeclare(
|
|
"", // name
|
|
false, // durable
|
|
false, // delete when unused
|
|
true, // exclusive
|
|
false, // noWait
|
|
nil, // arguments
|
|
)
|
|
failOnError(err, "Failed to declare a queue")
|
|
|
|
msgs, err := ch.Consume(
|
|
q.Name, // queue
|
|
"", // consumer
|
|
true, // auto-ack
|
|
false, // exclusive
|
|
false, // no-local
|
|
false, // no-wait
|
|
nil, // args
|
|
)
|
|
failOnError(err, "Failed to register a consumer")
|
|
|
|
corrId := randomString(32)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
err = ch.PublishWithContext(ctx,
|
|
"", // exchange
|
|
"rpc_queue", // routing key
|
|
false, // mandatory
|
|
false, // immediate
|
|
amqp.Publishing{
|
|
ContentType: "text/plain",
|
|
CorrelationId: corrId,
|
|
ReplyTo: q.Name,
|
|
Body: []byte(strconv.Itoa(n)),
|
|
})
|
|
failOnError(err, "Failed to publish a message")
|
|
|
|
for d := range msgs {
|
|
if corrId == d.CorrelationId {
|
|
res, err = strconv.Atoi(string(d.Body))
|
|
failOnError(err, "Failed to convert body to integer")
|
|
break
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func main() {
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
n := bodyFrom(os.Args)
|
|
|
|
log.Printf(" [x] Requesting fib(%d)", n)
|
|
res, err := fibonacciRPC(n)
|
|
failOnError(err, "Failed to handle RPC request")
|
|
|
|
log.Printf(" [.] Got %d", res)
|
|
}
|
|
|
|
func bodyFrom(args []string) int {
|
|
var s string
|
|
if (len(args) < 2) || os.Args[1] == "" {
|
|
s = "30"
|
|
} else {
|
|
s = strings.Join(args[1:], " ")
|
|
}
|
|
n, err := strconv.Atoi(s)
|
|
failOnError(err, "Failed to convert arg to integer")
|
|
return n
|
|
}
|