go.temporal.io/server@v1.23.0/common/rpc/interceptor/sdk_version.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2022 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package interceptor 26 27 import ( 28 "context" 29 "sync" 30 31 "go.temporal.io/version/check" 32 "google.golang.org/grpc" 33 34 "go.temporal.io/server/common/headers" 35 ) 36 37 type SDKVersionInterceptor struct { 38 sync.RWMutex 39 sdkInfoSet map[check.SDKInfo]struct{} 40 versionChecker headers.VersionChecker 41 maxSetSize int 42 } 43 44 const defaultMaxSetSize = 100 45 46 // NewSDKVersionInterceptor creates a new SDKVersionInterceptor with default max set size 47 func NewSDKVersionInterceptor() *SDKVersionInterceptor { 48 return &SDKVersionInterceptor{ 49 sdkInfoSet: make(map[check.SDKInfo]struct{}), 50 versionChecker: headers.NewDefaultVersionChecker(), 51 maxSetSize: defaultMaxSetSize, 52 } 53 } 54 55 // Intercept a grpc request 56 func (vi *SDKVersionInterceptor) Intercept( 57 ctx context.Context, 58 req interface{}, 59 info *grpc.UnaryServerInfo, 60 handler grpc.UnaryHandler, 61 ) (interface{}, error) { 62 sdkName, sdkVersion := headers.GetClientNameAndVersion(ctx) 63 if sdkName != "" && sdkVersion != "" { 64 vi.RecordSDKInfo(sdkName, sdkVersion) 65 if err := vi.versionChecker.ClientSupported(ctx); err != nil { 66 return nil, err 67 } 68 } 69 return handler(ctx, req) 70 } 71 72 // RecordSDKInfo records name and version tuple in memory 73 func (vi *SDKVersionInterceptor) RecordSDKInfo(name, version string) { 74 info := check.SDKInfo{Name: name, Version: version} 75 76 vi.RLock() 77 overCap := len(vi.sdkInfoSet) >= vi.maxSetSize 78 _, found := vi.sdkInfoSet[info] 79 vi.RUnlock() 80 81 if !overCap && !found { 82 vi.Lock() 83 vi.sdkInfoSet[info] = struct{}{} 84 vi.Unlock() 85 } 86 } 87 88 // GetAndResetSDKInfo gets all recorded name, version tuples and resets internal records 89 func (vi *SDKVersionInterceptor) GetAndResetSDKInfo() []check.SDKInfo { 90 vi.Lock() 91 currSet := vi.sdkInfoSet 92 vi.sdkInfoSet = make(map[check.SDKInfo]struct{}) 93 vi.Unlock() 94 95 sdkInfo := make([]check.SDKInfo, 0, len(currSet)) 96 for k := range currSet { 97 sdkInfo = append(sdkInfo, k) 98 } 99 return sdkInfo 100 }