-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
106 lines (92 loc) · 2.64 KB
/
gateway.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Payload struct {
Payload string `json:"payload"`
Addresses []string `json:"addresses"`
}
// HandleFunc type defines the func that is used as a callback in ToFunc
type HandleFunc func(low int, high int)
// partition calls provided func per fragment
func partition(totalLength int, partitionLength int, hf HandleFunc) {
if partitionLength <= 0 || totalLength <= 0 {
return
}
partitions := totalLength / partitionLength
var i int
for i = 0; i < partitions; i++ {
hf(i*partitionLength, i*partitionLength+partitionLength)
}
if rest := totalLength % partitionLength; rest != 0 {
hf(i*partitionLength, i*partitionLength+rest)
}
}
func RunGateway(config Config) {
// Create a Pulsar client
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: config.Url,
})
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Start a separate goroutine for Prometheus metrics
// In this case, Prometheus metrics can be accessed via http://localhost:2112/metrics
go func() {
log.Printf("Starting Prometheus metrics at http://localhost:%v/metrics\n", prometheusPort)
http.Handle("/metrics", promhttp.Handler())
err = http.ListenAndServe(":"+strconv.Itoa(prometheusPort), nil)
if err != nil {
log.Fatal(err)
}
}()
// Create a producer
jsonSchemaWithProperties := pulsar.NewJSONSchema(exampleSchemaDef, nil)
producer, err := client.CreateProducer(pulsar.ProducerOptions{
Topic: config.ChannelName,
Schema: jsonSchemaWithProperties,
})
if err != nil {
log.Fatal(err)
}
defer producer.Close()
ctx := context.Background()
// Write your business logic here
// In this case, you build a simple Web server. You can produce messages by requesting http://localhost:8082/produce
http.HandleFunc("/send", func(w http.ResponseWriter, r *http.Request) {
var payload Payload
err := json.NewDecoder(r.Body).Decode(&payload)
// TODO: split for faster distribution
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
partition(len(payload.Addresses), batchSize, func(l int, h int) {
var currentPayload = &Payload{
Addresses: payload.Addresses[l:h],
Payload: payload.Payload,
}
msgId, err := producer.Send(ctx, &pulsar.ProducerMessage{
Value: ¤tPayload,
})
if err != nil {
log.Fatal(err)
} else {
log.Printf("Published message: %v", msgId)
fmt.Fprint(w, "OK")
}
})
})
err = http.ListenAndServe(":"+strconv.Itoa(httpPort), nil)
if err != nil {
log.Fatal(err)
}
}