Skip to content

Commit

Permalink
add event closer
Browse files Browse the repository at this point in the history
  • Loading branch information
AnshulMalik committed Mar 7, 2024
1 parent d799664 commit 7ce50d2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"runtime"
Expand All @@ -15,6 +16,7 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/multierr"
"golang.org/x/sync/semaphore"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -57,6 +59,7 @@ type Metrics interface {

// Event interface
type Event interface {
io.Closer
// Publish send data to all subscribers
Publish(context.Context, Data)
// Subscribe receive data sent by Publish
Expand All @@ -74,6 +77,9 @@ type discardEvent struct{}
func (discardEvent) Name() string { return "" }
func (discardEvent) Subscribe(_ context.Context, _ Handler) {}
func (discardEvent) Publish(_ context.Context, _ Data) {}
func (discardEvent) Close() error {
return nil
}

// eventImpl event implementation
type eventImpl struct {
Expand Down Expand Up @@ -363,6 +369,17 @@ func (e Events) Publish(ctx context.Context, data Data) {
}
}

func (e Events) Close() error {
var combinedErr error
for _, event := range e {
err := event.Close()
if err != nil {
combinedErr = multierr.Append(combinedErr, err)
}
}
return combinedErr
}

// New create new instance of event
// and registers with given registry
func New(name string, opts ...Option) Event {
Expand Down

0 comments on commit 7ce50d2

Please sign in to comment.