master
go 49 lines 1001 Bytes
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package memcached
4
5 import (
6 "bytes"
7 "strings"
8
9 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/socket"
10 )
11
12 type memcachedConn interface {
13 connect() error
14 disconnect()
15 queryStats() ([]byte, error)
16 }
17
18 func newMemcachedConn(conf Config) memcachedConn {
19 return &memcachedClient{conn: socket.New(socket.Config{
20 Address: conf.Address,
21 Timeout: conf.Timeout.Duration(),
22 })}
23 }
24
25 type memcachedClient struct {
26 conn socket.Client
27 }
28
29 func (c *memcachedClient) connect() error {
30 return c.conn.Connect()
31 }
32
33 func (c *memcachedClient) disconnect() {
34 _ = c.conn.Disconnect()
35 }
36
37 func (c *memcachedClient) queryStats() ([]byte, error) {
38 var b bytes.Buffer
39 if err := c.conn.Command("stats\r\n", func(bytes []byte) (bool, error) {
40 s := strings.TrimSpace(string(bytes))
41 b.WriteString(s)
42 b.WriteByte('\n')
43
44 return !(strings.HasPrefix(s, "END") || strings.HasPrefix(s, "ERROR")), nil
45 }); err != nil {
46 return nil, err
47 }
48 return b.Bytes(), nil
49 }