Debian Codegraph @

go/examples/electron/send.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 package main
21
22 import (
23 "flag"
24 "fmt"
25 "log"
26 "os"
27 "github.com/apache/qpid-proton/go/pkg/amqp"
28 "github.com/apache/qpid-proton/go/pkg/electron"
29 "strings"
30 "sync"
31 )
32
33 // Usage and command-line flags
34 func usage() {
35 fmt.Fprintf(os.Stderr, `Usage: %s url [url ...]
36 Send messages to each URL concurrently.
37 URLs are of the form "amqp://<host>:<port>/<amqp-address>"
38 `, os.Args[0])
39 flag.PrintDefaults()
40 }
41
42 var count = flag.Int64("count", 1, "Send this many messages to each address.")
43 var debug = flag.Bool("debug", false, "Print detailed debug output")
44 var Debugf = func(format string, data ...interface{}) {} // Default no debugging output
45
46 func main() {
47 flag.Usage = usage
48 flag.Parse()
49
50 if *debug {
51 Debugf = func(format string, data ...interface{}) { log.Printf(format, data...) }
52 }
53
54 urls := flag.Args() // Non-flag arguments are URLs to receive from
55 if len(urls) == 0 {
56 log.Println("No URL provided")
57 flag.Usage()
58 os.Exit(1)
59 }
60
61 sentChan := make(chan electron.Outcome) // Channel to receive acknowledgements.
62
63 var wait sync.WaitGroup
64 wait.Add(len(urls)) // Wait for one goroutine per URL.
65
66 container := electron.NewContainer(fmt.Sprintf("send[%v]", os.Getpid()))
67 connections := make(chan electron.Connection, len(urls)) // Connections to close on exit
68
69 // Start a goroutine for each URL to send messages.
70 for _, urlStr := range urls {
71 Debugf("Connecting to %v\n", urlStr)
72 go func(urlStr string) {
73 defer wait.Done() // Notify main() when this goroutine is done.
74 url, err := amqp.ParseURL(urlStr)
75 fatalIf(err)
76 c, err := container.Dial("tcp", url.Host) // NOTE: Dial takes just the Host part of the URL
77 fatalIf(err)
78 connections <- c // Save connection so we can Close() when main() ends
79 addr := strings.TrimPrefix(url.Path, "/")
80 s, err := c.Sender(electron.Target(addr))
81 fatalIf(err)
82 // Loop sending messages.
83 for i := int64(0); i < *count; i++ {
84 m := amqp.NewMessage()
85 body := fmt.Sprintf("%v%v", addr, i)
86 m.Marshal(body)
87 s.SendAsync(m, sentChan, body) // Outcome will be sent to sentChan
88 }
89 }(urlStr)
90 }
91
92 // Wait for all the acknowledgements
93 expect := int(*count) * len(urls)
94 Debugf("Started senders, expect %v acknowledgements\n", expect)
95 for i := 0; i < expect; i++ {
96 out := <-sentChan // Outcome of async sends.
97 if out.Error != nil {
98 log.Fatalf("acknowledgement[%v] %v error: %v", i, out.Value, out.Error)
99 } else if out.Status != electron.Accepted {
100 log.Fatalf("acknowledgement[%v] unexpected status: %v", i, out.Status)
101 } else {
102 Debugf("acknowledgement[%v] %v (%v)\n", i, out.Value, out.Status)
103 }
104 }
105 fmt.Printf("Received all %v acknowledgements\n", expect)
106
107 wait.Wait() // Wait for all goroutines to finish.
108 close(connections)
109 for c := range connections { // Close all connections
110 if c != nil {
111 c.Close(nil)
112 }
113 }
114 }
115
116 func fatalIf(err error) {
117 if err != nil {
118 log.Fatal(err)
119 }
120 }