Skip to content

Commit

Permalink
Merge pull request #12 from mailgun/new-with-context
Browse files Browse the repository at this point in the history
Added NewWithContext()
  • Loading branch information
anton-efimenko authored Sep 6, 2018
2 parents 901b4e6 + ff1ecfd commit 7857322
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions kafkahook/kafkahook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/mailgun/logrus-hooks/common"
"github.com/mailru/easyjson/jwriter"
"github.com/sirupsen/logrus"
"context"
)

const bufferSize = 150
Expand All @@ -39,6 +40,22 @@ type Config struct {
Producer sarama.AsyncProducer
}

func NewWithContext(ctx context.Context, conf Config) (hook *KafkaHook, err error) {
done := make(chan struct{})
go func() {
hook, err = New(conf)
close(done)
}()

select {
case <-ctx.Done():
return hook, fmt.Errorf("connect timeout while connecting to kafka peers %s",
strings.Join(conf.Endpoints, ","))
case <-done:
return hook, err
}
}

func New(conf Config) (*KafkaHook, error) {
var err error

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.1
1.1.3

0 comments on commit 7857322

Please sign in to comment.