File: server_interceptors_options.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,574 bytes parent folder | download | duplicates (3)
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
37
38
39
40
package grpccorrelation

// The configuration for server correlation interceptors.
type serverInterceptConfig struct {
	propagateIncomingCorrelationID bool
	reversePropagateCorrelationID  bool
}

// ServerCorrelationInterceptorOption configures server correlation interceptor.
type ServerCorrelationInterceptorOption func(*serverInterceptConfig)

func applyServerCorrelationInterceptorOptions(opts []ServerCorrelationInterceptorOption) serverInterceptConfig {
	config := serverInterceptConfig{
		propagateIncomingCorrelationID: true, // enabled by default
	}
	for _, v := range opts {
		v(&config)
	}

	return config
}

// WithoutPropagation disables correlation id propagation from incoming request metadata.
// If the id is missing or the interceptor is configured to not propagate it, a new id is generated and
// injected into the request context.
func WithoutPropagation() ServerCorrelationInterceptorOption {
	return func(config *serverInterceptConfig) {
		config.propagateIncomingCorrelationID = false
	}
}

// WithReversePropagation enables server -> client correlation id propagation via response metadata.
// Client can then use the returned correlation id e.g. for logging purposes.
// It only makes sense to use this option together with WithoutPropagation i.e. in situations, when
// client-supplied correlation id is not trusted so server generates its own one and hence clients doesn't have it.
func WithReversePropagation() ServerCorrelationInterceptorOption {
	return func(config *serverInterceptConfig) {
		config.reversePropagateCorrelationID = true
	}
}