Commit 3292cd83 authored by ale's avatar ale

Pass a Context to client RPC methods

parent 8c95549d
Pipeline #650 passed with stages
in 5 minutes and 55 seconds
package client
import (
"context"
"crypto/tls"
"net/http"
"net/url"
......@@ -39,36 +40,36 @@ func New(backendURL string, tlsConfig *tls.Config) (*Client, error) {
}, nil
}
func (c *Client) CheckDevice(username string, dev *auth.DeviceInfo) (bool, error) {
func (c *Client) CheckDevice(ctx context.Context, username string, dev *auth.DeviceInfo) (bool, error) {
req := usermetadb.CheckDeviceRequest{
Username: username,
DeviceInfo: dev,
}
var resp usermetadb.CheckDeviceResponse
err := clientutil.DoJSONHTTPRequest(c.Client, c.backendURL+"/api/check_device", &req, &resp)
err := clientutil.DoJSONHTTPRequest(ctx, c.Client, c.backendURL+"/api/check_device", &req, &resp)
return resp.Seen, err
}
func (c *Client) AddLog(entry *usermetadb.LogEntry) error {
func (c *Client) AddLog(ctx context.Context, entry *usermetadb.LogEntry) error {
req := usermetadb.AddLogRequest{Log: entry}
return clientutil.DoJSONHTTPRequest(c.Client, c.backendURL+"/api/add_log", &req, nil)
return clientutil.DoJSONHTTPRequest(ctx, c.Client, c.backendURL+"/api/add_log", &req, nil)
}
func (c *Client) GetUserDevices(username string) ([]*usermetadb.MetaDeviceInfo, error) {
func (c *Client) GetUserDevices(ctx context.Context, username string) ([]*usermetadb.MetaDeviceInfo, error) {
req := usermetadb.GetUserDevicesRequest{Username: username}
var resp usermetadb.GetUserDevicesResponse
err := clientutil.DoJSONHTTPRequest(c.Client, c.backendURL+"/api/get_user_devices", &req, &resp)
err := clientutil.DoJSONHTTPRequest(ctx, c.Client, c.backendURL+"/api/get_user_devices", &req, &resp)
return resp.Devices, err
}
func (c *Client) GetUserLogs(username string, maxDays, limit int) ([]*usermetadb.LogEntry, error) {
func (c *Client) GetUserLogs(ctx context.Context, username string, maxDays, limit int) ([]*usermetadb.LogEntry, error) {
req := usermetadb.GetUserLogsRequest{
Username: username,
MaxDays: maxDays,
Limit: limit,
}
var resp usermetadb.GetUserLogsResponse
err := clientutil.DoJSONHTTPRequest(c.Client, c.backendURL+"/api/get_user_logs", &req, &resp)
err := clientutil.DoJSONHTTPRequest(ctx, c.Client, c.backendURL+"/api/get_user_logs", &req, &resp)
return resp.Results, err
}
package server
import (
"context"
"net/http/httptest"
"os"
"testing"
......@@ -21,7 +22,7 @@ func TestServer_AddLog(t *testing.T) {
c, _ := client.New(httpSrv.URL, nil)
entries := generateTestLogs(100, generateAllRandomDevices())
for _, e := range entries {
if err := c.AddLog(e); err != nil {
if err := c.AddLog(context.Background(), e); err != nil {
t.Fatalf("AddLog(%+v): %v", e, err)
}
}
......@@ -42,7 +43,7 @@ func BenchmarkServer_AddLog(b *testing.B) {
c, _ := client.New(httpSrv.URL, nil)
entries := generateTestLogs(b.N, generateAllRandomDevices())
for _, e := range entries {
if err := c.AddLog(e); err != nil {
if err := c.AddLog(context.Background(), e); err != nil {
b.Fatalf("AddLog(%+v): %v", e, err)
}
}
......
......@@ -2,13 +2,17 @@ package clientutil
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func DoJSONHTTPRequest(client *http.Client, uri string, req, resp interface{}) error {
// DoJSONHTTPRequest makes an HTTP POST request to the specified uri,
// with a JSON-encoded request body. It will attempt to decode the
// response body as JSON.
func DoJSONHTTPRequest(ctx context.Context, client *http.Client, uri string, req, resp interface{}) error {
data, err := json.Marshal(req)
if err != nil {
return err
......@@ -19,6 +23,7 @@ func DoJSONHTTPRequest(client *http.Client, uri string, req, resp interface{}) e
return err
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq = httpReq.WithContext(ctx)
httpResp, err := RetryHTTPDo(client, httpReq, NewExponentialBackOff())
if err != nil {
......
......@@ -30,19 +30,21 @@ type ServerConfig struct {
// SIGINT or SIGTERM and return nil.
func Serve(h http.Handler, serverConfig *ServerConfig, addr string) (err error) {
var tlsConfig *tls.Config
if serverConfig.TLS != nil {
tlsConfig, err = serverConfig.TLS.TLSConfig()
if err != nil {
return err
}
h, err = serverConfig.TLS.TLSAuthWrapper(h)
if err != nil {
return err
if serverConfig != nil {
if serverConfig.TLS != nil {
tlsConfig, err = serverConfig.TLS.TLSConfig()
if err != nil {
return err
}
h, err = serverConfig.TLS.TLSAuthWrapper(h)
if err != nil {
return err
}
}
}
if serverConfig.MaxInflightRequests > 0 {
h = newLoadSheddingWrapper(serverConfig.MaxInflightRequests, h)
if serverConfig.MaxInflightRequests > 0 {
h = newLoadSheddingWrapper(serverConfig.MaxInflightRequests, h)
}
}
srv := &http.Server{
......
......@@ -5,20 +5,20 @@
{
"checksumSHA1": "raJx5BjBbVQG0ylGSjPpi+JvqjU=",
"path": "git.autistici.org/ai3/go-common",
"revision": "0a3d704ccb8c5b1ef9497ef44d1d1ce719dec459",
"revisionTime": "2017-11-27T08:45:52Z"
"revision": "8be9030703cfaa52f8fee2ace5754599092cb252",
"revisionTime": "2017-12-07T11:12:28Z"
},
{
"checksumSHA1": "xg1s3oV24wFVkOZgP03xbsY/GiI=",
"checksumSHA1": "D8Sp0BhEzo+0mvEHnWlAN8ysu8A=",
"path": "git.autistici.org/ai3/go-common/clientutil",
"revision": "0a3d704ccb8c5b1ef9497ef44d1d1ce719dec459",
"revisionTime": "2017-11-27T08:45:52Z"
"revision": "8be9030703cfaa52f8fee2ace5754599092cb252",
"revisionTime": "2017-12-07T11:12:28Z"
},
{
"checksumSHA1": "1ShDcC1XB0eIR8xpq8Yr10GYybM=",
"checksumSHA1": "YK7DpeQUCc8mgBWeGkB4+F23O5Q=",
"path": "git.autistici.org/ai3/go-common/serverutil",
"revision": "0a3d704ccb8c5b1ef9497ef44d1d1ce719dec459",
"revisionTime": "2017-11-27T08:45:52Z"
"revision": "8be9030703cfaa52f8fee2ace5754599092cb252",
"revisionTime": "2017-12-07T11:12:28Z"
},
{
"checksumSHA1": "hJvRJwSx9aZUKF26o/gOmgUJSsE=",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment