From 2bd110bc4df810d1a9f84c2f0a92da9b1eb63faf Mon Sep 17 00:00:00 2001 From: PennyYoon <55304072+Chenxulin97@users.noreply.github.com> Date: Sun, 7 Jan 2024 10:20:45 +0800 Subject: [PATCH] refactor: extract newBuffer and innerWrite method (#32) Signed-off-by: PennyYoon <525296438@qq.com> Co-authored-by: ZhangJian He --- opengemini/write.go | 62 +++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/opengemini/write.go b/opengemini/write.go index 7e016ec..d8fb790 100644 --- a/opengemini/write.go +++ b/opengemini/write.go @@ -11,13 +11,8 @@ import ( func (c *client) WriteBatchPoints(database string, bp *BatchPoints) error { var buffer bytes.Buffer - var writer io.Writer + writer := c.newBuffer(&buffer) - if c.config.GzipEnabled { - writer = gzip.NewWriter(&buffer) - } else { - writer = &buffer - } for _, p := range bp.Points { if p == nil { continue @@ -35,17 +30,7 @@ func (c *client) WriteBatchPoints(database string, bp *BatchPoints) error { } } - req := requestDetails{ - queryValues: make(url.Values), - body: &buffer, - } - if c.config.GzipEnabled { - req.header = make(http.Header) - req.header.Set("Content-Encoding", "gzip") - req.header.Set("Accept-Encoding", "gzip") - } - req.queryValues.Add("db", database) - resp, err := c.executeHttpPost(UrlWrite, req) + resp, err := c.innerWrite(database, &buffer) if err != nil { return err } @@ -63,14 +48,7 @@ func (c *client) WriteBatchPoints(database string, bp *BatchPoints) error { func (c *client) WritePoint(database string, point *Point, callbackFunc func(error)) error { var buffer bytes.Buffer - - var writer io.Writer - - if c.config.GzipEnabled { - writer = gzip.NewWriter(&buffer) - } else { - writer = &buffer - } + writer := c.newBuffer(&buffer) if _, err := io.WriteString(writer, point.String()); err != nil { return err @@ -82,17 +60,7 @@ func (c *client) WritePoint(database string, point *Point, callbackFunc func(err } } - req := requestDetails{ - queryValues: make(url.Values), - body: &buffer, - } - req.queryValues.Add("db", database) - resp, err := c.executeHttpPost(UrlWrite, req) - if c.config.GzipEnabled { - req.header = make(http.Header) - req.header.Set("Content-Encoding", "gzip") - req.header.Set("Accept-Encoding", "gzip") - } + resp, err := c.innerWrite(database, &buffer) if err != nil { callbackFunc(err) } else if resp.StatusCode != http.StatusNoContent { @@ -108,3 +76,25 @@ func (c *client) WritePoint(database string, point *Point, callbackFunc func(err } return nil } + +func (c *client) newBuffer(buffer *bytes.Buffer) io.Writer { + if c.config.GzipEnabled { + return gzip.NewWriter(buffer) + } else { + return buffer + } +} + +func (c *client) innerWrite(database string, buffer *bytes.Buffer) (*http.Response, error) { + req := requestDetails{ + queryValues: make(url.Values), + body: buffer, + } + if c.config.GzipEnabled { + req.header = make(http.Header) + req.header.Set("Content-Encoding", "gzip") + req.header.Set("Accept-Encoding", "gzip") + } + req.queryValues.Add("db", database) + return c.executeHttpPost(UrlWrite, req) +}