|
| 1 | +/* |
| 2 | + * Copyright 2024 Function Stream Org. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package contube |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/functionstream/function-stream/common" |
| 24 | + "github.com/nats-io/nats.go" |
| 25 | + "github.com/pkg/errors" |
| 26 | +) |
| 27 | + |
| 28 | +type NatsTubeFactoryConfig struct { |
| 29 | + NatsURL string `json:"nats_url"` |
| 30 | +} |
| 31 | + |
| 32 | +type NatsEventQueueFactory struct { |
| 33 | + nc *nats.Conn |
| 34 | +} |
| 35 | + |
| 36 | +type NatsSourceTubeConfig struct { |
| 37 | + Subject string `json:"subject" validate:"required"` |
| 38 | +} |
| 39 | + |
| 40 | +func (n NatsEventQueueFactory) NewSourceTube(ctx context.Context, configMap ConfigMap) (<-chan Record, error) { |
| 41 | + config := &NatsSourceTubeConfig{} |
| 42 | + if err := configMap.ToConfigStruct(config); err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + c := make(chan Record) |
| 46 | + sub, err := n.nc.SubscribeSync(config.Subject) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + log := common.NewDefaultLogger() |
| 51 | + go func() { |
| 52 | + for { |
| 53 | + msg, err := sub.NextMsg(10 * time.Millisecond) |
| 54 | + if err != nil { |
| 55 | + if !errors.Is(err, nats.ErrTimeout) { |
| 56 | + log.Error(err, "Failed to get next message", "subject", config.Subject) |
| 57 | + } |
| 58 | + continue |
| 59 | + } |
| 60 | + select { |
| 61 | + case c <- NewRecordImpl(msg.Data, func() { |
| 62 | + _ = msg.Ack() |
| 63 | + }): // do nothing |
| 64 | + case <-ctx.Done(): |
| 65 | + return |
| 66 | + } |
| 67 | + } |
| 68 | + }() |
| 69 | + return c, nil |
| 70 | +} |
| 71 | + |
| 72 | +type NatsSinkTubeConfig struct { |
| 73 | + Subject string `json:"subject" validate:"required"` |
| 74 | +} |
| 75 | + |
| 76 | +func (n NatsEventQueueFactory) NewSinkTube(ctx context.Context, configMap ConfigMap) (chan<- Record, error) { |
| 77 | + config := &NatsSinkTubeConfig{} |
| 78 | + if err := configMap.ToConfigStruct(config); err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + c := make(chan Record) |
| 82 | + log := common.NewDefaultLogger() |
| 83 | + go func() { |
| 84 | + for { |
| 85 | + select { |
| 86 | + case <-ctx.Done(): |
| 87 | + return |
| 88 | + case event, ok := <-c: |
| 89 | + if !ok { |
| 90 | + return |
| 91 | + } |
| 92 | + err := n.nc.Publish(config.Subject, event.GetPayload()) |
| 93 | + log.Info("Published message", "subject", config.Subject, "err", err) |
| 94 | + if err != nil { |
| 95 | + log.Error(err, "Failed to publish message", "subject", config.Subject) |
| 96 | + continue |
| 97 | + } |
| 98 | + event.Commit() |
| 99 | + } |
| 100 | + } |
| 101 | + }() |
| 102 | + return c, nil |
| 103 | +} |
| 104 | + |
| 105 | +func NewNatsEventQueueFactory(ctx context.Context, configMap ConfigMap) (TubeFactory, error) { |
| 106 | + config := &NatsTubeFactoryConfig{} |
| 107 | + if err := configMap.ToConfigStruct(config); err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + if config.NatsURL == "" { |
| 111 | + config.NatsURL = "nats://localhost:4222" |
| 112 | + } |
| 113 | + nc, err := nats.Connect(config.NatsURL) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + log := common.NewDefaultLogger() |
| 118 | + go func() { |
| 119 | + <-ctx.Done() |
| 120 | + // Close the nats queue factory |
| 121 | + log.Info("Closing nats queue factory", "url", config.NatsURL) |
| 122 | + err := nc.Drain() |
| 123 | + if err != nil { |
| 124 | + log.Error(err, "Failed to drain nats connection", "url", config.NatsURL) |
| 125 | + } |
| 126 | + }() |
| 127 | + return &NatsEventQueueFactory{ |
| 128 | + nc: nc, |
| 129 | + }, nil |
| 130 | +} |
0 commit comments