Skip to content

Commit

Permalink
[FEATURE] status check working with horrible memory consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
xf0e committed Dec 14, 2018
1 parent 47a244e commit 0fd71d9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
4 changes: 1 addition & 3 deletions ocr_http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ func (s *OcrHttpHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
http.Error(w, errMsg, 500)
return
}

// log the whole result
// logg.LogTo("OCR_HTTP", "ocrResult: %v", ocrResult)

// fmt.Fprintf(w, ocrResult.Text)
w.Header().Set("Content-Type", "application/json")
js, err := json.Marshal(ocrResult)
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions ocr_http_status_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package ocrworker
import (
"encoding/json"
"fmt"
"github.com/couchbaselabs/logg"
"net/http"

"github.com/couchbaselabs/logg"
)

type OcrHttpStatusHandler struct {
Expand All @@ -15,29 +16,30 @@ func NewOcrHttpStatusHandler() *OcrHttpStatusHandler {
}

func (s *OcrHttpStatusHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

logg.LogTo("OCR_HTTP", "serveHttp called")
defer req.Body.Close()

OcrRequest := OcrRequest{}
ocrRequest := OcrRequest{}
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&OcrRequest)
err := decoder.Decode(&ocrRequest)
if err != nil {
logg.LogError(err)
http.Error(w, "unable to unmalrshal json", 500)
http.Error(w, "unable to unmarshal json", 500)
return
}

ocrResult, err := CheckOcrStatusById(OcrRequest.ImgUrl)
ocrResult, err := CheckOcrStatusById(ocrRequest.ImgUrl)

if err != nil {
msg := "unable to perform OCR status check. Error: %v"
msg := "unable to perform OCR status check. Error: %v"
errMsg := fmt.Sprintf(msg, err)
logg.LogError(fmt.Errorf(errMsg))
http.Error(w, errMsg, 500)
return
}

logg.LogTo("OCR_HTTP", "ocrResult: %v", ocrResult)
// logg.LogTo("OCR_HTTP", "ocrResult: %v", ocrResult)
w.Header().Set("Content-Type", "application/json")
js, err := json.Marshal(ocrResult)
if err != nil {
Expand Down
24 changes: 9 additions & 15 deletions ocr_rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

const (
RPC_RESPONSE_TIMEOUT = time.Minute * 5
RESPONE_CACHE_TIMEOUT = time.Minute * 120
RpcResponseTimeout = time.Minute * 1
ResponseCacheTimeout = time.Minute * 2
)

type OcrRpcClient struct {
Expand All @@ -25,7 +25,6 @@ type OcrResult struct {
Status string `json:"status"`
}

// var requests map[string]chan OcrResult = make(map[string]chan OcrResult)
var requests = make(map[string]chan OcrResult)
var timers = make(map[string]*time.Timer)

Expand All @@ -50,7 +49,9 @@ func (c *OcrRpcClient) DecodeImage(ocrRequest OcrRequest) (OcrResult, error) {
if err != nil {
return OcrResult{}, err
}
defer c.connection.Close()
// if we close the connection here, the deferred status wont get the ocr result
// and will be always returning "processing"
// defer c.connection.Close()

c.channel, err = c.connection.Channel()
if err != nil {
Expand Down Expand Up @@ -144,17 +145,10 @@ func (c *OcrRpcClient) DecodeImage(ocrRequest OcrRequest) (OcrResult, error) {
return OcrResult{}, nil
}

/* select {
case ocrResult := <-rpcResponseChan:
return ocrResult, nil
case <-time.After(RPC_RESPONSE_TIMEOUT):
return OcrResult{}, fmt.Errorf("Timeout waiting for RPC response")
} */

if ocrRequest.Deferred {
logg.LogTo("OCR_CLIENT", "Distributed request")
requestId, _ := uuid.NewV4()
timer := time.NewTimer(RESPONE_CACHE_TIMEOUT)
timer := time.NewTimer(ResponseCacheTimeout)
requests[requestId.String()] = rpcResponseChan
timers[requestId.String()] = timer
go func() {
Expand All @@ -165,7 +159,7 @@ func (c *OcrRpcClient) DecodeImage(ocrRequest OcrRequest) (OcrResult, error) {
Text: requestId.String(),
}, nil
} else {
return CheckReply(rpcResponseChan, RPC_RESPONSE_TIMEOUT)
return CheckReply(rpcResponseChan, RpcResponseTimeout)
}
}

Expand Down Expand Up @@ -262,12 +256,12 @@ func CheckOcrStatusById(requestId string) (OcrResult, error) {
}

func CheckReply(rpcResponseChan chan OcrResult, timeout time.Duration) (OcrResult, error) {
logg.LogTo("OCR_CLIENT", "checking for response")
logg.LogTo("OCR_CLIENT", "Checking for response")
select {
case ocrResult := <-rpcResponseChan:
return ocrResult, nil
case <-time.After(timeout):
return OcrResult{Text: "timeout waiting for RPC response", Status: "processing"}, nil
return OcrResult{Text: "Timeout waiting for RPC response", Status: "processing"}, nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion ocr_rpc_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func confirmDeliveryWorker(ack, nack chan uint64) {
logg.LogTo("OCR_WORKER", "confirmed delivery, tag: %v", tag)
case tag := <-nack:
logg.LogTo("OCR_WORKER", "failed to confirm delivery: %v", tag)
case <-time.After(RPC_RESPONSE_TIMEOUT):
case <-time.After(RpcResponseTimeout):
// this is bad, the worker will probably be dysfunctional
// at this point, so panic
logg.LogPanic("timeout trying to confirm delivery")
Expand Down

0 comments on commit 0fd71d9

Please sign in to comment.