Skip to content

Commit

Permalink
feat: adding configurable parameter for time in the listener (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
AshwinKul28 authored Oct 9, 2023
1 parent 7606292 commit 4ef1e23
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
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

0 comments on commit 4ef1e23

Please sign in to comment.