Skip to content

Commit

Permalink
Add nats request
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Apr 9, 2024
1 parent b3a10ae commit 3716926
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions providers/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package nats

import (
"context"
"fmt"
"time"

"github.com/nats-io/nats.go"
)
Expand Down Expand Up @@ -29,3 +31,17 @@ func (d *DotNats) Subscribe(subject string) (<-chan *nats.Msg, error) {
func (d *DotNats) Publish(subject, message string) error {
return d.conn.Publish(subject, []byte(message))
}

func (d *DotNats) Request(subject, data string, timeout_ ...time.Duration) (*nats.Msg, error) {
var timeout time.Duration
switch len(timeout_) {
case 0:
timeout = 1 * time.Second
case 1:
timeout = timeout_[0]
default:
return nil, fmt.Errorf("too many timeout args")
}

return d.conn.Request(subject, []byte(data), timeout)
}

0 comments on commit 3716926

Please sign in to comment.