-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azure eventhubs use the new api (#412)
- Loading branch information
1 parent
07e36c5
commit e7748b9
Showing
6 changed files
with
247 additions
and
613 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package conneventhub | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs" | ||
cmap "github.com/orcaman/concurrent-map/v2" | ||
) | ||
|
||
type EventHubManager struct { | ||
ctx context.Context | ||
creds *azidentity.DefaultAzureCredential | ||
namespace string | ||
hubs cmap.ConcurrentMap[string, *azeventhubs.ProducerClient] | ||
} | ||
|
||
func NewEventHubManager( | ||
ctx context.Context, | ||
creds *azidentity.DefaultAzureCredential, | ||
namespace string, | ||
) *EventHubManager { | ||
hubs := cmap.New[*azeventhubs.ProducerClient]() | ||
return &EventHubManager{ | ||
ctx: ctx, | ||
creds: creds, | ||
namespace: namespace, | ||
hubs: hubs, | ||
} | ||
} | ||
|
||
func (m *EventHubManager) GetOrCreateHub(name string) (*azeventhubs.ProducerClient, error) { | ||
hub, ok := m.hubs.Get(name) | ||
|
||
if !ok { | ||
opts := &azeventhubs.ProducerClientOptions{} | ||
hub, err := azeventhubs.NewProducerClient(m.namespace, name, m.creds, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create eventhub client: %v", err) | ||
} | ||
m.hubs.Set(name, hub) | ||
return hub, nil | ||
} | ||
|
||
return hub, nil | ||
} | ||
|
||
func (m *EventHubManager) Close() error { | ||
for hub := range m.hubs.IterBuffered() { | ||
err := hub.Val.Close(m.ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to close eventhub client: %v", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (m *EventHubManager) CreateEventDataBatch(name string) (*azeventhubs.EventDataBatch, error) { | ||
hub, err := m.GetOrCreateHub(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := &azeventhubs.EventDataBatchOptions{} | ||
batch, err := hub.NewEventDataBatch(m.ctx, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create event data batch: %v", err) | ||
} | ||
|
||
return batch, nil | ||
} |
Oops, something went wrong.