Skip to content

Commit

Permalink
works if response is not too big
Browse files Browse the repository at this point in the history
  • Loading branch information
j-bro committed Nov 1, 2023
1 parent 1c3a254 commit e68565e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 55 deletions.
64 changes: 26 additions & 38 deletions lib/svrquery/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package svrquery

import (
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -32,26 +33,10 @@ type Client struct {
key string
timeout time.Duration
protocol.Queryer
Transport Transport
transport
}

func (c *Client) Read(p []byte) (n int, err error) {
return c.Transport.Read(p)
}

func (c *Client) Write(p []byte) (n int, err error) {
return c.Transport.Write(p)
}

func (c *Client) Close() error {
return c.Transport.Close()
}

func (c *Client) Address() string {
return c.Transport.Address()
}

type Transport interface {
type transport interface {
Setup() error
Address() string
Read(b []byte) (int, error)
Expand All @@ -71,7 +56,7 @@ func WithKey(key string) Option {
// WithTimeout sets the read and write timeout for the client.
func WithTimeout(t time.Duration) Option {
return func(c *Client) error {
c.Transport.SetTimeout(t)
c.transport.SetTimeout(t)
return nil
}
}
Expand All @@ -95,12 +80,12 @@ func NewClient(proto, addr string, options ...Option) (*Client, error) {
}
}

var t Transport
var t transport
switch proto {
case "sqp":
t = &udpTransport{address: addr}
case "prom":
t = &HTTPTransport{}
t = &httpTransport{address: addr}
default:
return nil, fmt.Errorf("protocol %s not supported", proto)
}
Expand All @@ -109,7 +94,7 @@ func NewClient(proto, addr string, options ...Option) (*Client, error) {
return nil, fmt.Errorf("setup client transport: %w", err)
}

c.Transport = t
c.transport = t

return c, nil
}
Expand All @@ -118,8 +103,8 @@ func (c *Client) Query() (protocol.Responser, error) {
return c.Queryer.Query()
}

var _ Transport = (*udpTransport)(nil)
var _ Transport = (*HTTPTransport)(nil)
var _ transport = (*udpTransport)(nil)
var _ transport = (*httpTransport)(nil)

type udpTransport struct {
address string
Expand Down Expand Up @@ -193,36 +178,39 @@ func (c *Client) Protocol() string {
return c.protocol
}

type HTTPTransport struct {
type httpTransport struct {
address string
HttpClient *http.Client
httpClient *http.Client
}

func (h HTTPTransport) Setup() error {
h.HttpClient = &http.Client{}
func (h *httpTransport) Setup() error {
h.httpClient = &http.Client{}
return nil
}

func (h HTTPTransport) Address() string {
func (h *httpTransport) Address() string {
return h.address
}

func (h HTTPTransport) Read(b []byte) (int, error) {
//TODO implement me
panic("implement me")
func (h *httpTransport) Read(b []byte) (int, error) {
res, err := h.httpClient.Get(h.address)
if err != nil {
return 0, fmt.Errorf("http get: %w", err)
}

return res.Body.Read(b)
}

func (h HTTPTransport) Write(b []byte) (int, error) {
//TODO implement me
panic("implement me")
func (h *httpTransport) Write(b []byte) (int, error) {
return 0, errors.New("httpTransport.Write is unused")
}

// Close implements io.Closer.
func (h HTTPTransport) Close() error {
func (h *httpTransport) Close() error {
// no-op
return nil
}

func (h HTTPTransport) SetTimeout(t time.Duration) {
h.HttpClient.Timeout = t
func (h *httpTransport) SetTimeout(t time.Duration) {
h.httpClient.Timeout = t
}
33 changes: 16 additions & 17 deletions lib/svrquery/protocol/prom/query.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package prom

import (
"bytes"
"fmt"
"github.com/multiplay/go-svrquery/lib/svrquery"
"strconv"

"github.com/multiplay/go-svrquery/lib/svrquery/protocol"
"github.com/prometheus/common/expfmt"
"io"
"strconv"
)

const defaultBufSize = 4096

type queryer struct {
c protocol.Client
client protocol.Client
}

func newCreator(c protocol.Client) protocol.Queryer {
return newQueryer(c)
}

func newQueryer(c protocol.Client) *queryer {
func newQueryer(client protocol.Client) *queryer {
return &queryer{
c: c,
client: client,
}
}

Expand All @@ -29,20 +31,17 @@ func (q *queryer) Query() (protocol.Responser, error) {
}

func (q *queryer) makeQuery() (*QueryResponse, error) {
client, ok := q.c.(*svrquery.Client)
if !ok {
return nil, fmt.Errorf("expected svrquery.Client, got %T", q.c)
}
httpTransport, ok := client.Transport.(svrquery.HTTPTransport)
if !ok {
return nil, fmt.Errorf("expected svrquery.HTTPTransport, got %T", q.c)
// FIXME: this won't work if the response is larger than defaultBufSize
responseBytes := make([]byte, defaultBufSize)
n, err := q.client.Read(responseBytes)
if n > 0 {
responseBytes = responseBytes[:n]
}
res, err := httpTransport.HttpClient.Get(q.c.Address())
if err != nil {
return nil, fmt.Errorf("http get: %w", err)
if err != nil && err != io.EOF {
return nil, fmt.Errorf("query response: %w", err)
}
var parser expfmt.TextParser
metrics, err := parser.TextToMetricFamilies(res.Body)
metrics, err := parser.TextToMetricFamilies(bytes.NewReader(responseBytes))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e68565e

Please sign in to comment.