github.com/polarismesh/polaris@v1.17.8/cache/default.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package cache 19 20 import ( 21 "context" 22 "errors" 23 "sync" 24 25 types "github.com/polarismesh/polaris/cache/api" 26 cacheauth "github.com/polarismesh/polaris/cache/auth" 27 cacheclient "github.com/polarismesh/polaris/cache/client" 28 cacheconfig "github.com/polarismesh/polaris/cache/config" 29 cachens "github.com/polarismesh/polaris/cache/namespace" 30 cachesvc "github.com/polarismesh/polaris/cache/service" 31 "github.com/polarismesh/polaris/store" 32 ) 33 34 func init() { 35 RegisterCache(types.NamespaceName, types.CacheNamespace) 36 RegisterCache(types.ServiceName, types.CacheService) 37 RegisterCache(types.InstanceName, types.CacheInstance) 38 RegisterCache(types.RoutingConfigName, types.CacheRoutingConfig) 39 RegisterCache(types.RateLimitConfigName, types.CacheRateLimit) 40 RegisterCache(types.FaultDetectRuleName, types.CacheFaultDetector) 41 RegisterCache(types.CircuitBreakerName, types.CacheCircuitBreaker) 42 RegisterCache(types.L5Name, types.CacheCL5) 43 RegisterCache(types.ConfigFileCacheName, types.CacheConfigFile) 44 RegisterCache(types.ConfigGroupCacheName, types.CacheConfigGroup) 45 RegisterCache(types.UsersName, types.CacheUser) 46 RegisterCache(types.StrategyRuleName, types.CacheAuthStrategy) 47 RegisterCache(types.ClientName, types.CacheClient) 48 } 49 50 var ( 51 cacheMgn *CacheManager 52 once sync.Once 53 finishInit bool 54 ) 55 56 // Initialize 初始化 57 func Initialize(ctx context.Context, cacheOpt *Config, storage store.Store) error { 58 var err error 59 once.Do(func() { 60 err = initialize(ctx, cacheOpt, storage) 61 }) 62 63 if err != nil { 64 return err 65 } 66 67 finishInit = true 68 return nil 69 } 70 71 // initialize cache 初始化 72 func initialize(ctx context.Context, cacheOpt *Config, storage store.Store) error { 73 if !cacheOpt.Open { 74 return nil 75 } 76 77 var err error 78 cacheMgn, err = newCacheManager(ctx, cacheOpt, storage) 79 return err 80 } 81 82 func newCacheManager(ctx context.Context, cacheOpt *Config, storage store.Store) (*CacheManager, error) { 83 SetCacheConfig(cacheOpt) 84 mgr := &CacheManager{ 85 storage: storage, 86 caches: make([]types.Cache, types.CacheLast), 87 } 88 89 // 命名空间缓存 90 mgr.RegisterCacher(types.CacheNamespace, cachens.NewNamespaceCache(storage, mgr)) 91 // 注册发现 & 服务治理缓存 92 mgr.RegisterCacher(types.CacheService, cachesvc.NewServiceCache(storage, mgr)) 93 mgr.RegisterCacher(types.CacheInstance, cachesvc.NewInstanceCache(storage, mgr)) 94 mgr.RegisterCacher(types.CacheRoutingConfig, cachesvc.NewRoutingConfigCache(storage, mgr)) 95 mgr.RegisterCacher(types.CacheRateLimit, cachesvc.NewRateLimitCache(storage, mgr)) 96 mgr.RegisterCacher(types.CacheCircuitBreaker, cachesvc.NewCircuitBreakerCache(storage, mgr)) 97 mgr.RegisterCacher(types.CacheFaultDetector, cachesvc.NewFaultDetectCache(storage, mgr)) 98 mgr.RegisterCacher(types.CacheCL5, cachesvc.NewL5Cache(storage, mgr)) 99 // 配置分组 & 配置发布缓存 100 mgr.RegisterCacher(types.CacheConfigFile, cacheconfig.NewConfigFileCache(storage, mgr)) 101 mgr.RegisterCacher(types.CacheConfigGroup, cacheconfig.NewConfigGroupCache(storage, mgr)) 102 // 用户/用户组 & 鉴权规则缓存 103 mgr.RegisterCacher(types.CacheUser, cacheauth.NewUserCache(storage, mgr)) 104 mgr.RegisterCacher(types.CacheAuthStrategy, cacheauth.NewStrategyCache(storage, mgr)) 105 // 北极星SDK Client 106 mgr.RegisterCacher(types.CacheClient, cacheclient.NewClientCache(storage, mgr)) 107 108 if len(mgr.caches) != int(types.CacheLast) { 109 return nil, errors.New("some Cache implement not loaded into CacheManager") 110 } 111 112 if err := mgr.Initialize(); err != nil { 113 return nil, err 114 } 115 return mgr, nil 116 } 117 118 func Run(cacheMgr *CacheManager, ctx context.Context) error { 119 if startErr := cacheMgr.Start(ctx); startErr != nil { 120 log.Errorf("[Cache][Server] start cache err: %s", startErr.Error()) 121 return startErr 122 } 123 124 return nil 125 } 126 127 // GetCacheManager 128 func GetCacheManager() (*CacheManager, error) { 129 if !finishInit { 130 return nil, errors.New("cache has not done Initialize") 131 } 132 return cacheMgn, nil 133 }