Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Implement Time Command #2963

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
297caea
Implemented Time Command
niharikabhavaraju Jan 16, 2025
1177732
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 17, 2025
1244741
Fixed code review changes
niharikabhavaraju Jan 17, 2025
579f0f3
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 19, 2025
3716426
Added Time command (cluster)
niharikabhavaraju Jan 19, 2025
b13f411
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 20, 2025
d20ea15
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 22, 2025
bcc00a7
Fixed code review comments
niharikabhavaraju Jan 22, 2025
92886ba
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 27, 2025
2ce5dbc
Fixed code review comments for multi cluster value
niharikabhavaraju Jan 27, 2025
0e04a4e
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 27, 2025
5364841
Fixed linting error
niharikabhavaraju Jan 27, 2025
fb97914
Merge branch 'niharika-timecommand' of github.com:niharikabhavaraju/v…
niharikabhavaraju Jan 27, 2025
ff976a7
Fixed code review changes
niharikabhavaraju Jan 28, 2025
6a64db9
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 28, 2025
355b339
Fix linting error
niharikabhavaraju Jan 28, 2025
6e55e05
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 28, 2025
27aa766
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 29, 2025
55d5d83
Merge branch 'niharika-timecommand' of github.com:niharikabhavaraju/v…
niharikabhavaraju Jan 29, 2025
88c3e2d
Fixed code review comments and failing tests
niharikabhavaraju Jan 29, 2025
4ee885f
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 30, 2025
e1c22a0
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ package api
import "C"

import (
"errors"
"fmt"
"math"
"strconv"
"unsafe"

"github.com/valkey-io/valkey-glide/go/glide/api/config"
"github.com/valkey-io/valkey-glide/go/glide/api/errors"
"github.com/valkey-io/valkey-glide/go/glide/api/options"
"github.com/valkey-io/valkey-glide/go/glide/protobuf"
"github.com/valkey-io/valkey-glide/go/glide/utils"
Expand Down Expand Up @@ -54,8 +55,10 @@ func successCallback(channelPtr unsafe.Pointer, cResponse *C.struct_CommandRespo

//export failureCallback
func failureCallback(channelPtr unsafe.Pointer, cErrorMessage *C.char, cErrorType C.RequestErrorType) {
defer C.free_error_message(cErrorMessage)
msg := C.GoString(cErrorMessage)
resultChannel := *(*chan payload)(channelPtr)
resultChannel <- payload{value: nil, error: goError(cErrorType, cErrorMessage)}
resultChannel <- payload{value: nil, error: errors.GoError(uint32(cErrorType), msg)}
}

type clientConfiguration interface {
Expand Down Expand Up @@ -92,7 +95,7 @@ func createClient(config clientConfiguration) (*baseClient, error) {
cErr := cResponse.connection_error_message
if cErr != nil {
message := C.GoString(cErr)
return nil, &ConnectionError{message}
return nil, &errors.ConnectionError{Msg: message}
}

return &baseClient{cResponse.conn_ptr}, nil
Expand All @@ -115,10 +118,10 @@ func (client *baseClient) executeCommand(requestType C.RequestType, args []strin
func (client *baseClient) executeCommandWithRoute(
requestType C.RequestType,
args []string,
route route,
route config.Route,
) (*C.struct_CommandResponse, error) {
if client.coreClient == nil {
return nil, &ClosingError{"ExecuteCommand failed. The client is closed."}
return nil, &errors.ClosingError{Msg: "ExecuteCommand failed. The client is closed."}
}
var cArgsPtr *C.uintptr_t = nil
var argLengthsPtr *C.ulong = nil
Expand All @@ -134,9 +137,9 @@ func (client *baseClient) executeCommandWithRoute(
var routeBytesPtr *C.uchar = nil
var routeBytesCount C.uintptr_t = 0
if route != nil {
routeProto, err := route.toRoutesProtobuf()
routeProto, err := route.ToRoutesProtobuf()
if err != nil {
return nil, &RequestError{"ExecuteCommand failed due to invalid route"}
return nil, &errors.RequestError{Msg: "ExecuteCommand failed due to invalid route"}
}
msg, err := proto.Marshal(routeProto)
if err != nil {
Expand Down Expand Up @@ -357,7 +360,7 @@ func (client *baseClient) LCS(key1 string, key2 string) (string, error) {

func (client *baseClient) GetDel(key string) (Result[string], error) {
if key == "" {
return CreateNilStringResult(), errors.New("key is required")
return CreateNilStringResult(), &errors.RequestError{Msg: "key is required"}
}

result, err := client.executeCommand(C.GetDel, []string{key})
Expand Down Expand Up @@ -1134,7 +1137,7 @@ func (client *baseClient) LMPop(keys []string, listDirection ListDirection) (map

// Check for potential length overflow.
if len(keys) > math.MaxInt-2 {
return nil, &RequestError{"Length overflow for the provided keys"}
return nil, &errors.RequestError{Msg: "Length overflow for the provided keys"}
}

// args slice will have 2 more arguments with the keys provided.
Expand Down Expand Up @@ -1162,7 +1165,7 @@ func (client *baseClient) LMPopCount(

// Check for potential length overflow.
if len(keys) > math.MaxInt-4 {
return nil, &RequestError{"Length overflow for the provided keys"}
return nil, &errors.RequestError{Msg: "Length overflow for the provided keys"}
}

// args slice will have 4 more arguments with the keys provided.
Expand Down Expand Up @@ -1190,7 +1193,7 @@ func (client *baseClient) BLMPop(

// Check for potential length overflow.
if len(keys) > math.MaxInt-3 {
return nil, &RequestError{"Length overflow for the provided keys"}
return nil, &errors.RequestError{Msg: "Length overflow for the provided keys"}
}

// args slice will have 3 more arguments with the keys provided.
Expand Down Expand Up @@ -1219,7 +1222,7 @@ func (client *baseClient) BLMPopCount(

// Check for potential length overflow.
if len(keys) > math.MaxInt-5 {
return nil, &RequestError{"Length overflow for the provided keys"}
return nil, &errors.RequestError{Msg: "Length overflow for the provided keys"}
}

// args slice will have 5 more arguments with the keys provided.
Expand Down Expand Up @@ -3492,3 +3495,24 @@ func (client *baseClient) XClaimJustIdWithOptions(
}
return handleStringArrayResponse(result)
}

// Returns the server time.
//
// Return value:
// The current server time as a String array with two elements:
// A UNIX TIME and the amount of microseconds already elapsed in the current second.
// The returned array is in a [UNIX TIME, Microseconds already elapsed] format.
//
// For example:
//
// result, err := client.Time()
// result: [{1737051660} {994688}]
//
// [valkey.io]: https://valkey.io/commands/time/
func (client *baseClient) Time() ([]string, error) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
result, err := client.executeCommand(C.Time, []string{})
if err != nil {
return nil, err
}
return handleRawStringArrayResponse(result)
}
11 changes: 6 additions & 5 deletions go/api/command_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package api
import (
"strconv"

"github.com/valkey-io/valkey-glide/go/glide/api/errors"
"github.com/valkey-io/valkey-glide/go/glide/utils"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func (opts *SetOptions) toArgs() ([]string, error) {
case KeepExisting:
args = append(args, string(opts.Expiry.Type))
default:
err = &RequestError{"Invalid expiry type"}
err = &errors.RequestError{Msg: "Invalid expiry type"}
}
}

Expand Down Expand Up @@ -101,7 +102,7 @@ func (opts *GetExOptions) toArgs() ([]string, error) {
case Persist:
args = append(args, string(opts.Expiry.Type))
default:
err = &RequestError{"Invalid expiry type"}
err = &errors.RequestError{Msg: "Invalid expiry type"}
}
}

Expand Down Expand Up @@ -144,7 +145,7 @@ func (expireCondition ExpireCondition) toString() (string, error) {
case NewExpiryLessThanCurrent:
return string(NewExpiryLessThanCurrent), nil
default:
return "", &RequestError{"Invalid expire condition"}
return "", &errors.RequestError{Msg: "Invalid expire condition"}
}
}

Expand Down Expand Up @@ -254,7 +255,7 @@ func (insertPosition InsertPosition) toString() (string, error) {
case After:
return string(After), nil
default:
return "", &RequestError{"Invalid insert position"}
return "", &errors.RequestError{Msg: "Invalid insert position"}
}
}

Expand All @@ -275,7 +276,7 @@ func (listDirection ListDirection) toString() (string, error) {
case Right:
return string(Right), nil
default:
return "", &RequestError{"Invalid list direction"}
return "", &errors.RequestError{Msg: "Invalid list direction"}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api
package config

import (
"fmt"
"strconv"
"strings"

"github.com/valkey-io/valkey-glide/go/glide/api/errors"
"github.com/valkey-io/valkey-glide/go/glide/protobuf"
)

// Request routing basic interface. Please use one of the following:
// - [api.SimpleNodeRoute]
// - [api.SlotIdRoute]
// - [api.SlotKeyRoute]
// - [api.ByAddressRoute]
type route interface {
toRoutesProtobuf() (*protobuf.Routes, error)
// - [config.SimpleNodeRoute]
// - [config.SlotIdRoute]
// - [config.SlotKeyRoute]
// - [config.ByAddressRoute]
type Route interface {
ToRoutesProtobuf() (*protobuf.Routes, error)
}

type SimpleNodeRoute int
Expand All @@ -32,7 +33,7 @@ const (
RandomRoute
)

func (simpleNodeRoute SimpleNodeRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
func (simpleNodeRoute SimpleNodeRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
simpleRouteProto, err := mapSimpleNodeRoute(simpleNodeRoute)
if err != nil {
return nil, err
Expand All @@ -55,7 +56,7 @@ func mapSimpleNodeRoute(simpleNodeRoute SimpleNodeRoute) (protobuf.SimpleRoutes,
case RandomRoute:
return protobuf.SimpleRoutes_Random, nil
default:
return protobuf.SimpleRoutes_Random, &RequestError{"Invalid simple node route"}
return protobuf.SimpleRoutes_Random, &errors.RequestError{Msg: "Invalid simple node route"}
}
}

Expand All @@ -76,7 +77,7 @@ func mapSlotType(slotType SlotType) (protobuf.SlotTypes, error) {
case SlotTypeReplica:
return protobuf.SlotTypes_Replica, nil
default:
return protobuf.SlotTypes_Primary, &RequestError{"Invalid slot type"}
return protobuf.SlotTypes_Primary, &errors.RequestError{Msg: "Invalid slot type"}
}
}

Expand All @@ -94,7 +95,7 @@ func NewSlotIdRoute(slotType SlotType, slotId int32) *SlotIdRoute {
return &SlotIdRoute{slotType: slotType, slotID: slotId}
}

func (slotIdRoute *SlotIdRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
func (slotIdRoute *SlotIdRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
slotType, err := mapSlotType(slotIdRoute.slotType)
if err != nil {
return nil, err
Expand Down Expand Up @@ -124,7 +125,7 @@ func NewSlotKeyRoute(slotType SlotType, slotKey string) *SlotKeyRoute {
return &SlotKeyRoute{slotType: slotType, slotKey: slotKey}
}

func (slotKeyRoute *SlotKeyRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
func (slotKeyRoute *SlotKeyRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
slotType, err := mapSlotType(slotKeyRoute.slotType)
if err != nil {
return nil, err
Expand Down Expand Up @@ -159,17 +160,17 @@ func NewByAddressRoute(host string, port int32) *ByAddressRoute {
func NewByAddressRouteWithHost(host string) (*ByAddressRoute, error) {
split := strings.Split(host, ":")
if len(split) != 2 {
return nil, &RequestError{
fmt.Sprintf(
return nil, &errors.RequestError{
Msg: fmt.Sprintf(
"no port provided, or host is not in the expected format 'hostname:port'. Received: %s", host,
),
}
}

port, err := strconv.ParseInt(split[1], 10, 32)
if err != nil {
return nil, &RequestError{
fmt.Sprintf(
return nil, &errors.RequestError{
Msg: fmt.Sprintf(
"port must be a valid integer. Received: %s", split[1],
),
}
Expand All @@ -178,7 +179,7 @@ func NewByAddressRouteWithHost(host string) (*ByAddressRoute, error) {
return &ByAddressRoute{host: split[0], port: int32(port)}, nil
}

func (byAddressRoute *ByAddressRoute) toRoutesProtobuf() (*protobuf.Routes, error) {
func (byAddressRoute *ByAddressRoute) ToRoutesProtobuf() (*protobuf.Routes, error) {
request := &protobuf.Routes{
Value: &protobuf.Routes_ByAddressRoute{
ByAddressRoute: &protobuf.ByAddressRoute{
Expand Down
28 changes: 13 additions & 15 deletions go/api/errors.go → go/api/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api
package errors

// #cgo LDFLAGS: -L../target/release -lglide_rs
// #include "../lib.h"
// #include "../../lib.h"
import "C"

// ConnectionError is a client error that occurs when there is an error while connecting or when a connection
// disconnects.
type ConnectionError struct {
msg string
Msg string
}

func (e *ConnectionError) Error() string { return e.msg }
func (e *ConnectionError) Error() string { return e.Msg }

// RequestError is a client error that occurs when an error is reported during a request.
type RequestError struct {
msg string
Msg string
}

func (e *RequestError) Error() string { return e.msg }
func (e *RequestError) Error() string { return e.Msg }

// ExecAbortError is a client error that occurs when a transaction is aborted.
type ExecAbortError struct {
Expand All @@ -44,22 +44,20 @@ func (e *DisconnectError) Error() string { return e.msg }

// ClosingError is a client error that indicates that the client has closed and is no longer usable.
type ClosingError struct {
msg string
Msg string
}

func (e *ClosingError) Error() string { return e.msg }
func (e *ClosingError) Error() string { return e.Msg }

func goError(cErrorType C.RequestErrorType, cErrorMessage *C.char) error {
defer C.free_error_message(cErrorMessage)
msg := C.GoString(cErrorMessage)
func GoError(cErrorType uint32, errorMessage string) error {
switch cErrorType {
case C.ExecAbort:
return &ExecAbortError{msg}
return &ExecAbortError{errorMessage}
case C.Timeout:
return &TimeoutError{msg}
return &TimeoutError{errorMessage}
case C.Disconnect:
return &DisconnectError{msg}
return &DisconnectError{errorMessage}
default:
return &RequestError{msg}
return &RequestError{errorMessage}
}
}
Loading
Loading