cubefs
1/*
2*
3* Copyright 2020 gRPC authors.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*
17*/
18
19// Package resolver provides internal resolver-related functionality.
20package resolver21
22import (23"context"24"sync"25
26"google.golang.org/grpc/internal/serviceconfig"27"google.golang.org/grpc/resolver"28)
29
30// ConfigSelector controls what configuration to use for every RPC.
31type ConfigSelector interface {32// Selects the configuration for the RPC, or terminates it using the error.33// This error will be converted by the gRPC library to a status error with34// code UNKNOWN if it is not returned as a status error.35SelectConfig(RPCInfo) (*RPCConfig, error)36}
37
38// RPCInfo contains RPC information needed by a ConfigSelector.
39type RPCInfo struct {40// Context is the user's context for the RPC and contains headers and41// application timeout. It is passed for interception purposes and for42// efficiency reasons. SelectConfig should not be blocking.43Context context.Context44Method string // i.e. "/Service/Method"45}
46
47// RPCConfig describes the configuration to use for each RPC.
48type RPCConfig struct {49// The context to use for the remainder of the RPC; can pass info to LB50// policy or affect timeout or metadata.51Context context.Context52MethodConfig serviceconfig.MethodConfig // configuration to use for this RPC53OnCommitted func() // Called when the RPC has been committed (retries no longer possible)54}
55
56type csKeyType string57
58const csKey = csKeyType("grpc.internal.resolver.configSelector")59
60// SetConfigSelector sets the config selector in state and returns the new
61// state.
62func SetConfigSelector(state resolver.State, cs ConfigSelector) resolver.State {63state.Attributes = state.Attributes.WithValues(csKey, cs)64return state65}
66
67// GetConfigSelector retrieves the config selector from state, if present, and
68// returns it or nil if absent.
69func GetConfigSelector(state resolver.State) ConfigSelector {70cs, _ := state.Attributes.Value(csKey).(ConfigSelector)71return cs72}
73
74// SafeConfigSelector allows for safe switching of ConfigSelector
75// implementations such that previous values are guaranteed to not be in use
76// when UpdateConfigSelector returns.
77type SafeConfigSelector struct {78mu sync.RWMutex79cs ConfigSelector
80}
81
82// UpdateConfigSelector swaps to the provided ConfigSelector and blocks until
83// all uses of the previous ConfigSelector have completed.
84func (scs *SafeConfigSelector) UpdateConfigSelector(cs ConfigSelector) {85scs.mu.Lock()86defer scs.mu.Unlock()87scs.cs = cs88}
89
90// SelectConfig defers to the current ConfigSelector in scs.
91func (scs *SafeConfigSelector) SelectConfig(r RPCInfo) (*RPCConfig, error) {92scs.mu.RLock()93defer scs.mu.RUnlock()94return scs.cs.SelectConfig(r)95}
96