From e296a0d64897c2a3bc1a0ada8839d44360e2aa08 Mon Sep 17 00:00:00 2001 From: Michael Baird Date: Wed, 29 Nov 2017 15:10:11 -0600 Subject: [PATCH] Client method for the SendSync /cluster/message --- client.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/client.go b/client.go index e44d9c5de..a5fad55de 100644 --- a/client.go +++ b/client.go @@ -1044,6 +1044,41 @@ func (c *InternalHTTPClient) RowAttrDiff(ctx context.Context, index, frame strin return rsp.Attrs, nil } +// ClusterMessage posts a Gossip message synchronously. +func (c *InternalHTTPClient) ClusterMessage(ctx context.Context, pb proto.Message) error { + msg, err := MarshalMessage(pb) + if err != nil { + return err + } + + u := uriPathToURL(ctx.Value("uri").(*URI), "/cluster/message") + req, err := http.NewRequest("POST", u.String(), bytes.NewReader(msg)) + req.Header.Set("Content-Type", "application/x-protobuf") + req.Header.Set("User-Agent", "pilosa/"+Version) + + // Execute request. + resp, err := c.HTTPClient.Do(req.WithContext(ctx)) + if err != nil { + return err + } + defer resp.Body.Close() + + // Read body. + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + // Return error if status is not OK. + switch resp.StatusCode { + case http.StatusOK: // ok + default: + return errors.New(string(body)) + } + + return nil +} + func (c *InternalHTTPClient) clientURI(ctx context.Context) *URI { clientURI := c.defaultURI if contextURI, ok := ctx.Value("uri").(*URI); ok { @@ -1226,4 +1261,5 @@ type InternalClient interface { BlockData(ctx context.Context, index, frame, view string, slice uint64, block int) ([]uint64, []uint64, error) ColumnAttrDiff(ctx context.Context, index string, blks []AttrBlock) (map[uint64]map[string]interface{}, error) RowAttrDiff(ctx context.Context, index, frame string, blks []AttrBlock) (map[uint64]map[string]interface{}, error) + ClusterMessage(ctx context.Context, pb proto.Message) error }