-
Notifications
You must be signed in to change notification settings - Fork 42
Sender plugin: Nats #846
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
Open
muhammad-asghar-ali
wants to merge
20
commits into
resonatehq:main
Choose a base branch
from
muhammad-asghar-ali:nats-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sender plugin: Nats #846
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b40c300
feat(plugin): add prototype NATS.io plugin for sender subsystem
muhammad-asghar-ali 1d29663
refactor(nats.io): remove unused method from NATS plugin interface
muhammad-asghar-ali 2ab6202
fix(nats.io): prevent panic on closed channel in NATS.io mock client …
muhammad-asghar-ali d187fa7
refactor(nats.io): create dedicated NATS.io connection per worker for…
muhammad-asghar-ali 38e2978
feat(NATS.io): use nats:///subject format to remove misleading host info
muhammad-asghar-ali eea2f9b
fix(): improve code coverage
muhammad-asghar-ali 578cdfa
fix(): golangci-lint issues
muhammad-asghar-ali c545554
Merge branch 'main' into nats-plugin
muhammad-asghar-ali 8a73138
refactor(): convert to use base plugin pattern
muhammad-asghar-ali d4015c1
fix(): pointer addr
muhammad-asghar-ali d53e392
Merge branch 'resonatehq:main' into main
muhammad-asghar-ali 551f672
Merge branch 'main' into nats-plugin
muhammad-asghar-ali 9dbd616
chore(): add head in nats plugin process func
muhammad-asghar-ali 2152d44
fix(): pass correct args to process
muhammad-asghar-ali 3107adc
Merge branch 'main' into nats-plugin
muhammad-asghar-ali 9e672df
Merge branch 'main' into nats-plugin
muhammad-asghar-ali b983d37
sync with main
muhammad-asghar-ali 32df92c
chore(): go mod tidy
muhammad-asghar-ali 6484dd5
feat(): add NATS subsystem and plugin implementation with sender support
muhammad-asghar-ali 2c65de2
chore(nats): remove the nats from api to remove the complexity
muhammad-asghar-ali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| package nats | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log/slog" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| natsgo "github.com/nats-io/nats.go" | ||
|
|
||
| "github.com/resonatehq/resonate/internal/aio" | ||
| "github.com/resonatehq/resonate/internal/kernel/t_aio" | ||
| "github.com/resonatehq/resonate/internal/metrics" | ||
| ) | ||
|
|
||
| type Config struct { | ||
| Size int `flag:"size" desc:"submission buffered channel size" default:"1000"` | ||
| Workers int `flag:"workers" desc:"number of workers" default:"4"` | ||
| Timeout time.Duration `flag:"timeout" desc:"nats request timeout" default:"30s"` | ||
| TimeToRetry time.Duration `flag:"ttr" desc:"time to wait before resending" default:"15s"` | ||
| TimeToClaim time.Duration `flag:"ttc" desc:"time to wait for claim before resending" default:"1m"` | ||
| } | ||
|
|
||
| type Client interface { | ||
| Publish(subject string, data []byte) error | ||
| Close() | ||
| } | ||
|
|
||
| type Worker struct { | ||
| id int | ||
| sq <-chan *aio.Message | ||
| timeout time.Duration | ||
| aio aio.AIO | ||
| metrics *metrics.Metrics | ||
| config *Config | ||
| client Client | ||
| } | ||
|
|
||
| type NATS struct { | ||
| sq chan *aio.Message | ||
| workers []*Worker | ||
| } | ||
|
|
||
| type Addr struct { | ||
| URL string `json:"url"` | ||
| Subject string `json:"subject"` | ||
| } | ||
|
|
||
| func New(a aio.AIO, metrics *metrics.Metrics, config *Config) (*NATS, error) { | ||
| return NewWithClient(a, metrics, config, nil) | ||
| } | ||
|
|
||
| func NewWithClient(a aio.AIO, metrics *metrics.Metrics, config *Config, client Client) (*NATS, error) { | ||
| sq := make(chan *aio.Message, config.Size) | ||
| workers := make([]*Worker, config.Workers) | ||
|
|
||
| for i := 0; i < config.Workers; i++ { | ||
| workers[i] = &Worker{ | ||
| id: i, | ||
| sq: sq, | ||
| timeout: config.Timeout, | ||
| aio: a, | ||
| metrics: metrics, | ||
| config: config, | ||
| client: client, | ||
| } | ||
| } | ||
|
|
||
| return &NATS{ | ||
| sq: sq, | ||
| workers: workers, | ||
| }, nil | ||
| } | ||
|
|
||
| func (p *NATS) String() string { | ||
| return fmt.Sprintf("%s:nats", t_aio.Sender.String()) | ||
| } | ||
|
|
||
| func (p *NATS) Type() string { | ||
| return "nats" | ||
| } | ||
|
|
||
| func (p *NATS) Start(chan<- error) error { | ||
| for _, worker := range p.workers { | ||
| go worker.Start() | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *NATS) Stop() error { | ||
| if p.sq != nil { | ||
| close(p.sq) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (p *NATS) Enqueue(msg *aio.Message) bool { | ||
| if p.sq == nil || msg == nil { | ||
| return false | ||
| } | ||
|
|
||
| select { | ||
| case p.sq <- msg: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func (w *Worker) String() string { | ||
| return fmt.Sprintf("%s:nats", t_aio.Sender.String()) | ||
| } | ||
|
|
||
| func (w *Worker) Start() { | ||
| counter := w.metrics.AioWorkerInFlight.WithLabelValues(w.String(), strconv.Itoa(w.id)) | ||
| w.metrics.AioWorker.WithLabelValues(w.String()).Inc() | ||
| defer w.metrics.AioWorker.WithLabelValues(w.String()).Dec() | ||
|
|
||
| for { | ||
| msg, ok := <-w.sq | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| counter.Inc() | ||
| success, err := w.Process(msg.Body, msg.Addr) | ||
| if err != nil { | ||
| slog.Warn("failed to send task", "err", err) | ||
| } | ||
|
|
||
| msg.Done(&t_aio.SenderCompletion{ | ||
| Success: success, | ||
| TimeToRetry: w.config.TimeToRetry.Milliseconds(), | ||
| TimeToClaim: w.config.TimeToClaim.Milliseconds(), | ||
| }) | ||
| counter.Dec() | ||
| } | ||
| } | ||
|
|
||
| func (w *Worker) Process(body []byte, data []byte) (bool, error) { | ||
| var addr Addr | ||
| if err := json.Unmarshal(data, &addr); err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| // TODO - need to find the best approach to handle this | ||
| if addr.URL == "" { | ||
| return false, fmt.Errorf("missing URL") | ||
| } | ||
| if addr.Subject == "" { | ||
| return false, fmt.Errorf("missing subject") | ||
| } | ||
|
|
||
| client := w.client | ||
| if client == nil { | ||
| opts := []natsgo.Option{ | ||
| natsgo.Timeout(w.timeout), | ||
| natsgo.RetryOnFailedConnect(false), // Disable retry for timeout testing | ||
| natsgo.PingInterval(20 * time.Second), | ||
| natsgo.MaxPingsOutstanding(2), | ||
| } | ||
|
|
||
| nc, err := natsgo.Connect(addr.URL, opts...) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| client = nc | ||
| defer nc.Close() | ||
| } | ||
|
|
||
| // If we have a client (including mock clients), use it directly | ||
| if w.client != nil { | ||
| err := client.Publish(addr.Subject, body) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| // Only use timeout logic for real NATS connections | ||
| ctx, cancel := context.WithTimeout(context.Background(), w.timeout) | ||
dfarr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| defer cancel() | ||
|
|
||
| done := make(chan error, 1) | ||
| go func() { | ||
| done <- client.Publish(addr.Subject, body) | ||
| }() | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return true, nil | ||
| case <-ctx.Done(): | ||
| return false, ctx.Err() | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.