Skip to content
Snippets Groups Projects
Commit 4a4d4a2b authored by Francé Wilke's avatar Francé Wilke
Browse files

OpenSearch - let's close some bodies

parent 4585632c
No related branches found
No related tags found
No related merge requests found
......@@ -107,6 +107,11 @@ func (ds *DocumentStore) Write(id string, data interface{}) error {
indexName, // index name
indexName, // document id
strings.NewReader(string(ds.jsonSettings)))
defer func() {
if res != nil {
res.Body.Close()
}
}()
if err != nil {
return errors.Wrapf(err, "failed to create index(%s)", indexName)
}
......
......@@ -37,8 +37,11 @@ type TimeSeries struct {
}
// NewTimeSeries purpose:
//
// create a time series to write e.g. api api_logs
//
// parameters:
//
// name must be the openSearch index name prefix without the date, e.g. "uafrica-v3-api-api_logs"
// the actual indices in openSearch will be called "<indexName>-<ccyymmdd>" e.g. "uafrica-v3-api-api_logs-20210102"
// tmpl must be your log data struct consisting of public fields as:
......@@ -225,6 +228,11 @@ func (ts *TimeSeries) Write(startTime, endTime time.Time, data interface{}) erro
indexName, // index name also used for document id
strings.NewReader(string(ts.jsonSettings)),
)
defer func() {
if res != nil {
res.Body.Close()
}
}()
if err != nil {
return errors.Wrapf(err, "failed to create index(%s)", indexName)
}
......@@ -271,8 +279,11 @@ func (ts *TimeSeries) Write(startTime, endTime time.Time, data interface{}) erro
}
// DelOldTimeSeries parameters:
//
// indexName is index prefix before dash-date, e.g. "api-api_logs" then will look for "api-api_logs-<date>"
//
// returns
//
// list of indices to delete with err==nil if deleted successfully
func (ts *TimeSeries) DelOldTimeSeries(olderThanDays int) ([]string, error) {
if olderThanDays < 0 {
......@@ -349,6 +360,7 @@ type IndexSettings struct {
// Search returns docs indexed on OpenSearch document ID which cat be used in Get(id)
// The docs value type is the same as that of tmpl specified when you created the TimeSeries(..., tmpl)
// So you can safely type assert e.g.
//
// type myType struct {...}
// ts := search.TimeSeries(..., myType{})
// docs,totalCount,err := ts.Search(...)
......
......@@ -76,6 +76,11 @@ func (writer Writer) Write(indexName string, id string, doc interface{}) (*Index
strings.NewReader(jsonDocStr),
options...,
)
defer func() {
if indexResponse != nil {
indexResponse.Body.Close()
}
}()
if err != nil {
return nil, errors.Wrapf(err, "failed to index document")
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment