From 76d4125b619a85fc140ca3fd7be8a2b8f602db96 Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Tue, 31 Oct 2023 16:54:56 +0300 Subject: [PATCH] crud: change timeout option type `Timeout` is an option supported for all crud operations using VShard API. In Lua, timeout has `number` type, so float values are allowed. Float timeouts can be useful to set the timeout less than a second. After this patch, `Timeout` will be an optional float64 instead of an optional int. This is a breaking change. CRUD allows to use negative timeouts. This approach does not make any sense for an operation: for example, initial schema fetch will fail. Since the module itself does not check for a negative value argument, we do not forbid it here too. Closes #342 --- CHANGELOG.md | 2 ++ README.md | 6 ++++++ crud/count.go | 2 +- crud/get.go | 2 +- crud/options.go | 31 +++++++++++++++++++++++++------ crud/request_test.go | 4 ++-- crud/select.go | 2 +- crud/tarantool_test.go | 24 ++++++++++++------------ 8 files changed, 50 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63ed00b39..fbec3f1b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release. - `iproto.Feature` type now used instead of `ProtocolFeature` (#337) - `iproto.IPROTO_FEATURE_` constants now used instead of local `Feature` constants for `protocol` (#337) +- Change `crud` operations `Timeout` option type to `crud.OptFloat64` + instead of `crud.OptUint` (#342) ### Deprecated diff --git a/README.md b/README.md index 2c7de68c4..eae6f92ab 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ faster than other packages according to public benchmarks. * [decimal package](#decimal-package) * [multi package](#multi-package) * [pool package](#pool-package) + * [crud package](#crud-package) * [msgpack.v5](#msgpackv5) * [Call = Call17](#call--call17) * [IPROTO constants](#iproto-constants) @@ -187,6 +188,11 @@ The subpackage has been deleted. You could use `pool` instead. * `pool.Add` now accepts context as first argument, which user may cancel in process. +#### crud package + +* `crud` operations `Timeout` option has `crud.OptFloat64` type + instead of `crud.OptUint`. + #### msgpack.v5 Most function names and argument types in `msgpack.v5` and `msgpack.v2` diff --git a/crud/count.go b/crud/count.go index dc89f916e..e4c163249 100644 --- a/crud/count.go +++ b/crud/count.go @@ -15,7 +15,7 @@ type CountResult = NumberResult type CountOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString diff --git a/crud/get.go b/crud/get.go index fcfef5426..6ab91440e 100644 --- a/crud/get.go +++ b/crud/get.go @@ -12,7 +12,7 @@ import ( type GetOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString diff --git a/crud/options.go b/crud/options.go index 214417d1f..eec3fc0db 100644 --- a/crud/options.go +++ b/crud/options.go @@ -63,6 +63,25 @@ func (opt OptInt) Get() (int, bool) { return opt.value, opt.exist } +// OptFloat64 is an optional float64. +type OptFloat64 struct { + value float64 + exist bool +} + +// MakeOptFloat64 creates an optional float64 from value. +func MakeOptFloat64(value float64) OptFloat64 { + return OptFloat64{ + value: value, + exist: true, + } +} + +// Get returns the float64 value or an error if not present. +func (opt OptFloat64) Get() (float64, bool) { + return opt.value, opt.exist +} + // OptString is an optional string. type OptString struct { value string @@ -120,7 +139,7 @@ func (o *OptTuple) Get() (interface{}, bool) { type BaseOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString @@ -144,7 +163,7 @@ func (opts BaseOpts) EncodeMsgpack(enc *msgpack.Encoder) error { type SimpleOperationOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString @@ -186,7 +205,7 @@ func (opts SimpleOperationOpts) EncodeMsgpack(enc *msgpack.Encoder) error { type SimpleOperationObjectOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString @@ -232,7 +251,7 @@ func (opts SimpleOperationObjectOpts) EncodeMsgpack(enc *msgpack.Encoder) error type OperationManyOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString @@ -280,7 +299,7 @@ func (opts OperationManyOpts) EncodeMsgpack(enc *msgpack.Encoder) error { type OperationObjectManyOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString @@ -332,7 +351,7 @@ func (opts OperationObjectManyOpts) EncodeMsgpack(enc *msgpack.Encoder) error { type BorderOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString diff --git a/crud/request_test.go b/crud/request_test.go index 4c49a9214..8a64ab427 100644 --- a/crud/request_test.go +++ b/crud/request_test.go @@ -137,7 +137,7 @@ func BenchmarkLenRequest(b *testing.B) { buf.Reset() req := crud.MakeLenRequest(spaceName). Opts(crud.LenOpts{ - Timeout: crud.MakeOptUint(3), + Timeout: crud.MakeOptFloat64(3.5), }) if err := req.Body(nil, enc); err != nil { b.Error(err) @@ -156,7 +156,7 @@ func BenchmarkSelectRequest(b *testing.B) { buf.Reset() req := crud.MakeSelectRequest(spaceName). Opts(crud.SelectOpts{ - Timeout: crud.MakeOptUint(3), + Timeout: crud.MakeOptFloat64(3.5), VshardRouter: crud.MakeOptString("asd"), Balance: crud.MakeOptBool(true), }) diff --git a/crud/select.go b/crud/select.go index 0d0b5708a..198f74bae 100644 --- a/crud/select.go +++ b/crud/select.go @@ -12,7 +12,7 @@ import ( type SelectOpts struct { // Timeout is a `vshard.call` timeout and vshard // master discovery timeout (in seconds). - Timeout OptUint + Timeout OptFloat64 // VshardRouter is cartridge vshard group name or // vshard router instance. VshardRouter OptString diff --git a/crud/tarantool_test.go b/crud/tarantool_test.go index e3b8615b8..511980bd9 100644 --- a/crud/tarantool_test.go +++ b/crud/tarantool_test.go @@ -37,7 +37,7 @@ var startOpts test_helpers.StartOpts = test_helpers.StartOpts{ RetryTimeout: 500 * time.Millisecond, } -var timeout = uint(1) +var timeout = float64(1.1) var operations = []crud.Operation{ { @@ -48,43 +48,43 @@ var operations = []crud.Operation{ } var selectOpts = crud.SelectOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var countOpts = crud.CountOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var getOpts = crud.GetOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var minOpts = crud.MinOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var maxOpts = crud.MaxOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var baseOpts = crud.BaseOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var simpleOperationOpts = crud.SimpleOperationOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var simpleOperationObjectOpts = crud.SimpleOperationObjectOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var opManyOpts = crud.OperationManyOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var opObjManyOpts = crud.OperationObjectManyOpts{ - Timeout: crud.MakeOptUint(timeout), + Timeout: crud.MakeOptFloat64(timeout), } var conditions = []crud.Condition{ @@ -815,7 +815,7 @@ func TestGetAdditionalOpts(t *testing.T) { defer conn.Close() req := crud.MakeGetRequest(spaceName).Key(key).Opts(crud.GetOpts{ - Timeout: crud.MakeOptUint(1), + Timeout: crud.MakeOptFloat64(1.1), Fields: crud.MakeOptTuple([]interface{}{"name"}), Mode: crud.MakeOptString("read"), PreferReplica: crud.MakeOptBool(true),