forked from knative/eventing
/
main.go
155 lines (133 loc) · 3.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/cloudevents/sdk-go/pkg/cloudevents"
"github.com/cloudevents/sdk-go/pkg/cloudevents/client"
"github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http"
"github.com/cloudevents/sdk-go/pkg/cloudevents/types"
)
type Heartbeat struct {
Sequence int `json:"id"`
Data string `json:"data"`
}
var (
sink string
data string
eventID string
eventType string
source string
periodStr string
delayStr string
maxMsgStr string
encoding string
)
func init() {
flag.StringVar(&sink, "sink", "", "The sink url for the message destination.")
flag.StringVar(&data, "data", `{"hello": "world!"}`, "Cloudevent data body.")
flag.StringVar(&eventID, "event-id", "", "Event ID to use. Defaults to a generated UUID")
flag.StringVar(&eventType, "event-type", "knative.eventing.test.e2e", "The Event Type to use.")
flag.StringVar(&source, "source", "", "Source URI to use. Defaults to the current machine's hostname")
flag.StringVar(&periodStr, "period", "5", "The number of seconds between messages.")
flag.StringVar(&delayStr, "delay", "5", "The number of seconds to wait before sending messages.")
flag.StringVar(&maxMsgStr, "max-messages", "1", "The number of messages to attempt to send. 0 for unlimited.")
flag.StringVar(&encoding, "encoding", "binary", "The encoding of the cloud event, one of(binary, structured).")
}
func parseDurationStr(durationStr string, defaultDuration int) time.Duration {
var duration time.Duration
if d, err := strconv.Atoi(durationStr); err != nil {
duration = time.Duration(defaultDuration) * time.Second
} else {
duration = time.Duration(d) * time.Second
}
return duration
}
func main() {
flag.Parse()
period := parseDurationStr(periodStr, 5)
delay := parseDurationStr(delayStr, 5)
maxMsg := 1
if m, err := strconv.Atoi(maxMsgStr); err == nil {
maxMsg = m
}
if delay > 0 {
log.Printf("will sleep for %s", delay)
time.Sleep(delay)
log.Printf("awake, contining")
}
if source == "" {
source = "localhost"
}
var encodingOption http.Option
switch encoding {
case "binary":
encodingOption = http.WithBinaryEncoding()
case "structured":
encodingOption = http.WithStructuredEncoding()
default:
fmt.Printf("unsupported encoding option: %q\n", encoding)
os.Exit(1)
}
t, err := http.New(
http.WithTarget(sink),
encodingOption,
)
if err != nil {
log.Fatalf("failed to create transport, %v", err)
}
c, err := client.New(t,
client.WithTimeNow(),
client.WithUUIDs(),
)
if err != nil {
log.Fatalf("failed to create client, %v", err)
}
var untyped map[string]interface{}
if err := json.Unmarshal([]byte(data), &untyped); err != nil {
fmt.Println("Currently sendevent only supports JSON event data")
os.Exit(1)
}
sequence := 0
ticker := time.NewTicker(period)
for {
sequence++
untyped["sequence"] = fmt.Sprintf("%d", sequence)
event := cloudevents.Event{
Context: cloudevents.EventContextV02{
ID: eventID,
Type: eventType,
Source: *types.ParseURLRef(source),
}.AsV02(),
Data: untyped,
}
if resp, err := c.Send(context.Background(), event); err != nil {
fmt.Printf("send returned an error: %v\n", err)
} else if resp != nil {
fmt.Printf("Got response from %s\n%s\n", sink, resp)
}
// Wait for next tick
<-ticker.C
// Only send a limited number of messages.
if maxMsg != 0 && maxMsg == sequence {
return
}
}
}