Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request context propagation #593

Closed
seungbemi opened this issue Jul 15, 2022 · 3 comments
Closed

Request context propagation #593

seungbemi opened this issue Jul 15, 2022 · 3 comments
Assignees
Labels

Comments

@seungbemi
Copy link

Describe the bug
The HTTP request context is not propagated to the proxy level, which leads not to getting an HTTP cancel signal into the client plugin as well as any stored context values from the handler plugins.

To Reproduce
Steps to reproduce the behavior:

  1. Create a client plugin with the following the handler
func registerClients(ctx context.Context, extra map[string]interface{}) (http.Handler, error) {
    return http.HandlerFunc(
  	  func(w http.ResponseWriter, req *http.Request) {
  		  select {
  		  case <-req.Context().Done():
  			  fmt.Println("cancel signal received")
  			  return

  		  case <-time.After(10 * time.Second):
  			  fmt.Println("timeout")
  		  }

  		  w.Header().Set("Content-Type", "application/json")
  		  w.WriteHeader(http.StatusOK)
  		  w.Write([]byte("{}"))
  	  }), nil
}
  1. Configuration with an enough timeout setting
{
    "$id": "https://www.krakend.io/schema/v3.json",
    "version": 3,
    "name": "Development gateway",
    "port": 8080,
    "cache_ttl": "3600s",
    "timeout": "20s",
    "plugin": {
      "pattern": ".so",
      "folder": "./plugins"
    },
    "extra_config": {
      "telemetry/logging": {
        "level": "DEBUG",
        "prefix": "[KRAKEND]",
        "stdout": true,
        "format": "logstash"
      }
    },
    "endpoints": [
      {
        "endpoint": "/test",
        "method": "GET",
        "backend": [
          {
            "host": [
              "http://127.0.0.1:8080"
            ],
            "extra_config": {
              "plugin/http-client": {
                "name": "client-plugin"
              }
            }
          }
        ]
      }
    ]
  }
  
  1. run using krakend-ce project
$ go run ./cmd/krakend-ce/main.go run -c krakend.json
  1. curl to the endpoint with a short timeout
$ curl -m 2 -v http://localhost:8080/test
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /test HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.79.1
> Accept: */*
>
* Operation timed out after 2004 milliseconds with 0 bytes received
* Closing connection 0
curl: (28) Operation timed out after 2004 milliseconds with 0 bytes received

Expected behavior
expect to get the context.Done to be sent by the request cancelation from the client side and see the log of cancel signal received

Logs

Parsing configuration file: krakend.json
[KRAKEND] 2022/07/15 - 11:11:35.036 ▶ DEBUG [SERVICE: Plugin Loader] Starting loading process
RegisterLogger called with: gologging.Logger{logger:(*logging.Logger)(0x14000a83ef0)}
[KRAKEND] 2022/07/15 - 11:11:35.053 ▶ INFO [SERVICE: Executor Plugin] Total plugins loaded: 1
[KRAKEND] 2022/07/15 - 11:11:35.053 ▶ DEBUG [SERVICE: Handler Plugin] plugin
[KRAKEND] 2022/07/15 - 11:11:35.053 ▶ DEBUG [SERVICE: Modifier Plugin] plugin
[KRAKEND] 2022/07/15 - 11:11:35.053 ▶ DEBUG [SERVICE: Plugin Loader] Loading process completed
[KRAKEND] 2022/07/15 - 11:11:35.054 ▶ INFO Starting the KrakenD instance
[KRAKEND] 2022/07/15 - 11:11:35.054 ▶ DEBUG [ENDPOINT: /test] Building the proxy pipe
[KRAKEND] 2022/07/15 - 11:11:35.054 ▶ DEBUG [BACKEND: /] Building the backend pipe
[KRAKEND] 2022/07/15 - 11:11:35.054 ▶ DEBUG [BACKEND: /] Injecting plugin client-plugin
[KRAKEND] 2022/07/15 - 11:11:35.054 ▶ DEBUG [ENDPOINT: /test] Building the http handler
[KRAKEND] 2022/07/15 - 11:11:35.054 ▶ DEBUG [ENDPOINT: /test][JWTSigner] Signer disabled
[KRAKEND] 2022/07/15 - 11:11:35.054 ▶ INFO [ENDPOINT: /test][JWTValidator] Validator disabled for this endpoint
[KRAKEND] 2022/07/15 - 11:11:35.054 ▶ INFO [SERVICE: Gin] Listening on port: 8080
[KRAKEND] 2022/07/15 - 11:11:40.037 ▶ DEBUG [SERVICE: Telemetry] Registering usage stats for Cluster ID
timeout
[GIN] 2022/07/15 - 11:11:52 | 200 | 10.004338666s |       127.0.0.1 | GET      "/test"

Additional context
Add any other context about the problem here.

gin.Context does not gets the request cancel signal, therefore Request.Context() inside of the gin.Context should be handed over to propagate the cancel signal and its stored values.

requestCtx, cancel := context.WithTimeout(c.Request.Context(), configuration.Timeout)

code reference

@kpacha
Copy link
Member

kpacha commented Jul 18, 2022

we hope it will get fixed once we update to gin v1.8.1. It's planed for the next release (v2.1.0)

thanks for the heads up!

@kpacha kpacha self-assigned this Jul 18, 2022
@taik0
Copy link
Contributor

taik0 commented Nov 24, 2022

Fixed in #617

@taik0 taik0 closed this as completed Nov 24, 2022
@github-actions
Copy link

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants