Skip to content

Commit

Permalink
CASMHMS-6294: Checkpointing work
Browse files Browse the repository at this point in the history
  • Loading branch information
jwlv committed Dec 10, 2024
1 parent 1ec8df4 commit 14524df
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 15 deletions.
19 changes: 16 additions & 3 deletions cmd/smd/job-types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// (C) Copyright [2018-2023] Hewlett Packard Enterprise Development LP
// (C) Copyright [2018-2024] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand All @@ -27,18 +27,30 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/hashicorp/go-retryablehttp"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"time"

"github.com/hashicorp/go-retryablehttp"

base "github.com/Cray-HPE/hms-base/v2"
"github.com/Cray-HPE/hms-smd/v2/pkg/sm"
)

// Response bodies should always be drained and closed, else we leak resources
// and fail to reuse network connections.
// TODO: This should be moved into hms-base
func DrainAndCloseResponseBody(resp *http.Response) {
if resp != nil && resp.Body != nil {
_, _ = io.Copy(io.Discard, resp.Body) // ok even if already drained
resp.Body.Close() // ok even if already closed
}
}

///////////////////////////////////////////////////////////////////////////////
// Job definitions
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -195,12 +207,13 @@ func (j *JobSCN) Run() {
}
rsp, err := client.Do(newRequest)
if err != nil {
DrainAndCloseResponseBody(rsp)
j.s.LogAlways("WARNING: SCN POST failed for %s: %v", urlStr, err)
} else {
if rsp.Body != nil {
strbody, _ = ioutil.ReadAll(rsp.Body)
rsp.Body.Close()
}
DrainAndCloseResponseBody(rsp)
if rsp.StatusCode != 200 {
j.s.LogAlways("WARNING: An error occurred uploading SCN to %s: %s %s", urlStr, rsp.Status, string(strbody))
} else {
Expand Down
3 changes: 3 additions & 0 deletions cmd/smd/rfevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ func TestDoHandleRFEvent(t *testing.T) {

func DummyHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(404)
DrainAndCloseRequestBody(req)
return
}

Expand All @@ -750,6 +751,8 @@ func DummyHandler(w http.ResponseWriter, req *http.Request) {
//////////////////////////////////////////////////////////////////////////////

func GigabyteHandler(w http.ResponseWriter, req *http.Request) {
defer DrainAndCloseRequestBody(req)

// Test request parameters
switch req.URL.RequestURI() {

Expand Down
Loading

0 comments on commit 14524df

Please sign in to comment.