• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-server / 1495380963

14 Oct 2024 03:35PM UTC coverage: 70.373% (-2.5%) from 72.904%
1495380963

Pull #101

gitlab-ci

mineralsfree
feat: tenant list added

Ticket: MEN-7568
Changelog: None

Signed-off-by: Mikita Pilinka <mikita.pilinka@northern.tech>
Pull Request #101: feat: tenant list added

4406 of 6391 branches covered (68.94%)

Branch coverage included in aggregate %.

88 of 183 new or added lines in 10 files covered. (48.09%)

2623 existing lines in 65 files now uncovered.

36673 of 51982 relevant lines covered (70.55%)

31.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/backend/services/deviceconnect/client/nats/client.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package nats
16

17
import (
18
        "context"
19
        "time"
20

21
        "github.com/nats-io/nats.go"
22
        natsio "github.com/nats-io/nats.go"
23

24
        "github.com/mendersoftware/mender-server/pkg/log"
25
)
26

27
const (
28
        // Set reconnect buffer size in bytes (10 MB)
29
        reconnectBufSize = 10 * 1024 * 1024
30
        // Set reconnect interval to 1 second
31
        reconnectWaitTime = 1 * time.Second
32
)
33

34
// Client is the nats client
35
//
36
//go:generate ../../../../utils/mockgen.sh
37
type Client interface {
38
        Publish(string, []byte) error
39
        ChanSubscribe(string, chan *natsio.Msg) (*natsio.Subscription, error)
40
}
41

42
// NewClient returns a new workflows client
UNCOV
43
func NewClient(url string, opts ...natsio.Option) (Client, error) {
×
UNCOV
44
        natsClient, err := natsio.Connect(url, opts...)
×
UNCOV
45
        if err != nil {
×
46
                return nil, err
×
47
        }
×
UNCOV
48
        return &client{
×
UNCOV
49
                nats: natsClient,
×
UNCOV
50
        }, nil
×
51
}
52

53
// NewClient returns a new nats client with default options
UNCOV
54
func NewClientWithDefaults(url string) (Client, error) {
×
UNCOV
55
        ctx := context.Background()
×
UNCOV
56
        l := log.FromContext(ctx)
×
UNCOV
57

×
UNCOV
58
        natsClient, err := NewClient(url,
×
UNCOV
59
                func(o *nats.Options) error {
×
UNCOV
60
                        o.AllowReconnect = true
×
UNCOV
61
                        o.MaxReconnect = -1
×
UNCOV
62
                        o.ReconnectBufSize = reconnectBufSize
×
UNCOV
63
                        o.ReconnectWait = reconnectWaitTime
×
UNCOV
64
                        o.RetryOnFailedConnect = true
×
UNCOV
65
                        o.ClosedCB = func(_ *nats.Conn) {
×
66
                                l.Info("nats client closed the connection")
×
67
                        }
×
UNCOV
68
                        o.DisconnectedErrCB = func(_ *nats.Conn, e error) {
×
69
                                if e != nil {
×
70
                                        l.Warnf("nats client disconnected, err: %v", e)
×
71
                                }
×
72
                        }
UNCOV
73
                        o.ReconnectedCB = func(_ *nats.Conn) {
×
74
                                l.Warn("nats client reconnected")
×
75
                        }
×
UNCOV
76
                        return nil
×
77
                },
78
        )
UNCOV
79
        if err != nil {
×
80
                return nil, err
×
81
        }
×
UNCOV
82
        return natsClient, nil
×
83
}
84

85
type client struct {
86
        nats *natsio.Conn
87
}
88

UNCOV
89
func (c *client) Publish(subj string, data []byte) error {
×
UNCOV
90
        return c.nats.Publish(subj, data)
×
UNCOV
91
}
×
92

93
func (c *client) ChanSubscribe(subj string,
UNCOV
94
        channel chan *natsio.Msg) (*natsio.Subscription, error) {
×
UNCOV
95
        return c.nats.ChanSubscribe(subj, channel)
×
UNCOV
96
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc