Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
queue complete
  • Loading branch information
chenyu468 committed Apr 11, 2024
commit b2b9a561431d6eaf37e409eb434d326e3168273f
17 changes: 16 additions & 1 deletion kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net"
"sync/atomic"
"time"

"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
Expand Down Expand Up @@ -68,8 +69,22 @@ func (w *Worker) Queue(job core.QueuedMessage) (err error) {
if atomic.LoadInt32(&w.stopFlag) == 1 {
return queue.ErrQueueShutdown
}
return err
// send message
base := time.Now()

msg := kafkaAPI.Message{
Time: base.Truncate(time.Millisecond),
Value: job.Bytes(),
}
if w.opts.compression == nil {
_, err = w.conn.WriteMessages(msg)
} else {
_, err = w.conn.WriteCompressedMessages(w.opts.compression, msg)
}
// if err != nil {
// t.Fatal(err)
// }
return err
}

func (w *Worker) Request() (core.QueuedMessage, error) {
Expand Down
23 changes: 16 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import (

"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
"github.com/segmentio/kafka-go/compress"
)

// Option for queue system
type Option func(*options)

type options struct {
runFunc func(context.Context, core.QueuedMessage) error
logger queue.Logger
addr string
network string
queue string
topic string
partition int //kafka's partition
runFunc func(context.Context, core.QueuedMessage) error
logger queue.Logger
addr string
network string
queue string
topic string
partition int //kafka's partition
compression compress.Codec
}

// WithAddr setup the URI
Expand Down Expand Up @@ -54,6 +56,13 @@ func WithQueue(val string) Option {
}
}

// WithAddr setup the URI
func WithCompress(compress compress.Codec) Option {
return func(w *options) {
w.compression = compress
}
}

// WithRunFunc setup the run func of queue
func WithRunFunc(fn func(context.Context, core.QueuedMessage) error) Option {
return func(w *options) {
Expand Down