Skip to content

Commit e4fee05

Browse files
authored
Use PublishWithContext instad of Publish (#337)
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 [email protected]
1 parent cd4f0f4 commit e4fee05

10 files changed

+85
-10
lines changed

go/emit_log.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"log"
56
"os"
67
"strings"
8+
"time"
79

810
amqp "github.com/rabbitmq/amqp091-go"
911
)
@@ -34,8 +36,11 @@ func main() {
3436
)
3537
failOnError(err, "Failed to declare an exchange")
3638

39+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
40+
defer cancel()
41+
3742
body := bodyFrom(os.Args)
38-
err = ch.Publish(
43+
err = ch.PublishWithContext(ctx,
3944
"logs", // exchange
4045
"", // routing key
4146
false, // mandatory

go/emit_log_direct.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"log"
56
"os"
67
"strings"
8+
"time"
79

810
amqp "github.com/rabbitmq/amqp091-go"
911
)
@@ -34,8 +36,11 @@ func main() {
3436
)
3537
failOnError(err, "Failed to declare an exchange")
3638

39+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
40+
defer cancel()
41+
3742
body := bodyFrom(os.Args)
38-
err = ch.Publish(
43+
err = ch.PublishWithContext(ctx,
3944
"logs_direct", // exchange
4045
severityFrom(os.Args), // routing key
4146
false, // mandatory

go/emit_log_topic.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"log"
56
"os"
67
"strings"
8+
"time"
79

810
amqp "github.com/rabbitmq/amqp091-go"
911
)
@@ -34,8 +36,11 @@ func main() {
3436
)
3537
failOnError(err, "Failed to declare an exchange")
3638

39+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
40+
defer cancel()
41+
3742
body := bodyFrom(os.Args)
38-
err = ch.Publish(
43+
err = ch.PublishWithContext(ctx,
3944
"logs_topic", // exchange
4045
severityFrom(os.Args), // routing key
4146
false, // mandatory

go/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/rabbitmq/rabbitmq-tutorials
22

3-
go 1.16
3+
go 1.17
44

5-
require github.com/rabbitmq/amqp091-go v0.0.0-20210609115249-03e0554a59cf
5+
require github.com/rabbitmq/amqp091-go v1.4.0

go/go.sum

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
3+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16
github.com/rabbitmq/amqp091-go v0.0.0-20210609115249-03e0554a59cf h1:EBDKctNpOEfxlZAm2At5rUjmztnfswQr4ljWQXvQ3pM=
27
github.com/rabbitmq/amqp091-go v0.0.0-20210609115249-03e0554a59cf/go.mod h1:ogQDLSOACsLPsIq0NpbtiifNZi2YOz0VTJ0kHRghqbM=
8+
github.com/rabbitmq/amqp091-go v1.4.0 h1:T2G+J9W9OY4p64Di23J6yH7tOkMocgnESvYeBjuG9cY=
9+
github.com/rabbitmq/amqp091-go v1.4.0/go.mod h1:JsV0ofX5f1nwOGafb8L5rBItt9GyhfQfcJj+oyz0dGg=
10+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
12+
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
13+
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
14+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
15+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
16+
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
17+
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
18+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
19+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
20+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
21+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
22+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
23+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
24+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
25+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
26+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
27+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
28+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
30+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
31+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
32+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
33+
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
34+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
35+
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
36+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
37+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
38+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
39+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
40+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
41+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

go/new_task.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"log"
56
"os"
67
"strings"
8+
"time"
79

810
amqp "github.com/rabbitmq/amqp091-go"
911
)
@@ -33,8 +35,11 @@ func main() {
3335
)
3436
failOnError(err, "Failed to declare a queue")
3537

38+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
39+
defer cancel()
40+
3641
body := bodyFrom(os.Args)
37-
err = ch.Publish(
42+
err = ch.PublishWithContext(ctx,
3843
"", // exchange
3944
q.Name, // routing key
4045
false, // mandatory

go/publisher_confirms.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"context"
45
"log"
6+
"time"
57

68
amqp "github.com/rabbitmq/amqp091-go"
79
)
@@ -76,7 +78,9 @@ func consume(ch *amqp.Channel, qName string) {
7678
}
7779

7880
func publish(ch *amqp.Channel, qName, text string) {
79-
err := ch.Publish("", qName, false, false, amqp.Publishing{
81+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
82+
defer cancel()
83+
err := ch.PublishWithContext(ctx, "", qName, false, false, amqp.Publishing{
8084
ContentType: "text/plain",
8185
Body: []byte(text),
8286
})

go/rpc_client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"log"
56
"math/rand"
67
"os"
@@ -61,7 +62,10 @@ func fibonacciRPC(n int) (res int, err error) {
6162

6263
corrId := randomString(32)
6364

64-
err = ch.Publish(
65+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
66+
defer cancel()
67+
68+
err = ch.PublishWithContext(ctx,
6569
"", // exchange
6670
"rpc_queue", // routing key
6771
false, // mandatory

go/rpc_server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"context"
45
"log"
56
"strconv"
7+
"time"
68

79
amqp "github.com/rabbitmq/amqp091-go"
810
)
@@ -63,14 +65,16 @@ func main() {
6365
var forever chan struct{}
6466

6567
go func() {
68+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
69+
defer cancel()
6670
for d := range msgs {
6771
n, err := strconv.Atoi(string(d.Body))
6872
failOnError(err, "Failed to convert body to integer")
6973

7074
log.Printf(" [.] fib(%d)", n)
7175
response := fib(n)
7276

73-
err = ch.Publish(
77+
err = ch.PublishWithContext(ctx,
7478
"", // exchange
7579
d.ReplyTo, // routing key
7680
false, // mandatory

go/send.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"context"
45
"log"
6+
"time"
57

68
amqp "github.com/rabbitmq/amqp091-go"
79
)
@@ -30,9 +32,11 @@ func main() {
3032
nil, // arguments
3133
)
3234
failOnError(err, "Failed to declare a queue")
35+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
36+
defer cancel()
3337

3438
body := "Hello World!"
35-
err = ch.Publish(
39+
err = ch.PublishWithContext(ctx,
3640
"", // exchange
3741
q.Name, // routing key
3842
false, // mandatory

0 commit comments

Comments
 (0)