forked from smancke/guble
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed name and interface funcs for KafkaProducer, and made it compa…
…tible with gobbler's module system; configuration for Kafka (brokers)
- Loading branch information
Cosmin Rentea
committed
Mar 29, 2017
1 parent
6487e57
commit 6ee8966
Showing
5 changed files
with
102 additions
and
58 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,38 @@ | ||
package configstring | ||
|
||
import ( | ||
"strings" | ||
|
||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
type List []string | ||
|
||
func NewFromKingpin(settings kingpin.Settings) *List { | ||
sl := make(List, 0) | ||
settings.SetValue(&sl) | ||
return &sl | ||
} | ||
|
||
func (sl *List) Set(value string) error { | ||
delimiter := " " | ||
if strings.Contains(value, ",") { | ||
delimiter = "," | ||
} | ||
slice := strings.Split(value, delimiter) | ||
for _, s := range slice { | ||
if s != "" { | ||
*sl = append(*sl, s) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (sl List) String() string { | ||
res := "[" | ||
for _, s := range sl { | ||
res = res + " " + s | ||
} | ||
res = res + "]" | ||
return res | ||
} |
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 kafka | ||
|
||
import ( | ||
"github.com/Shopify/sarama" | ||
"github.com/cosminrentea/gobbler/server/configstring" | ||
"github.com/cosminrentea/gobbler/server/service" | ||
) | ||
|
||
type Producer interface { | ||
service.Stopable | ||
Report(topic string, bytes []byte) | ||
} | ||
|
||
type Config struct { | ||
Brokers *configstring.List | ||
} | ||
|
||
type producer struct { | ||
Config | ||
|
||
asyncProducer sarama.AsyncProducer | ||
} | ||
|
||
func NewProducer(c Config) (Producer, error) { | ||
logger.WithField("config", c).Info("NewProducer") | ||
saramaConfig := sarama.NewConfig() | ||
saramaConfig.Version = sarama.V0_10_1_0 | ||
saramaConfig.Producer.Return.Errors = false | ||
saramaConfig.Producer.Retry.Max = 10 | ||
p, err := sarama.NewAsyncProducer(*c.Brokers, saramaConfig) | ||
if err != nil { | ||
logger.WithError(err).Error("Could not create AsyncProducer") | ||
return nil, err | ||
} | ||
return &producer{ | ||
Config: c, | ||
asyncProducer: p, | ||
}, nil | ||
} | ||
|
||
func (p *producer) Report(topic string, bytes []byte) { | ||
p.asyncProducer.Input() <- &sarama.ProducerMessage{ | ||
Topic: topic, | ||
Value: sarama.ByteEncoder(bytes), | ||
} | ||
} | ||
|
||
func (p *producer) Stop() error { | ||
logger.Info("Stop") | ||
if err := p.asyncProducer.Close(); err != nil { | ||
logger.WithError(err).Error("Could not close Kafka Producer") | ||
return err | ||
} | ||
return nil | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package kafkareporter | ||
package kafka | ||
|
||
import ( | ||
log "github.com/Sirupsen/logrus" | ||
|
This file was deleted.
Oops, something went wrong.