atmost working example
This commit is contained in:
86
internal/interconnect/collector/client.go
Normal file
86
internal/interconnect/collector/client.go
Normal file
@ -0,0 +1,86 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"git.loyso.art/frx/devsim/internal/entities"
|
||||
)
|
||||
|
||||
type upsertRequest struct {
|
||||
IncomingTraffic int `json:"incoming_traffic"`
|
||||
OutgoingTraffic int `json:"outgoing_traffic"`
|
||||
IncomingRPS int `json:"incoming_rps"`
|
||||
ReadRPS int `json:"read_rps"`
|
||||
WriteRPS int `json:"write_rps"`
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
Upsert(context.Context, entities.DeviceStatistics) error
|
||||
}
|
||||
|
||||
type client struct {
|
||||
httpClient *http.Client
|
||||
baseurl string
|
||||
}
|
||||
|
||||
func New(addr string) (*client, error) {
|
||||
hc := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: time.Second * 10,
|
||||
KeepAlive: time.Second * 30,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 10,
|
||||
IdleConnTimeout: time.Second * 90,
|
||||
TLSHandshakeTimeout: time.Second * 5,
|
||||
ExpectContinueTimeout: time.Second * 1,
|
||||
ForceAttemptHTTP2: true,
|
||||
},
|
||||
Timeout: time.Second * 10,
|
||||
}
|
||||
|
||||
return &client{
|
||||
httpClient: hc,
|
||||
baseurl: addr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var upsertRequestTemplate = template.Must(template.New("request").Parse(`{"incoming_traffic":{{.IncomingTrafficBytes}},"outgoing_traffic":{{.OutgoingTrafficBytes}},"incoming_rps":{{.IncomingRPS}},"read_rps":{{.ReadRPS}},"write_rps":{{.WriteRPS}}}`))
|
||||
|
||||
func (c *client) Upsert(ctx context.Context, stat entities.DeviceStatistics) error {
|
||||
var buf bytes.Buffer
|
||||
err := upsertRequestTemplate.Lookup("request").Execute(&buf, stat)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing template: %w", err)
|
||||
}
|
||||
|
||||
path := c.baseurl + "/api/v1/stats/" + string(stat.ID)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path, &buf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("preparing http request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing request: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading body by status code %d: %w", resp.StatusCode, err)
|
||||
}
|
||||
|
||||
return fmt.Errorf("expected status 200 got %d with body: %q", resp.StatusCode, data)
|
||||
}
|
||||
Reference in New Issue
Block a user