File: zc_policy_unique_request_id.go

package info (click to toggle)
golang-github-azure-azure-storage-blob-go 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,084 kB
  • sloc: makefile: 3
file content (36 lines) | stat: -rw-r--r-- 1,110 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package azblob

import (
	"context"
	"errors"

	"github.com/Azure/azure-pipeline-go/pipeline"
)

// NewUniqueRequestIDPolicyFactory creates a UniqueRequestIDPolicyFactory object
// that sets the request's x-ms-client-request-id header if it doesn't already exist.
func NewUniqueRequestIDPolicyFactory() pipeline.Factory {
	return pipeline.FactoryFunc(func(next pipeline.Policy, po *pipeline.PolicyOptions) pipeline.PolicyFunc {
		// This is Policy's Do method:
		return func(ctx context.Context, request pipeline.Request) (pipeline.Response, error) {
			id := request.Header.Get(xMsClientRequestID)
			if id == "" { // Add a unique request ID if the caller didn't specify one already
				id = newUUID().String()
				request.Header.Set(xMsClientRequestID, id)
			}

			resp, err := next.Do(ctx, request)

			if err == nil && resp != nil {
				crId := resp.Response().Header.Get(xMsClientRequestID)
				if crId != "" && crId != id {
					err = errors.New("client Request ID from request and response does not match")
				}
			}

			return resp, err
		}
	})
}

const xMsClientRequestID = "x-ms-client-request-id"