github.com/kaydxh/golang@v0.0.131/pkg/database/redis/config.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package redis
    23  
    24  import (
    25  	"context"
    26  
    27  	"github.com/go-redis/redis/v8"
    28  	viper_ "github.com/kaydxh/golang/pkg/viper"
    29  	"github.com/sirupsen/logrus"
    30  	"github.com/spf13/viper"
    31  )
    32  
    33  type Config struct {
    34  	Proto Redis
    35  	opts  struct {
    36  		// If set, overrides params below
    37  		viper *viper.Viper
    38  	}
    39  }
    40  
    41  type completedConfig struct {
    42  	*Config
    43  	completeError error
    44  }
    45  
    46  type CompletedConfig struct {
    47  	// Embed a private pointer that cannot be instantiated outside of this package.
    48  	*completedConfig
    49  }
    50  
    51  func (c *completedConfig) New(ctx context.Context) (*redis.Client, error) {
    52  
    53  	logrus.Infof("Installing Redis")
    54  
    55  	if c.completeError != nil {
    56  		return nil, c.completeError
    57  	}
    58  
    59  	if !c.Proto.GetEnabled() {
    60  		logrus.Warnf("Redis disenabled")
    61  		return nil, nil
    62  	}
    63  
    64  	redisDB, err := c.install(ctx)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	logrus.Infof("Installed Redis")
    69  
    70  	return redisDB, nil
    71  }
    72  
    73  func (c *completedConfig) install(ctx context.Context) (*redis.Client, error) {
    74  	db := NewRedisClient(
    75  
    76  		DBConfig{
    77  			Addresses: c.Proto.GetAddresses(),
    78  			UserName:  c.Proto.GetUsername(),
    79  			Password:  c.Proto.GetPassword(),
    80  			DB:        int(c.Proto.GetDb()),
    81  		},
    82  		WithPoolSize(int(c.Proto.GetPoolSize())),
    83  		WithMinIdleConnections(int(c.Proto.GetMinIdleConns())),
    84  		WithDialTimeout(c.Proto.GetDialTimeout().AsDuration()),
    85  		WithReadTimeout(c.Proto.GetReadTimeout().AsDuration()),
    86  		WithWriteTimeout(c.Proto.GetWriteTimeout().AsDuration()),
    87  	)
    88  
    89  	return db.GetDatabaseUntil(
    90  		ctx,
    91  		c.Proto.GetMaxWaitDuration().AsDuration(),
    92  		c.Proto.GetFailAfterDuration().AsDuration(),
    93  	)
    94  
    95  }
    96  
    97  // Complete set default ServerRunOptions.
    98  func (c *Config) Complete() CompletedConfig {
    99  	err := c.loadViper()
   100  	if err != nil {
   101  		return CompletedConfig{&completedConfig{
   102  			Config:        c,
   103  			completeError: err,
   104  		}}
   105  	}
   106  
   107  	return CompletedConfig{&completedConfig{Config: c}}
   108  }
   109  
   110  func (c *Config) loadViper() error {
   111  	if c.opts.viper != nil {
   112  		return viper_.UnmarshalProtoMessageWithJsonPb(c.opts.viper, &c.Proto)
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  func NewConfig(options ...ConfigOption) *Config {
   119  	c := &Config{}
   120  	c.ApplyOptions(options...)
   121  
   122  	return c
   123  }