Skip to content

Commit

Permalink
implement contexts for handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-p-nikolov committed Sep 30, 2019
1 parent 5ed9bfb commit f8e34ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
16 changes: 11 additions & 5 deletions concept/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@ import (
)

type AggregateConceptHandler struct {
svc Service
svc Service
requestTimeout time.Duration
}

type httpClient interface {
Do(req *http.Request) (resp *http.Response, err error)
}

func NewHandler(svc Service) AggregateConceptHandler {
return AggregateConceptHandler{svc: svc}
func NewHandler(svc Service, timeout time.Duration) AggregateConceptHandler {
return AggregateConceptHandler{svc: svc, requestTimeout: timeout}
}

func (h *AggregateConceptHandler) GetHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
UUID := vars["uuid"]
w.Header().Set("Content-Type", "application/json")
ctx, cancel := context.WithTimeout(r.Context(), h.requestTimeout)
defer cancel()

concordedConcept, transactionID, err := h.svc.GetConcordedConcept(context.TODO(), UUID, "")
concordedConcept, transactionID, err := h.svc.GetConcordedConcept(ctx, UUID, "")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, fmt.Sprintf("{\"message\": \"%s\"}", err.Error()))
Expand All @@ -54,7 +57,10 @@ func (h *AggregateConceptHandler) SendHandler(w http.ResponseWriter, r *http.Req
UUID := vars["uuid"]
w.Header().Set("Content-Type", "application/json")

err := h.svc.ProcessMessage(context.TODO(), UUID, "")
ctx, cancel := context.WithTimeout(r.Context(), h.requestTimeout)
defer cancel()

err := h.svc.ProcessMessage(ctx, UUID, "")

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
3 changes: 2 additions & 1 deletion concept/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"sync"

Expand Down Expand Up @@ -124,7 +125,7 @@ func TestHandlers(t *testing.T) {
t.Run(d.name, func(t *testing.T) {
fb := make(chan bool)
mockService := NewMockService(d.concepts, d.notifications, d.healthchecks, d.err)
handler := NewHandler(mockService)
handler := NewHandler(mockService, time.Second*1)
m := mux.NewRouter()
handler.RegisterHandlers(m)
handler.RegisterAdminHandlers(m, NewHealthService(mockService, "system-code", "app-name", 8080, "description"), true, fb)
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ func main() {
Desc: "Duration(seconds) that messages will be ignored by subsequent requests after initial response",
EnvVar: "VISIBILITY_TIMEOUT",
})
httpTimeout := app.Int(cli.IntOpt{
Name: "http-timeout",
Value: 8,
Desc: "Duration(seconds) to wait before timing out a request",
EnvVar: "HTTP_TIMEOUT",
})
waitTime := app.Int(cli.IntOpt{
Name: "waitTime",
Value: 20,
Expand Down Expand Up @@ -240,7 +246,7 @@ func main() {
feedback,
done)

handler := concept.NewHandler(svc)
handler := concept.NewHandler(svc, time.Second*time.Duration(*httpTimeout))
hs := concept.NewHealthService(svc, *appSystemCode, *appName, *port, appDescription)

router := mux.NewRouter()
Expand Down

0 comments on commit f8e34ec

Please sign in to comment.