go.temporal.io/server@v1.23.0/common/client_cache.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 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 common 26 27 import ( 28 "sync" 29 ) 30 31 type ( 32 // ClientCache store initialized clients 33 ClientCache interface { 34 GetClientForKey(key string) (interface{}, error) 35 GetClientForClientKey(clientKey string) (interface{}, error) 36 GetAllClients() ([]interface{}, error) 37 } 38 39 keyResolver interface { 40 Lookup(key string) (string, error) 41 GetAllAddresses() ([]string, error) 42 } 43 44 clientProvider func(string) (interface{}, error) 45 46 clientCacheImpl struct { 47 keyResolver keyResolver 48 clientProvider clientProvider 49 50 cacheLock sync.RWMutex 51 clients map[string]interface{} 52 } 53 ) 54 55 // NewClientCache creates a new client cache based on membership 56 func NewClientCache( 57 keyResolver keyResolver, 58 clientProvider clientProvider, 59 ) ClientCache { 60 61 return &clientCacheImpl{ 62 keyResolver: keyResolver, 63 clientProvider: clientProvider, 64 65 clients: make(map[string]interface{}), 66 } 67 } 68 69 func (c *clientCacheImpl) GetClientForKey(key string) (interface{}, error) { 70 clientKey, err := c.keyResolver.Lookup(key) 71 if err != nil { 72 return nil, err 73 } 74 75 return c.GetClientForClientKey(clientKey) 76 } 77 78 func (c *clientCacheImpl) GetClientForClientKey(clientKey string) (interface{}, error) { 79 c.cacheLock.RLock() 80 client, ok := c.clients[clientKey] 81 c.cacheLock.RUnlock() 82 if ok { 83 return client, nil 84 } 85 86 c.cacheLock.Lock() 87 defer c.cacheLock.Unlock() 88 89 client, ok = c.clients[clientKey] 90 if ok { 91 return client, nil 92 } 93 94 client, err := c.clientProvider(clientKey) 95 if err != nil { 96 return nil, err 97 } 98 c.clients[clientKey] = client 99 return client, nil 100 } 101 102 func (c *clientCacheImpl) GetAllClients() ([]interface{}, error) { 103 var result []interface{} 104 allAddresses, err := c.keyResolver.GetAllAddresses() 105 if err != nil { 106 return nil, err 107 } 108 for _, addr := range allAddresses { 109 client, err := c.GetClientForClientKey(addr) 110 if err != nil { 111 return nil, err 112 } 113 result = append(result, client) 114 } 115 116 return result, nil 117 }