Skip to content

Commit

Permalink
fix: client null handler
Browse files Browse the repository at this point in the history
  • Loading branch information
amirhnajafiz committed Aug 11, 2022
1 parent 9e3a27d commit 5e282a0
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
"time"
)

// we need safety timeout, to prevent send more than
// one message in http request.
const (
safetyTimeout = 1
)

// client is our user application handler.
type client struct {
// map of topics
Expand Down Expand Up @@ -94,27 +100,29 @@ func (c *client) Publish(topic string, data []byte) error {
return err
}

time.Sleep(1 * time.Millisecond)
time.Sleep(safetyTimeout * time.Millisecond)

return nil
}

// Subscribe subscribes over broker.
func (c *client) Subscribe(topic string, handler MessageHandler) {
// set a handler for given topic
c.topics[topic] = handler

// send an http request to broker server
err := c.network.send(encodeMessage(newMessage(Subscribe, topic, nil)))
if err != nil {
log.Fatal(err)
}

time.Sleep(1 * time.Millisecond)

// set a handler for given topic
c.topics[topic] = handler
time.Sleep(safetyTimeout * time.Millisecond)
}

// Unsubscribe removes client from subscribing over a topic.
func (c *client) Unsubscribe(topic string) {
_ = c.network.send(encodeMessage(newMessage(Unsubscribe, topic, nil)))
time.Sleep(1 * time.Millisecond)
time.Sleep(safetyTimeout * time.Millisecond)

// remove topic and its handler
delete(c.topics, topic)
Expand Down

0 comments on commit 5e282a0

Please sign in to comment.