consumer constructor
This commit is contained in:
parent
9366cbbf6d
commit
4c5b1c7030
2 changed files with 22 additions and 8 deletions
17
consumer.go
17
consumer.go
|
|
@ -7,6 +7,23 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Consumer interface {
|
||||||
|
Start() chan []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type consumeHandler struct {
|
||||||
|
ctx context.Context
|
||||||
|
client *Client
|
||||||
|
chanLen int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *consumeHandler) Start() chan []byte {
|
||||||
|
msgCh := make(chan []byte, c.chanLen)
|
||||||
|
go runConsumer(c.ctx, c.client, msgCh)
|
||||||
|
|
||||||
|
return msgCh
|
||||||
|
}
|
||||||
|
|
||||||
func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
func runConsumer(ctx context.Context, client *Client, msgCh chan []byte) {
|
||||||
runCtx, cancel := context.WithCancel(ctx)
|
runCtx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
|
||||||
13
handler.go
13
handler.go
|
|
@ -64,13 +64,10 @@ func NewClient(addr, queueName string, opts ...Option) (*Client, error) {
|
||||||
return &client, nil
|
return &client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartConsumer(ctx context.Context, client *Client, chanLen int) chan []byte {
|
|
||||||
msgCh := make(chan []byte, chanLen)
|
|
||||||
go runConsumer(ctx, client, msgCh)
|
|
||||||
|
|
||||||
return msgCh
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPublisher(client *Client) Publisher {
|
func NewPublisher(client *Client) Publisher {
|
||||||
return &PubHandler{client: client}
|
return &pubHandler{client: client}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConsumer(ctx context.Context, client *Client, chanLen int) Consumer {
|
||||||
|
return &consumeHandler{ctx: ctx, client: client, chanLen: chanLen}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue