|
| 1 | +package mqtt |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "time" |
| 7 | + |
| 8 | + mqtt "github.com/eclipse/paho.mqtt.golang" |
| 9 | + "github.com/influxdata/flux" |
| 10 | + "github.com/influxdata/flux/codes" |
| 11 | + "github.com/influxdata/flux/internal/errors" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + DefaultTimeout = 1 * time.Second |
| 16 | + DefaultClientID = "flux-mqtt" |
| 17 | +) |
| 18 | + |
| 19 | +type key int |
| 20 | + |
| 21 | +const clientKey key = iota |
| 22 | + |
| 23 | +// Inject will inject this Dialer into the dependency chain. |
| 24 | +func Inject(ctx context.Context, dialer Dialer) context.Context { |
| 25 | + return context.WithValue(ctx, clientKey, dialer) |
| 26 | +} |
| 27 | + |
| 28 | +// Dependency will inject the Dialer into the dependency chain. |
| 29 | +type Dependency struct { |
| 30 | + Dialer Dialer |
| 31 | +} |
| 32 | + |
| 33 | +// Inject will inject the Dialer into the dependency chain. |
| 34 | +func (d Dependency) Inject(ctx context.Context) context.Context { |
| 35 | + return Inject(ctx, d.Dialer) |
| 36 | +} |
| 37 | + |
| 38 | +// GetDialer will return the Dialer for the current context. |
| 39 | +// If no Dialer has been injected into the dependencies, |
| 40 | +// this will return a default provider. |
| 41 | +func GetDialer(ctx context.Context) Dialer { |
| 42 | + p := ctx.Value(clientKey) |
| 43 | + if p == nil { |
| 44 | + return DefaultProvider{} |
| 45 | + } |
| 46 | + return p.(Dialer) |
| 47 | +} |
| 48 | + |
| 49 | +// Options contains additional options for configuring the mqtt client. |
| 50 | +type Options struct { |
| 51 | + ClientID string |
| 52 | + Username string |
| 53 | + Password string |
| 54 | + Timeout time.Duration |
| 55 | +} |
| 56 | + |
| 57 | +// Dialer provides a method to connect a client to one or more mqtt brokers. |
| 58 | +type Dialer interface { |
| 59 | + // Dial will connect to the given brokers and return a Client. |
| 60 | + Dial(ctx context.Context, brokers []string, options Options) (Client, error) |
| 61 | +} |
| 62 | + |
| 63 | +// Client is an mqtt client that can publish to an mqtt broker. |
| 64 | +type Client interface { |
| 65 | + // Publish will publish the payload to a particular topic. |
| 66 | + Publish(ctx context.Context, topic string, qos byte, retain bool, payload interface{}) error |
| 67 | + |
| 68 | + io.Closer |
| 69 | +} |
| 70 | + |
| 71 | +// DefaultProvider is the default provider that uses the default mqtt client. |
| 72 | +type DefaultProvider struct{} |
| 73 | + |
| 74 | +func (p DefaultProvider) Dial(ctx context.Context, brokers []string, options Options) (Client, error) { |
| 75 | + if len(brokers) == 0 { |
| 76 | + return nil, errors.New(codes.Invalid, "at least one broker is required for mqtt") |
| 77 | + } |
| 78 | + opts := mqtt.NewClientOptions() |
| 79 | + for _, broker := range brokers { |
| 80 | + opts.AddBroker(broker) |
| 81 | + } |
| 82 | + |
| 83 | + deps := flux.GetDependencies(ctx) |
| 84 | + if url, err := deps.URLValidator(); err != nil { |
| 85 | + return nil, err |
| 86 | + } else { |
| 87 | + for _, broker := range opts.Servers { |
| 88 | + if err := url.Validate(broker); err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if options.ClientID != "" { |
| 95 | + opts.SetClientID(options.ClientID) |
| 96 | + } else { |
| 97 | + opts.SetClientID(DefaultClientID) |
| 98 | + } |
| 99 | + |
| 100 | + if options.Timeout > 0 { |
| 101 | + opts.SetConnectTimeout(options.Timeout) |
| 102 | + } else { |
| 103 | + opts.SetConnectTimeout(DefaultTimeout) |
| 104 | + } |
| 105 | + |
| 106 | + if options.Username != "" { |
| 107 | + opts.SetUsername(options.Username) |
| 108 | + if options.Password != "" { |
| 109 | + opts.SetPassword(options.Password) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + client := mqtt.NewClient(opts) |
| 114 | + if token := client.Connect(); token.Wait() && token.Error() != nil { |
| 115 | + return nil, token.Error() |
| 116 | + } |
| 117 | + return &defaultClient{ |
| 118 | + client: client, |
| 119 | + timeout: options.Timeout, |
| 120 | + }, nil |
| 121 | +} |
| 122 | + |
| 123 | +type defaultClient struct { |
| 124 | + client mqtt.Client |
| 125 | + timeout time.Duration |
| 126 | +} |
| 127 | + |
| 128 | +func (d *defaultClient) Publish(ctx context.Context, topic string, qos byte, retain bool, payload interface{}) error { |
| 129 | + token := d.client.Publish(topic, qos, retain, payload) |
| 130 | + if !token.WaitTimeout(d.timeout) { |
| 131 | + return errors.New(codes.Canceled, "mqtt publish: timeout reached") |
| 132 | + } else if err := token.Error(); err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func (d *defaultClient) Close() error { |
| 139 | + d.client.Disconnect(250) |
| 140 | + return nil |
| 141 | +} |
0 commit comments