forked from tidwall/tile38
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Tuohy
committed
Oct 15, 2021
1 parent
72b3683
commit 4454995
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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 | ||
} | ||
|
||
// 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 | ||
} |