-
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.
* feat: add nsq output * feat: add nsq output * perf: add nsqd output doc
- Loading branch information
JTrancender
authored
Aug 11, 2021
1 parent
22af0a3
commit 49bac73
Showing
9 changed files
with
265 additions
and
10 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,77 @@ | ||
package nsq | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/JieTrancender/nsq_to_consumer/libconsumer/consumer" | ||
"github.com/JieTrancender/nsq_to_consumer/libconsumer/logp" | ||
"github.com/JieTrancender/nsq_to_consumer/libconsumer/outputs" | ||
"github.com/nsqio/go-nsq" | ||
) | ||
|
||
type client struct { | ||
logger *logp.Logger | ||
outputs.NetworkClient | ||
|
||
// for nsq | ||
nsqd string | ||
topic string | ||
enabledTopic bool | ||
producer *nsq.Producer | ||
config *nsq.Config | ||
|
||
mux sync.Mutex | ||
} | ||
|
||
func newNsqClient(config *Config) (*client, error) { | ||
cfg := nsq.NewConfig() | ||
cfg.WriteTimeout = config.WriteTimeout | ||
cfg.DialTimeout = config.DialTimeout | ||
c := &client{ | ||
logger: logp.NewLogger(logSelector), | ||
nsqd: config.Nsqd, | ||
topic: config.Topic, | ||
config: cfg, | ||
enabledTopic: config.EnabledTopic, | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *client) Close() error { | ||
c.producer.Stop() | ||
return nil | ||
} | ||
|
||
func (c *client) Connect() error { | ||
c.mux.Lock() | ||
defer c.mux.Unlock() | ||
|
||
c.logger.Debugf("connect: %v", c.nsqd) | ||
producer, err := nsq.NewProducer(c.nsqd, c.config) | ||
if err != nil { | ||
c.logger.Errorf("nsq connect fail with: %+v", err) | ||
return err | ||
} | ||
|
||
// todo: set logger | ||
c.producer = producer | ||
return nil | ||
} | ||
|
||
func (c *client) Publish(_ context.Context, m consumer.Message) error { | ||
if c.enabledTopic { | ||
return c.producer.Publish(c.topic, m.GetMessageBody()) | ||
} | ||
|
||
if m.GetNsqMessage().NSQDAddress == c.nsqd { | ||
c.logger.Debugf("The nsq address are same as the message's address, maybe endless") | ||
} | ||
|
||
return c.producer.Publish(m.GetTopic(), m.GetMessageBody()) | ||
} | ||
|
||
func (c *client) String() string { | ||
return "NSQD" | ||
} |
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,49 @@ | ||
package nsq | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/JieTrancender/nsq_to_consumer/libconsumer/common" | ||
) | ||
|
||
type Config struct { | ||
Nsqd string `config:"nsqd"` | ||
Topic string `config:"topic"` | ||
BulkMaxSize int `config:"bulk_max_size"` | ||
MaxRetries int `config:"max_retries"` | ||
WriteTimeout time.Duration `config:"write_timeout"` | ||
DialTimeout time.Duration `config:"dial_timeout"` | ||
|
||
// If not enabled topic, while using original topic | ||
EnabledTopic bool `config:"enabled_topic"` | ||
} | ||
|
||
func defaultConfig() Config { | ||
return Config{ | ||
Nsqd: "127.0.0.1:4150", | ||
Topic: "nsq_consumer", | ||
BulkMaxSize: 256, | ||
MaxRetries: 3, | ||
WriteTimeout: 6 * time.Second, | ||
DialTimeout: 6 * time.Second, | ||
EnabledTopic: false, | ||
} | ||
} | ||
|
||
func readConfig(cfg *common.Config) (*Config, error) { | ||
c := defaultConfig() | ||
if err := cfg.Unpack(&c); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &c, nil | ||
} | ||
|
||
func (c *Config) Validate() error { | ||
if c.EnabledTopic && c.Topic == "" { | ||
return fmt.Errorf("Topic can not be empty when enabled topic") | ||
} | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package nsq | ||
|
||
import ( | ||
"github.com/JieTrancender/nsq_to_consumer/libconsumer/common" | ||
"github.com/JieTrancender/nsq_to_consumer/libconsumer/consumer" | ||
"github.com/JieTrancender/nsq_to_consumer/libconsumer/outputs" | ||
) | ||
|
||
const ( | ||
logSelector = "nsqd" | ||
) | ||
|
||
func init() { | ||
outputs.RegisterType("nsqd", makeNsq) | ||
} | ||
|
||
func makeNsq( | ||
consumerInfo consumer.Info, | ||
cfg *common.Config, | ||
) (outputs.Group, error) { | ||
config, err := readConfig(cfg) | ||
if err != nil { | ||
return outputs.Fail(err) | ||
} | ||
|
||
client, err := newNsqClient(config) | ||
if err != nil { | ||
return outputs.Fail(err) | ||
} | ||
|
||
return outputs.Success(0, 0, client) | ||
} |
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