/
eventHub.go
59 lines (45 loc) · 1.11 KB
/
eventHub.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
package endpoint
import (
"context"
"fmt"
"time"
"github.com/tidwall/gjson"
eventhub "github.com/Azure/azure-event-hubs-go/v3"
)
const ()
// HTTPConn is an endpoint connection
type EvenHubConn struct {
ep Endpoint
}
func newEventHubConn(ep Endpoint) *EvenHubConn {
return &EvenHubConn{
ep: ep,
}
}
// Expired returns true if the connection has expired
func (conn *EvenHubConn) Expired() bool {
return false
}
// ExpireNow forces the connection to expire
func (conn *EvenHubConn) ExpireNow() {
}
// Send sends a message
func (conn *EvenHubConn) Send(msg string) error {
hub, err := eventhub.NewHubFromConnectionString(conn.ep.EventHub.ConnectionString)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
// parse json again to get out info for our kafka key
key := gjson.Get(msg, "key")
id := gjson.Get(msg, "id")
keyValue := fmt.Sprintf("%s-%s", key.String(), id.String())
evtHubMsg := eventhub.NewEventFromString(msg)
evtHubMsg.PartitionKey = &keyValue
err = hub.Send(ctx, evtHubMsg)
if err != nil {
return err
}
return nil
}