summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/uwsgi/client.go
blob: 12138672f4eb122d15f9ee5cb48f1fa441fbb896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: GPL-3.0-or-later

package uwsgi

import (
	"bytes"
	"fmt"
	"time"

	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/socket"
)

type uwsgiConn interface {
	queryStats() ([]byte, error)
}

func newUwsgiConn(conf Config) uwsgiConn {
	return &uwsgiClient{
		address: conf.Address,
		timeout: conf.Timeout.Duration(),
	}
}

type uwsgiClient struct {
	address string
	timeout time.Duration
}

func (c *uwsgiClient) queryStats() ([]byte, error) {
	var b bytes.Buffer
	var n int64
	var err error
	const readLineLimit = 1000 * 10

	cfg := socket.Config{
		Address: c.address,
		Timeout: c.timeout,
	}

	clientErr := socket.ConnectAndRead(cfg, func(bs []byte) bool {
		b.Write(bs)
		b.WriteByte('\n')

		if n++; n >= readLineLimit {
			err = fmt.Errorf("read line limit exceeded %d", readLineLimit)
			return false
		}
		// The server will close the connection when it has finished sending data.
		return true
	})
	if clientErr != nil {
		return nil, clientErr
	}
	if err != nil {
		return nil, err
	}

	return b.Bytes(), nil
}