Open opens the redis driver.
(_ context.Context, _ storepb.Engine, config db.ConnectionConfig)
| 47 | |
| 48 | // Open opens the redis driver. |
| 49 | func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) { |
| 50 | tlscfg, err := util.GetTLSConfig(config.DataSource) |
| 51 | if err != nil { |
| 52 | return nil, errors.Wrap(err, "redis: failed to get tls config") |
| 53 | } |
| 54 | db := 0 |
| 55 | if config.ConnectionContext.DatabaseName != "" { |
| 56 | database, err := strconv.Atoi(config.ConnectionContext.DatabaseName) |
| 57 | if err != nil { |
| 58 | return nil, errors.Wrapf(err, "failed to convert database %s to int", config.ConnectionContext.DatabaseName) |
| 59 | } |
| 60 | db = database |
| 61 | } |
| 62 | d.databaseName = fmt.Sprintf("%d", db) |
| 63 | switch config.DataSource.GetRedisType() { |
| 64 | case storepb.DataSource_REDIS_TYPE_UNSPECIFIED, storepb.DataSource_STANDALONE: |
| 65 | options := &redis.Options{ |
| 66 | Addr: fmt.Sprintf("%s:%s", config.DataSource.Host, config.DataSource.Port), |
| 67 | Username: config.DataSource.Username, |
| 68 | Password: config.Password, |
| 69 | TLSConfig: tlscfg, |
| 70 | DB: db, |
| 71 | } |
| 72 | if config.DataSource.GetSshHost() != "" { |
| 73 | sshClient, err := util.GetSSHClient(config.DataSource) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | d.sshClient = sshClient |
| 78 | |
| 79 | options.Dialer = func(_ context.Context, network, addr string) (net.Conn, error) { |
| 80 | conn, err := sshClient.Dial(network, addr) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | return &util.NoDeadlineConn{Conn: conn}, nil |
| 85 | } |
| 86 | } |
| 87 | client := redis.NewClient(options) |
| 88 | d.databaseName = fmt.Sprintf("%d", db) |
| 89 | d.rdb = client |
| 90 | case storepb.DataSource_SENTINEL: |
| 91 | sentinelAddrs := make([]string, 0, 1+len(config.DataSource.GetAdditionalAddresses())) |
| 92 | sentinelAddrs = append(sentinelAddrs, fmt.Sprintf("%s:%s", config.DataSource.Host, config.DataSource.Port)) |
| 93 | for _, sentinelAddr := range config.DataSource.GetAdditionalAddresses() { |
| 94 | sentinelAddrs = append(sentinelAddrs, fmt.Sprintf("%s:%s", sentinelAddr.Host, sentinelAddr.Port)) |
| 95 | } |
| 96 | options := &redis.FailoverOptions{ |
| 97 | MasterName: config.DataSource.GetMasterName(), |
| 98 | Username: config.DataSource.GetMasterUsername(), |
| 99 | Password: config.DataSource.GetMasterPassword(), |
| 100 | SentinelUsername: config.DataSource.Username, |
| 101 | SentinelPassword: config.Password, |
| 102 | SentinelAddrs: sentinelAddrs, |
| 103 | DB: db, |
| 104 | TLSConfig: tlscfg, |
| 105 | } |
| 106 | if config.DataSource.GetSshHost() != "" { |
nothing calls this directly
no test coverage detected