Skip to content

Commit

Permalink
feat: Quota API
Browse files Browse the repository at this point in the history
* Quota limits
* Quota usage
  • Loading branch information
efirs committed Oct 10, 2022
1 parent 1da1e1d commit 153a83c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
27 changes: 23 additions & 4 deletions client/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ import (
"github.com/tigrisdata/tigris-client-go/driver"
)

// D is single instance of client.
var D driver.Driver

// M is single instance of auth service client.
var (
// D is single instance of client.
D driver.Driver

// M is single instance of management service client.
M driver.Management
cfg *cconfig.Driver

// O is single instance of observability service client.
O driver.Observability
)

var ErrUnknownProtocol = fmt.Errorf("unknown protocol set by TIGRIS_PROTOCOL. allowed: grpc, http, https")
Expand Down Expand Up @@ -140,6 +143,22 @@ func ManagementGet() driver.Management {
return M
}

func ObservabilityGet() driver.Observability {
if O == nil {
ctx, cancel := util.GetContext(context.Background())
defer cancel()

drv, err := driver.NewObservability(ctx, cfg)
if err != nil {
util.Error(err, "tigris client initialization failed")
}

O = drv
}

return O
}

func Transact(bctx context.Context, db string, fn func(ctx context.Context, tx driver.Tx)) {
ctx, cancel := util.GetContext(bctx)
defer cancel()
Expand Down
68 changes: 68 additions & 0 deletions cmd/quota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2022 Tigris Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/spf13/cobra"
"github.com/tigrisdata/tigris-cli/client"
"github.com/tigrisdata/tigris-cli/util"
)

var quotaLimitsCmd = &cobra.Command{
Use: "limits",
Short: "Show quota limits for the namespace user logged in to",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := util.GetContext(cmd.Context())
defer cancel()

l, err := client.ObservabilityGet().QuotaLimits(ctx)
if err != nil {
util.Error(err, "quota limits failed")
}

if err := util.PrettyJSON(l); err != nil {
util.Error(err, "quota limits failed")
}
},
}

var quotaUsageCmd = &cobra.Command{
Use: "usage",
Short: "Show current quota usage for the namespace user logged in to",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := util.GetContext(cmd.Context())
defer cancel()

u, err := client.ObservabilityGet().QuotaUsage(ctx)
if err != nil {
util.Error(err, "quota usage failed")
}

if err := util.PrettyJSON(u); err != nil {
util.Error(err, "quota usage failed")
}
},
}

var quotaCmd = &cobra.Command{
Use: "quota",
Short: "Quota related commands",
}

func init() {
quotaCmd.AddCommand(quotaLimitsCmd)
quotaCmd.AddCommand(quotaUsageCmd)
rootCmd.AddCommand(quotaCmd)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/rs/zerolog v1.27.0
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.12.0
github.com/tigrisdata/tigris-client-go v1.0.0-beta.3
github.com/tigrisdata/tigris-client-go v1.0.0-beta.5
golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs=
github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo=
github.com/tigrisdata/tigris-client-go v1.0.0-beta.3 h1:wCK7lzDzwKqeYontuBpihRaUnSZBrPpPkc5AyUP9WZk=
github.com/tigrisdata/tigris-client-go v1.0.0-beta.3/go.mod h1:E1wI4sV0uNvDLPyq0l5YZhVLWUod1Vi1FhKf5bRhDeI=
github.com/tigrisdata/tigris-client-go v1.0.0-beta.5 h1:V2G0FnXLaeMYqA39hG7YLSoo/topkuEpeEPnETPYMEU=
github.com/tigrisdata/tigris-client-go v1.0.0-beta.5/go.mod h1:E1wI4sV0uNvDLPyq0l5YZhVLWUod1Vi1FhKf5bRhDeI=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
Expand Down
7 changes: 6 additions & 1 deletion tests/db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ test_pubsub() {
exp_out='{"Field1":123}
{"Field1":456}
{"Key1":"ee"}'
exp_out1='{"Key1":"ee"}
{"Field1":123}
{"Field1":456}'

(
# Give some time for subscribe to start
Expand All @@ -325,7 +328,9 @@ test_pubsub() {
pid=$!

out=$($cli subscribe db1 coll_msg '{}' --limit 3)
diff -w -u <(echo "$exp_out") <(echo "$out")
# There is no ordering guarantee so compare both possibilities
diff -w -u <(echo "$exp_out") <(echo "$out") ||
diff -w -u <(echo "$exp_out1") <(echo "$out")

# make sure subhell terminated
wait $pid
Expand Down

0 comments on commit 153a83c

Please sign in to comment.