Skip to content

Commit

Permalink
Merge pull request #26 from omec-project/dev-sdcore-641
Browse files Browse the repository at this point in the history
SDCORE-641 simapp stuck at subscriber add and not sending slice info …
  • Loading branch information
thakurajayL authored Nov 29, 2021
2 parents a6841f5 + 0f8f630 commit 329c82d
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions simapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main

import (
"bytes"
"context"
"crypto/sha1"
"crypto/tls"
"encoding/hex"
Expand Down Expand Up @@ -314,9 +315,13 @@ func getNextBackoffInterval(retry, interval uint) uint {
func sendHttpReqMsg(req *http.Request) (*http.Response, error) {
//Keep sending request to Http server until response is success
var retries uint = 0
body, _ := ioutil.ReadAll(req.Body)
for {
rsp, err := client.Do(req)
retries = +1
cloneReq := req.Clone(context.Background())
req.Body = ioutil.NopCloser(bytes.NewReader(body))
cloneReq.Body = ioutil.NopCloser(bytes.NewReader(body))
rsp, err := client.Do(cloneReq)
retries += 1
if err != nil {
nextInterval := getNextBackoffInterval(retries, 2)
log.Printf("http req send error [%v], retrying after %v sec...", err.Error(), nextInterval)
Expand All @@ -328,10 +333,12 @@ func sendHttpReqMsg(req *http.Request) (*http.Response, error) {
rsp.StatusCode == http.StatusOK || rsp.StatusCode == http.StatusNoContent ||
rsp.StatusCode == http.StatusCreated {
log.Println("config push success")
req.Body.Close()
return rsp, nil
} else {
nextInterval := getNextBackoffInterval(retries, 2)
log.Printf("http rsp error [%v], retrying after [%v] sec...", http.StatusText(rsp.StatusCode), nextInterval)
rsp.Body.Close()
time.Sleep(time.Second * time.Duration(nextInterval))
}
}
Expand Down Expand Up @@ -367,7 +374,8 @@ func sendMessage(msgChan chan configMessage, subProvisionEndpt SubProvisionEndpt
case subscriber:
httpend = subscriberHttpend + msg.name
}

var rsp *http.Response
var httpErr error
for {
if msg.msgOp == add_op {
log.Printf("Post Message [%v] to %v", msg.String(), httpend)
Expand All @@ -379,20 +387,12 @@ func sendMessage(msgChan chan configMessage, subProvisionEndpt SubProvisionEndpt
}

req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := sendHttpReqMsg(req)
if err != nil {
rsp, httpErr = sendHttpReqMsg(req)
if httpErr != nil {
log.Printf("Post Message [%v] returned error [%v] ", httpend, err.Error())
}

defer resp.Body.Close()
//Read the response body
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
time.Sleep(1 * time.Second)
continue
}
fmt.Printf("Message Post %v Success\n", httpend)
fmt.Printf("Message POST %v Success\n", rsp.StatusCode)
} else if msg.msgOp == modify_op {
log.Printf("Put Message [%v] to %v", msg.String(), httpend)

Expand All @@ -405,12 +405,12 @@ func sendMessage(msgChan chan configMessage, subProvisionEndpt SubProvisionEndpt
}
// set the request header Content-Type for json
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := sendHttpReqMsg(req)
if err != nil {
rsp, httpErr = sendHttpReqMsg(req)
if httpErr != nil {
log.Printf("Put Message [%v] returned error [%v] ", httpend, err.Error())
}

fmt.Printf("Message PUT %v Success\n", resp.StatusCode)
fmt.Printf("Message PUT %v Success\n", rsp.StatusCode)
} else if msg.msgOp == delete_op {
log.Printf("Delete Message [%v] to %v", msg.String(), httpend)

Expand All @@ -423,12 +423,13 @@ func sendMessage(msgChan chan configMessage, subProvisionEndpt SubProvisionEndpt
}
// set the request header Content-Type for json
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := sendHttpReqMsg(req)
if err != nil {
rsp, httpErr = sendHttpReqMsg(req)
if httpErr != nil {
log.Printf("Delete Message [%v] returned error [%v] ", httpend, err.Error())
}
fmt.Printf("Message DEL %v Success\n", resp.StatusCode)
fmt.Printf("Message DEL %v Success\n", rsp.StatusCode)
}
rsp.Body.Close()
break
}
}
Expand Down

0 comments on commit 329c82d

Please sign in to comment.