This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cluster package, made monitor cluster aware
- Loading branch information
Showing
18 changed files
with
525 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cluster | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-redis/redis/v8" | ||
zLogger "github.com/mrz1836/go-logger" | ||
) | ||
|
||
type ( | ||
|
||
// Client is the client (configuration) | ||
Client struct { | ||
pubSubService | ||
options *clientOptions | ||
} | ||
|
||
// clientOptions holds all the configuration for the client | ||
clientOptions struct { | ||
coordinator Coordinator // which coordinator to use, either 'memory' or 'redis' | ||
debug bool // For extra logs and additional debug information | ||
logger zLogger.GormLoggerInterface // Internal logger interface | ||
newRelicEnabled bool // Whether to use New Relic | ||
prefix string // the cluster key prefix to use before all keys | ||
redisOptions *redis.Options | ||
} | ||
) | ||
|
||
// NewClient create new cluster client | ||
func NewClient(ctx context.Context, opts ...ClientOps) (*Client, error) { | ||
// Create a new client with defaults | ||
client := &Client{options: defaultClientOptions()} | ||
|
||
// Overwrite defaults with any set by user | ||
for _, opt := range opts { | ||
opt(client.options) | ||
} | ||
|
||
// Use NewRelic if it's enabled (use existing txn if found on ctx) | ||
ctx = client.options.getTxnCtx(ctx) | ||
|
||
// Set logger if not set | ||
if client.options.logger == nil { | ||
client.options.logger = zLogger.NewGormLogger(client.IsDebug(), 4) | ||
} | ||
|
||
if client.options.coordinator == CoordinatorRedis { | ||
pubSubClient, err := NewRedisPubSub(ctx, client.options.redisOptions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pubSubClient.debug = client.options.debug | ||
pubSubClient.prefix = client.options.prefix | ||
client.pubSubService = pubSubClient | ||
} else { | ||
pubSubClient, err := NewMemoryPubSub(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pubSubClient.debug = client.options.debug | ||
pubSubClient.prefix = client.options.prefix | ||
client.pubSubService = pubSubClient | ||
} | ||
|
||
// Return the client | ||
return client, nil | ||
} | ||
|
||
// IsDebug returns whether debugging is on or off | ||
func (c *Client) IsDebug() bool { | ||
return c.options.debug | ||
} | ||
|
||
// GetClusterPrefix returns the cluster key prefix that can be used in things like Redis | ||
func (c *Client) GetClusterPrefix() string { | ||
return c.options.prefix | ||
} |
Oops, something went wrong.