diff --git a/ocr_http_handler.go b/ocr_http_handler.go index 7aeac3b..f8ad065 100644 --- a/ocr_http_handler.go +++ b/ocr_http_handler.go @@ -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 { diff --git a/ocr_http_status_handler.go b/ocr_http_status_handler.go index 2adcf43..19c578f 100644 --- a/ocr_http_status_handler.go +++ b/ocr_http_status_handler.go @@ -3,8 +3,9 @@ package ocrworker import ( "encoding/json" "fmt" - "github.com/couchbaselabs/logg" "net/http" + + "github.com/couchbaselabs/logg" ) type OcrHttpStatusHandler struct { @@ -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 { diff --git a/ocr_rpc_client.go b/ocr_rpc_client.go index eba0d46..143b410 100644 --- a/ocr_rpc_client.go +++ b/ocr_rpc_client.go @@ -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 { @@ -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) @@ -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 { @@ -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() { @@ -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) } } @@ -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 } } diff --git a/ocr_rpc_worker.go b/ocr_rpc_worker.go index fcf2c82..2969a61 100644 --- a/ocr_rpc_worker.go +++ b/ocr_rpc_worker.go @@ -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")