Improvements to HTTP and graphQL bindings (#1828)
Ensure context propagation in output bindings + other fixes Spin-off from PR adding contexts to input bindings Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com> Co-authored-by: Dapr Bot <56698301+dapr-bot@users.noreply.github.com>
This commit is contained in:
parent
1010f9c7ee
commit
a58d8dddda
|
@ -52,8 +52,6 @@ type GraphQL struct {
|
|||
logger logger.Logger
|
||||
}
|
||||
|
||||
var _ = bindings.OutputBinding(&GraphQL{})
|
||||
|
||||
// NewGraphQL returns a new GraphQL binding instance.
|
||||
func NewGraphQL(logger logger.Logger) *GraphQL {
|
||||
return &GraphQL{logger: logger}
|
||||
|
@ -102,7 +100,7 @@ func (gql *GraphQL) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*b
|
|||
}
|
||||
gql.logger.Debugf("operation: %v", req.Operation)
|
||||
|
||||
startTime := time.Now().UTC()
|
||||
startTime := time.Now()
|
||||
|
||||
resp := &bindings.InvokeResponse{
|
||||
Metadata: map[string]string{
|
||||
|
@ -116,12 +114,12 @@ func (gql *GraphQL) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*b
|
|||
|
||||
switch req.Operation { // nolint: exhaustive
|
||||
case QueryOperation:
|
||||
if err := gql.runRequest(commandQuery, req, &graphqlResponse); err != nil {
|
||||
if err := gql.runRequest(ctx, commandQuery, req, &graphqlResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
case MutationOperation:
|
||||
if err := gql.runRequest(commandMutation, req, &graphqlResponse); err != nil {
|
||||
if err := gql.runRequest(ctx, commandMutation, req, &graphqlResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -137,14 +135,14 @@ func (gql *GraphQL) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*b
|
|||
|
||||
resp.Data = b
|
||||
|
||||
endTime := time.Now().UTC()
|
||||
endTime := time.Now()
|
||||
resp.Metadata[respEndTimeKey] = endTime.Format(time.RFC3339Nano)
|
||||
resp.Metadata[respDurationKey] = endTime.Sub(startTime).String()
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (gql *GraphQL) runRequest(requestKey string, req *bindings.InvokeRequest, response interface{}) error {
|
||||
func (gql *GraphQL) runRequest(ctx context.Context, requestKey string, req *bindings.InvokeRequest, response interface{}) error {
|
||||
requestString, ok := req.Metadata[requestKey]
|
||||
if !ok || requestString == "" {
|
||||
return fmt.Errorf("GraphQL Error: required %q not set", requestKey)
|
||||
|
@ -170,7 +168,7 @@ func (gql *GraphQL) runRequest(requestKey string, req *bindings.InvokeRequest, r
|
|||
}
|
||||
}
|
||||
|
||||
if err := gql.client.Run(context.Background(), request, response); err != nil {
|
||||
if err := gql.client.Run(ctx, request, response); err != nil {
|
||||
return fmt.Errorf("GraphQL Error: %w", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ func (h *HTTPSource) Init(metadata bindings.Metadata) error {
|
|||
TLSHandshakeTimeout: 5 * time.Second,
|
||||
}
|
||||
h.client = &http.Client{
|
||||
Timeout: time.Second * 10,
|
||||
Timeout: time.Second * 30,
|
||||
Transport: netTransport,
|
||||
}
|
||||
|
||||
|
@ -115,11 +115,10 @@ func (h *HTTPSource) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*
|
|||
return nil, fmt.Errorf("invalid operation: %s", req.Operation)
|
||||
}
|
||||
|
||||
request, err := http.NewRequest(method, u, body)
|
||||
request, err := http.NewRequestWithContext(ctx, method, u, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request = request.WithContext(ctx)
|
||||
|
||||
// Set default values for Content-Type and Accept headers.
|
||||
if body != nil {
|
||||
|
|
Loading…
Reference in New Issue