From 595b1917f369740d118aa88900a7673c653717e1 Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> Date: Wed, 5 Oct 2022 00:14:23 -0400 Subject: [PATCH] Add support for Redis connection pool size --- redis/README.md | 7 +++++++ redis/config.go | 11 ++++++++++- redis/redis.go | 8 +++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/redis/README.md b/redis/README.md index 161a028e..01eeeabb 100644 --- a/redis/README.md +++ b/redis/README.md @@ -50,6 +50,7 @@ store := redis.New(redis.Config{ Database: 0, Reset: false, TLSConfig: nil, + PoolSize: 10 * runtime.GOMAXPROCS(0), } // or just the url with all information @@ -102,6 +103,11 @@ type Config struct { // // Optional. Default is nil TLSConfig *tls.Config + + // Maximum number of socket connections. + // + // Optional. Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS. + PoolSize int } ``` @@ -117,5 +123,6 @@ var ConfigDefault = Config{ Database: 0, Reset: false, TLSConfig: nil, + PoolSize: 10 * runtime.GOMAXPROCS(0), } ``` diff --git a/redis/config.go b/redis/config.go index 1a0876d6..c154678f 100644 --- a/redis/config.go +++ b/redis/config.go @@ -1,6 +1,9 @@ package redis -import "crypto/tls" +import ( + "crypto/tls" + "runtime" +) // Config defines the config for storage. type Config struct { @@ -43,6 +46,11 @@ type Config struct { // TLS Config to use. When set TLS will be negotiated. TLSConfig *tls.Config + // Maximum number of socket connections. + // + // Optional. Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS. + PoolSize int + //////////////////////////////////// // Adaptor related config options // //////////////////////////////////// @@ -60,6 +68,7 @@ var ConfigDefault = Config{ Database: 0, Reset: false, TLSConfig: nil, + PoolSize: 10 * runtime.GOMAXPROCS(0), } // Helper function to set default values diff --git a/redis/redis.go b/redis/redis.go index 4f2f6ea0..b3aa9660 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -19,16 +19,18 @@ func New(config ...Config) *Storage { cfg := configDefault(config...) // Create new redis client - var options *redis.Options var err error if cfg.URL != "" { options, err = redis.ParseURL(cfg.URL) - options.TLSConfig = cfg.TLSConfig + if err != nil { panic(err) } + + options.TLSConfig = cfg.TLSConfig + options.PoolSize = cfg.PoolSize } else { options = &redis.Options{ Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), @@ -36,6 +38,7 @@ func New(config ...Config) *Storage { Username: cfg.Username, Password: cfg.Password, TLSConfig: cfg.TLSConfig, + PoolSize: cfg.PoolSize, } } @@ -71,7 +74,6 @@ func (s *Storage) Get(key string) ([]byte, error) { return val, err } -// Set key with value // Set key with value func (s *Storage) Set(key string, val []byte, exp time.Duration) error { // Ain't Nobody Got Time For That