func RegisterProvider(provider base.Provider) error { defer func() { if err := recover(); err != nil { debug.PrintStack() errorMsg := fmt.Sprintf("Occur FATAL error when register provider (provider=%v): %s", provider, err) base.Logger().Fatalln(errorMsg) } }() if provider == nil { panicMsg := "IdCenter: The provider is nil!\n" base.Logger().Fatal(panicMsg) panic(panicMsg) } name := provider.Name() if len(name) == 0 { panicMsg := "IdCenter: The name of provider is nil!\n" base.Logger().Fatal(panicMsg) panic(panicMsg) } switch t := interface{}(provider).(type) { case base.CacheProvider: if _, contains := cacheProviderMap[name]; contains { errorMsg := fmt.Sprintf("IdCenter: Repetitive cache provider name '%s'!\n", name) base.Logger().Error(errorMsg) return errors.New(errorMsg) } cp, ok := interface{}(provider).(base.CacheProvider) if !ok { errorMsg := fmt.Sprintf("IdCenter: Incorrect cache provider type! (name '%s')\n", name) base.Logger().Error(errorMsg) return errors.New(errorMsg) } cacheProviderMap[name] = cp case base.StorageProvider: if _, contains := storageProviderMap[name]; contains { errorMsg := fmt.Sprintf("IdCenter: Repetitive storage provider name '%s'!\n", name) base.Logger().Error(errorMsg) return errors.New(errorMsg) } sp, ok := interface{}(provider).(base.StorageProvider) if !ok { errorMsg := fmt.Sprintf("IdCenter: Incorrect cache provider type! (name '%s')\n", name) base.Logger().Error(errorMsg) return errors.New(errorMsg) } storageProviderMap[name] = sp default: panicMsg := fmt.Sprintf("IdCenter: Unexpected Provider type '%v'! (name=%s)\n", t, name) base.Logger().Fatal(panicMsg) panic(panicMsg) } return nil }
func UnregisterProvider(provider base.Provider) { defer func() { if err := recover(); err != nil { debug.PrintStack() errorMsg := fmt.Sprintf("Occur FATAL error when unregister provider (provider=%v): %s", provider, err) base.Logger().Fatalln(errorMsg) } }() if provider == nil { panicMsg := "IdCenter: The provider is nil!\n" base.Logger().Fatal(panicMsg) panic(panicMsg) } name := provider.Name() if len(name) == 0 { panicMsg := "IdCenter: The name of provider is nil!\n" base.Logger().Fatal(panicMsg) panic(panicMsg) } switch t := interface{}(provider).(type) { case base.CacheProvider: _, contains := cacheProviderMap[name] if contains { delete(cacheProviderMap, name) } else { base.Logger().Warnf("IdCenter: The cache Provider named '%s' is NOTEXISTENT!\n", name) } case base.StorageProvider: _, contains := storageProviderMap[name] if contains { delete(storageProviderMap, name) } else { base.Logger().Warnf("IdCenter: The storage Provider named '%s' is NOTEXISTENT!\n", name) } default: panicMsg := fmt.Sprintf("IdCenter: Unexpected Provider type '%v'! (name=%s)\n", t, name) base.Logger().Fatal(panicMsg) panic(panicMsg) } return }