Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configurable parameter for time in the listener #13

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion examples/base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func main() {
TableName: "messenger_messages",
}

err := gosumer.Listen(database, processMessage, Message{})
sec := 5

err := gosumer.Listen(database, processMessage, Message{}, sec)

if err != nil {
log.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions pgsql_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (database PgDatabase) connect() error {
return nil
}

func (database PgDatabase) listen(fn process, message any) error {
func (database PgDatabase) listen(fn process, message any, sec int) error {
err := database.connect()

if err != nil {
Expand All @@ -40,7 +40,7 @@ func (database PgDatabase) listen(fn process, message any) error {

defer pool.Close()

database.listenEvery(5, fn, message)
database.listenEvery(sec, fn, message)

log.Printf("Successfully connected to the database!")

Expand Down
2 changes: 1 addition & 1 deletion pgsql_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestPgListen(t *testing.T) {
defer pool.Close()

go func() {
err := database.listen(processMessage, Message{})
err := database.listen(processMessage, Message{}, 5)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type Transport interface {
connect() error
listen(fn process, message any) error
listen(fn process, message any, sec int) error
}

func formatMessage(message string, msg any) (any, error) {
Expand All @@ -17,8 +17,8 @@ func formatMessage(message string, msg any) (any, error) {
return msg, nil
}

func Listen(transport Transport, fn process, message any) error {
err := transport.listen(fn, message)
func Listen(transport Transport, fn process, message any, sec int) error {
err := transport.listen(fn, message, sec)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (transport TestTransport) connect() error {
return nil
}

func (transport TestTransport) listen(fn process, message any) error {
func (transport TestTransport) listen(fn process, message any, sec int) error {
go fn(message, make(chan error))

return nil
Expand All @@ -51,7 +51,7 @@ type Message struct {
func TestListen(t *testing.T) {
transport := TestTransport{}

err := Listen(transport, processMessage, Message{})
err := Listen(transport, processMessage, Message{}, 5)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
Expand Down