tls_config.go 1001 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package clickhouse
  2. import (
  3. "crypto/tls"
  4. "sync"
  5. )
  6. // Based on the original implementation in the project go-sql-driver/mysql:
  7. // https://github.com/go-sql-driver/mysql/blob/master/utils.go
  8. var (
  9. tlsConfigLock sync.RWMutex
  10. tlsConfigRegistry map[string]*tls.Config
  11. )
  12. // RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
  13. func RegisterTLSConfig(key string, config *tls.Config) error {
  14. tlsConfigLock.Lock()
  15. if tlsConfigRegistry == nil {
  16. tlsConfigRegistry = make(map[string]*tls.Config)
  17. }
  18. tlsConfigRegistry[key] = config
  19. tlsConfigLock.Unlock()
  20. return nil
  21. }
  22. // DeregisterTLSConfig removes the tls.Config associated with key.
  23. func DeregisterTLSConfig(key string) {
  24. tlsConfigLock.Lock()
  25. if tlsConfigRegistry != nil {
  26. delete(tlsConfigRegistry, key)
  27. }
  28. tlsConfigLock.Unlock()
  29. }
  30. func getTLSConfigClone(key string) (config *tls.Config) {
  31. tlsConfigLock.RLock()
  32. if v, ok := tlsConfigRegistry[key]; ok {
  33. config = v.Clone()
  34. }
  35. tlsConfigLock.RUnlock()
  36. return
  37. }