Skip to content

Commit

Permalink
feat: add no-op database client
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Jan 8, 2025
1 parent 96f9435 commit 820fd74
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
29 changes: 18 additions & 11 deletions cmd/ants/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,24 @@ func runQueenCommand(c *cli.Context) error {
logger.Debugln("Starting metrics server", "host", queenConfig.MetricsHost, "port", queenConfig.MetricsPort)
go metrics.ListenAndServe(queenConfig.MetricsHost, queenConfig.MetricsPort)

// initializing a new clickhouse client
client, err := db.NewClient(
queenConfig.ClickhouseAddress,
queenConfig.ClickhouseDatabase,
queenConfig.ClickhouseUsername,
queenConfig.ClickhousePassword,
queenConfig.ClickhouseSSL,
telemetry,
)
if err != nil {
return fmt.Errorf("init database client: %w", err)
var client db.Client
if queenConfig.ClickhouseAddress == "" {
logger.Warn("No clickhouse address provided, using no-op client.")
client = db.NewNoopClient()
} else {
// initializing a new clickhouse client
client, err = db.NewClickhouseClient(
queenConfig.ClickhouseAddress,
queenConfig.ClickhouseDatabase,
queenConfig.ClickhouseUsername,
queenConfig.ClickhousePassword,
queenConfig.ClickhouseSSL,
telemetry,
)
if err != nil {
return fmt.Errorf("init database client: %w", err)
}

}

// pinging database to check availability
Expand Down
18 changes: 17 additions & 1 deletion db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,30 @@ type Client interface {
BulkInsertRequests(ctx context.Context, requests []*Request) error
}

type NoopClient struct{}

var _ Client = (*NoopClient)(nil)

func NewNoopClient() *NoopClient {
return &NoopClient{}
}

func (n NoopClient) Ping(ctx context.Context) error {
return nil
}

func (n NoopClient) BulkInsertRequests(ctx context.Context, requests []*Request) error {
return nil
}

type ClickhouseClient struct {
driver.Conn
telemetry *metrics.Telemetry
}

var _ Client = (*ClickhouseClient)(nil)

func NewClient(address, database, username, password string, ssl bool, telemetry *metrics.Telemetry) (*ClickhouseClient, error) {
func NewClickhouseClient(address, database, username, password string, ssl bool, telemetry *metrics.Telemetry) (*ClickhouseClient, error) {
logger.Infoln("Creating new clickhouse client...")

conn, err := clickhouse.Open(&clickhouse.Options{
Expand Down

0 comments on commit 820fd74

Please sign in to comment.