forked from openGemini/opengemini-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support new OpenGemini client with options
Signed-off-by: PennyYoon <525296438@qq.com>
- Loading branch information
1 parent
2a41447
commit bfc8686
Showing
1 changed file
with
123 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,124 @@ | ||
package opengemini | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
) | ||
|
||
var _ OpenGemini = (*Client)(nil) | ||
|
||
type Options = func(client *Client) error | ||
|
||
type Client struct { | ||
Addresses []*Address | ||
AuthConfig *AuthConfig | ||
BatchConfig *BatchConfig | ||
GzipEnable bool | ||
TlsConfig *tls.Config | ||
} | ||
|
||
func NewClient(addresses []*Address, options ...Options) (*Client, error) { | ||
client := new(Client) | ||
if len(addresses) == 0 { | ||
return nil, errors.New("must have at least one address") | ||
} | ||
client.Addresses = addresses | ||
for _, op := range options { | ||
err := op(client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return client, nil | ||
} | ||
|
||
func WithPassword(username, password string) Options { | ||
return func(client *Client) error { | ||
client.AuthConfig = &AuthConfig{ | ||
AuthType: Password, | ||
UserName: username, | ||
Password: password, | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func WithToken(token string) Options { | ||
return func(client *Client) error { | ||
client.AuthConfig = &AuthConfig{ | ||
AuthType: Token, | ||
Token: token, | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func WithGzipEnable(enable bool) Options { | ||
return func(client *Client) error { | ||
client.GzipEnable = enable | ||
return nil | ||
} | ||
} | ||
|
||
func WithBatchConfig(enable bool, size, interval int) Options { | ||
return func(client *Client) error { | ||
if enable && (size <= 0 || interval <= 0) { | ||
return errors.New("if batch enable , size and interval must more than 0") | ||
} | ||
client.BatchConfig = &BatchConfig{ | ||
enable, | ||
interval, | ||
size, | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func WithTlsConfig(tls *tls.Config) Options { | ||
return func(client *Client) error { | ||
client.TlsConfig = tls | ||
return nil | ||
} | ||
} | ||
|
||
type Address struct { | ||
Host string | ||
Port int | ||
} | ||
|
||
type AuthType int | ||
|
||
const ( | ||
Password AuthType = iota | ||
Token | ||
) | ||
|
||
type AuthConfig struct { | ||
AuthType AuthType | ||
UserName string | ||
Password string | ||
Token string | ||
} | ||
|
||
type BatchConfig struct { | ||
BatchEnable bool | ||
BatchInterval int | ||
BatchSize int | ||
} | ||
|
||
type OpenGemini interface { | ||
WriteBatchPoint(points BatchPoints) | ||
Check failure on line 110 in opengemini/client.go GitHub Actions / lint
|
||
WritePoint(point Point) | ||
Check failure on line 111 in opengemini/client.go GitHub Actions / lint
|
||
//WriteLineProtocol() | ||
//Query() | ||
} | ||
|
||
func (c *Client) WriteBatchPoint(points BatchPoints) { | ||
Check failure on line 116 in opengemini/client.go GitHub Actions / lint
|
||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Client) WritePoint(point Point) { | ||
Check failure on line 121 in opengemini/client.go GitHub Actions / lint
|
||
//TODO implement me | ||
panic("implement me") | ||
} |