Skip to content

Commit

Permalink
🔇 silent changes: update structs and base functions #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Feb 18, 2025
1 parent 8e35e07 commit 805e2b4
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 36 deletions.
91 changes: 91 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
package redisc

import (
"strings"
"time"

"github.com/go-redis/redis"
"github.com/sivaosorg/unify4g"
"github.com/sivaosorg/wrapify"
)

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Getter settings
//_______________________________________________________________________

func NewSettings() *Settings {
s := &Settings{}
s.
SetRetry(NewRetrySettings()).
SetTimeout(NewTimeoutSettings()).
SetPool(NewPoolSettings()).
SetConn(NewConnSettings())
return s
}

func NewConnSettings() *connectionSettings {
c := &connectionSettings{
network: "tcp", // Use TCP for most connections. Use "unix" if you prefer a Unix domain socket.
database: 0, // Connects to the first logical database. Change if your application needs a different DB.
}
return c
}

func NewRetrySettings() *retrySettings {
r := &retrySettings{
maxRetries: 3, // Three retry attempts for commands, balancing resilience and responsiveness.
minRetryBackoff: 8 * time.Millisecond, // Provides a short initial delay between retries.
maxRetryBackoff: 512 * time.Millisecond, // Caps the maximum delay between retries to prevent long waits.
}
return r
}

func NewTimeoutSettings() *timeoutSettings {
t := &timeoutSettings{
connTimeout: 5 * time.Second, // Allows a moderate wait time when establishing a connection.
readTimeout: 3 * time.Second, // Sufficient for most environments to avoid long hangs during operations.
writeTimeout: 3 * time.Second, // Sufficient for most environments to avoid long hangs during operations.
}
return t
}

func NewPoolSettings() *poolSettings {
p := &poolSettings{
poolSize: 10, // Supports moderate concurrency. Increase if your application has a high number of simultaneous requests.
minIdleConn: 2, // Keeps a couple of connections always ready to reduce latency.
maxConnAge: 0, // Connections are recycled indefinitely. Set a non-zero value to force periodic connection renewal.
poolTimeout: 4 * time.Second, // Wait up to 4 seconds for a free connection from the pool.
idleTimeout: 5 * time.Minute, // Closes idle connections after 5 minutes, freeing up resources.
idleCheckFrequency: 1 * time.Minute, // Checks every minute to clear out idle connections.
}
return p
}

// IsEnabled returns true if the configuration is enabled, indicating that
// a connection to Redis should be attempted.
func (c *Settings) IsEnabled() bool {
Expand Down Expand Up @@ -50,6 +100,30 @@ func (c *Settings) Pool() *poolSettings {
return c.pool
}

// redis://<username>:<password>@<host>:<port>
func (c *Settings) String(safe bool) string {
var builder strings.Builder
builder.WriteString("redis://")
if unify4g.IsEmpty(c.conn.username) && unify4g.IsEmpty(c.conn.password) {
builder.WriteString(c.conn.connectionStrings)
return builder.String()
}
if unify4g.IsNotEmpty(c.conn.username) {
builder.WriteString(c.conn.username)
}
if unify4g.IsNotEmpty(c.conn.password) {
builder.WriteString(":")
if safe {
builder.WriteString("*****")
} else {
builder.WriteString(c.conn.password)
}
builder.WriteString("@")
builder.WriteString(c.conn.connectionStrings)
}
return builder.String()
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Getter Datasource
//_______________________________________________________________________
Expand Down Expand Up @@ -111,21 +185,33 @@ func (c *Settings) SetKeepalive(value bool) *Settings {
}

func (c *Settings) SetConn(value *connectionSettings) *Settings {
if value == nil {
value = NewConnSettings()
}
c.conn = value
return c
}

func (c *Settings) SetRetry(value *retrySettings) *Settings {
if value == nil {
value = NewRetrySettings()
}
c.retry = value
return c
}

func (c *Settings) SetTimeout(value *timeoutSettings) *Settings {
if value == nil {
value = NewTimeoutSettings()
}
c.timeout = value
return c
}

func (c *Settings) SetPool(value *poolSettings) *Settings {
if value == nil {
value = NewPoolSettings()
}
c.pool = value
return c
}
Expand All @@ -144,6 +230,11 @@ func (c *connectionSettings) SetConnectionStrings(value string) *connectionSetti
return c
}

func (c *connectionSettings) SetUsername(value string) *connectionSettings {
c.username = value
return c
}

func (c *connectionSettings) SetPassword(value string) *connectionSettings {
c.password = value
return c
Expand Down
8 changes: 8 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
package redisc

import "time"

const (
// defaultPingInterval defines the frequency at which the connection is pinged.
defaultPingInterval = 30 * time.Second
defaultTimeFormat = "2006-01-02 15:04:05.000000"
)
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ go 1.23.1

require (
github.com/go-redis/redis v6.15.9+incompatible
github.com/sivaosorg/loggy v0.0.1
github.com/sivaosorg/unify4g v0.0.2
github.com/sivaosorg/wrapify v0.0.3
)

require (
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.36.2 // indirect
github.com/sivaosorg/unify4g v0.0.2 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.36.2 h1:koNYke6TVk6ZmnyHrCXba/T/MoLBXFjeC1PtvYgw0A8=
github.com/onsi/gomega v1.36.2/go.mod h1:DdwyADRjrc825LhMEkD76cHR5+pUnjhUN8GlHlRPHzY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sivaosorg/loggy v0.0.1 h1:pWd55wcSH6bn6I1ETkFmaCwv+fE5yBSo68KNf+Vg3Lg=
github.com/sivaosorg/loggy v0.0.1/go.mod h1:gI3lAgO8b8aukpUymS0DKMz9/ccVfwc3IN2C/noHYN8=
github.com/sivaosorg/unify4g v0.0.2 h1:5UVUHqp4tJnqgiZaFs5iSxdCab5hTB9Jx3HQNMyZZSY=
github.com/sivaosorg/unify4g v0.0.2/go.mod h1:rkCukiHwnpNmbu/sO5VCM3OM5wm1dfPDrLqtwbmgLgQ=
github.com/sivaosorg/wrapify v0.0.3 h1:4Lgg1jKuacMsCFjR7TUmPDftnO+MnKkw4ePkIBPkJIo=
Expand Down
Loading

0 comments on commit 805e2b4

Please sign in to comment.