Skip to content

Commit a664c6b

Browse files
DerekBumoleg-jukovec
authored andcommitted
api: use iproto types and constants for the protocol
Replaced the local `ProtocolFeature` type with the `iproto.Feature`. Replaced local `Feature` constants with their `iproto.IPROTO_FEATURE_` analogues. Closes #337
1 parent 3f7860e commit a664c6b

15 files changed

+167
-188
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
4040
`pool.Connect` and `pool.Add` now accept context as first argument, which
4141
user may cancel in process. If `pool.Connect` is canceled in progress, an
4242
error will be returned. All created connections will be closed.
43+
- `iproto.Feature` type now used instead of `ProtocolFeature` (#337)
44+
- `iproto.IPROTO_FEATURE_` constants now used instead of local `Feature`
45+
constants for `protocol` (#337)
4346

4447
### Deprecated
4548

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ now does not attempt to reconnect and tries to establish a connection only once.
234234
Function might be canceled via context. Context accepted as first argument,
235235
and user may cancel it in process.
236236

237+
#### Protocol changes
238+
239+
* `iproto.Feature` type used instead of `ProtocolFeature`.
240+
* `iproto.IPROTO_FEATURE_` constants used instead of local ones.
241+
237242
## Contributing
238243

239244
See [the contributing guide](CONTRIBUTING.md) for detailed instructions on how

connection.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ func (conn *Connection) dial(ctx context.Context) error {
564564

565565
// Subscribe shutdown event to process graceful shutdown.
566566
if conn.shutdownWatcher == nil &&
567-
isFeatureInSlice(WatchersFeature, conn.serverProtocolInfo.Features) {
567+
isFeatureInSlice(iproto.IPROTO_FEATURE_WATCHERS,
568+
conn.serverProtocolInfo.Features) {
568569
watcher, werr := conn.newWatcherImpl(shutdownEventKey, shutdownEventCallback)
569570
if werr != nil {
570571
return werr
@@ -1425,7 +1426,7 @@ func subscribeWatchChannel(conn *Connection, key string) (chan watchState, error
14251426
return st, nil
14261427
}
14271428

1428-
func isFeatureInSlice(expected ProtocolFeature, actualSlice []ProtocolFeature) bool {
1429+
func isFeatureInSlice(expected iproto.Feature, actualSlice []iproto.Feature) bool {
14291430
for _, actual := range actualSlice {
14301431
if expected == actual {
14311432
return true
@@ -1436,8 +1437,8 @@ func isFeatureInSlice(expected ProtocolFeature, actualSlice []ProtocolFeature) b
14361437

14371438
// NewWatcher creates a new Watcher object for the connection.
14381439
//
1439-
// You need to require WatchersFeature to use watchers, see examples for the
1440-
// function.
1440+
// You need to require IPROTO_FEATURE_WATCHERS to use watchers, see examples
1441+
// for the function.
14411442
//
14421443
// After watcher creation, the watcher callback is invoked for the first time.
14431444
// In this case, the callback is triggered whether or not the key has already
@@ -1472,9 +1473,10 @@ func (conn *Connection) NewWatcher(key string, callback WatchCallback) (Watcher,
14721473
// asynchronous. We do not expect any response from a Tarantool instance
14731474
// That's why we can't just check the Tarantool response for an unsupported
14741475
// request error.
1475-
if !isFeatureInSlice(WatchersFeature, conn.opts.RequiredProtocolInfo.Features) {
1476+
if !isFeatureInSlice(iproto.IPROTO_FEATURE_WATCHERS,
1477+
conn.opts.RequiredProtocolInfo.Features) {
14761478
err := fmt.Errorf("the feature %s must be required by connection "+
1477-
"options to create a watcher", WatchersFeature)
1479+
"options to create a watcher", iproto.IPROTO_FEATURE_WATCHERS)
14781480
return nil, err
14791481
}
14801482

connection_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7+
"github.com/tarantool/go-iproto"
78

89
. "github.com/tarantool/go-tarantool/v2"
910
)
@@ -12,20 +13,20 @@ func TestOptsClonePreservesRequiredProtocolFeatures(t *testing.T) {
1213
original := Opts{
1314
RequiredProtocolInfo: ProtocolInfo{
1415
Version: ProtocolVersion(100),
15-
Features: []ProtocolFeature{ProtocolFeature(99), ProtocolFeature(100)},
16+
Features: []iproto.Feature{iproto.Feature(99), iproto.Feature(100)},
1617
},
1718
}
1819

1920
origCopy := original.Clone()
2021

21-
original.RequiredProtocolInfo.Features[1] = ProtocolFeature(98)
22+
original.RequiredProtocolInfo.Features[1] = iproto.Feature(98)
2223

2324
require.Equal(t,
2425
origCopy,
2526
Opts{
2627
RequiredProtocolInfo: ProtocolInfo{
2728
Version: ProtocolVersion(100),
28-
Features: []ProtocolFeature{ProtocolFeature(99), ProtocolFeature(100)},
29+
Features: []iproto.Feature{iproto.Feature(99), iproto.Feature(100)},
2930
},
3031
})
3132
}

dial_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
16+
"github.com/tarantool/go-iproto"
1617

1718
"github.com/tarantool/go-tarantool/v2"
1819
"github.com/tarantool/go-tarantool/v2/test_helpers"
@@ -72,8 +73,8 @@ func TestDialer_Dial_passedOpts(t *testing.T) {
7273
RequiredProtocol: tarantool.ProtocolInfo{
7374
Auth: tarantool.ChapSha1Auth,
7475
Version: 33,
75-
Features: []tarantool.ProtocolFeature{
76-
tarantool.ErrorExtensionFeature,
76+
Features: []iproto.Feature{
77+
iproto.IPROTO_FEATURE_ERROR_EXTENSION,
7778
},
7879
},
7980
Auth: tarantool.ChapSha1Auth,
@@ -302,8 +303,8 @@ func TestConn_ProtocolInfo(t *testing.T) {
302303
info := tarantool.ProtocolInfo{
303304
Auth: tarantool.ChapSha1Auth,
304305
Version: 33,
305-
Features: []tarantool.ProtocolFeature{
306-
tarantool.ErrorExtensionFeature,
306+
Features: []iproto.Feature{
307+
iproto.IPROTO_FEATURE_ERROR_EXTENSION,
307308
},
308309
}
309310
conn, dialer := dialIo(t, func(conn *mockIoConn) {

example_test.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/tarantool/go-iproto"
9+
810
"github.com/tarantool/go-tarantool/v2"
911
"github.com/tarantool/go-tarantool/v2/test_helpers"
1012
)
@@ -627,12 +629,12 @@ func ExampleProtocolVersion() {
627629
}
628630
// Output:
629631
// Connector client protocol version: 6
630-
// Connector client protocol feature: StreamsFeature
631-
// Connector client protocol feature: TransactionsFeature
632-
// Connector client protocol feature: ErrorExtensionFeature
633-
// Connector client protocol feature: WatchersFeature
634-
// Connector client protocol feature: PaginationFeature
635-
// Connector client protocol feature: WatchOnceFeature
632+
// Connector client protocol feature: IPROTO_FEATURE_STREAMS
633+
// Connector client protocol feature: IPROTO_FEATURE_TRANSACTIONS
634+
// Connector client protocol feature: IPROTO_FEATURE_ERROR_EXTENSION
635+
// Connector client protocol feature: IPROTO_FEATURE_WATCHERS
636+
// Connector client protocol feature: IPROTO_FEATURE_PAGINATION
637+
// Connector client protocol feature: IPROTO_FEATURE_WATCH_ONCE
636638
}
637639

638640
func getTestTxnOpts() tarantool.Opts {
@@ -641,9 +643,9 @@ func getTestTxnOpts() tarantool.Opts {
641643
// Assert that server supports expected protocol features
642644
txnOpts.RequiredProtocolInfo = tarantool.ProtocolInfo{
643645
Version: tarantool.ProtocolVersion(1),
644-
Features: []tarantool.ProtocolFeature{
645-
tarantool.StreamsFeature,
646-
tarantool.TransactionsFeature,
646+
Features: []iproto.Feature{
647+
iproto.IPROTO_FEATURE_STREAMS,
648+
iproto.IPROTO_FEATURE_TRANSACTIONS,
647649
},
648650
}
649651

@@ -1168,7 +1170,7 @@ func ExampleConnection_NewWatcher() {
11681170
Pass: "test",
11691171
// You need to require the feature to create a watcher.
11701172
RequiredProtocolInfo: tarantool.ProtocolInfo{
1171-
Features: []tarantool.ProtocolFeature{tarantool.WatchersFeature},
1173+
Features: []iproto.Feature{iproto.IPROTO_FEATURE_WATCHERS},
11721174
},
11731175
}
11741176
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)

pool/connection_pool.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"sync"
1818
"time"
1919

20+
"github.com/tarantool/go-iproto"
21+
2022
"github.com/tarantool/go-tarantool/v2"
2123
)
2224

@@ -911,8 +913,8 @@ func (p *ConnectionPool) NewPrepared(expr string, userMode Mode) (*tarantool.Pre
911913

912914
// NewWatcher creates a new Watcher object for the connection pool.
913915
//
914-
// You need to require WatchersFeature to use watchers, see examples for the
915-
// function.
916+
// You need to require IPROTO_FEATURE_WATCHERS to use watchers, see examples
917+
// for the function.
916918
//
917919
// The behavior is same as if Connection.NewWatcher() called for each
918920
// connection with a suitable role.
@@ -932,14 +934,14 @@ func (p *ConnectionPool) NewWatcher(key string,
932934
callback tarantool.WatchCallback, mode Mode) (tarantool.Watcher, error) {
933935
watchersRequired := false
934936
for _, feature := range p.connOpts.RequiredProtocolInfo.Features {
935-
if tarantool.WatchersFeature == feature {
937+
if iproto.IPROTO_FEATURE_WATCHERS == feature {
936938
watchersRequired = true
937939
break
938940
}
939941
}
940942
if !watchersRequired {
941-
return nil, errors.New("the feature WatchersFeature must be " +
942-
"required by connection options to create a watcher")
943+
return nil, errors.New("the feature IPROTO_FEATURE_WATCHERS must " +
944+
"be required by connection options to create a watcher")
943945
}
944946

945947
watcher := &poolWatcher{

pool/connection_pool_test.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
17+
"github.com/tarantool/go-iproto"
1718
"github.com/vmihailenco/msgpack/v5"
1819

1920
"github.com/tarantool/go-tarantool/v2"
@@ -2832,7 +2833,7 @@ func TestConnectionPool_NewWatcher_noWatchersFeature(t *testing.T) {
28322833
roles := []bool{true, false, false, true, true}
28332834

28342835
opts := connOpts.Clone()
2835-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{}
2836+
opts.RequiredProtocolInfo.Features = []iproto.Feature{}
28362837
err := test_helpers.SetClusterRO(servers, opts, roles)
28372838
require.Nilf(t, err, "fail to set roles for cluster")
28382839

@@ -2847,8 +2848,8 @@ func TestConnectionPool_NewWatcher_noWatchersFeature(t *testing.T) {
28472848
func(event tarantool.WatchEvent) {}, pool.ANY)
28482849
require.Nilf(t, watcher, "watcher must not be created")
28492850
require.NotNilf(t, err, "an error is expected")
2850-
expected := "the feature WatchersFeature must be required by connection " +
2851-
"options to create a watcher"
2851+
expected := "the feature IPROTO_FEATURE_WATCHERS must be required by " +
2852+
"connection options to create a watcher"
28522853
require.Equal(t, expected, err.Error())
28532854
}
28542855

@@ -2860,8 +2861,8 @@ func TestConnectionPool_NewWatcher_modes(t *testing.T) {
28602861
roles := []bool{true, false, false, true, true}
28612862

28622863
opts := connOpts.Clone()
2863-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
2864-
tarantool.WatchersFeature,
2864+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
2865+
iproto.IPROTO_FEATURE_WATCHERS,
28652866
}
28662867
err := test_helpers.SetClusterRO(servers, opts, roles)
28672868
require.Nilf(t, err, "fail to set roles for cluster")
@@ -2941,8 +2942,8 @@ func TestConnectionPool_NewWatcher_update(t *testing.T) {
29412942
roles := []bool{true, false, false, true, true}
29422943

29432944
opts := connOpts.Clone()
2944-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
2945-
tarantool.WatchersFeature,
2945+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
2946+
iproto.IPROTO_FEATURE_WATCHERS,
29462947
}
29472948
err := test_helpers.SetClusterRO(servers, opts, roles)
29482949
require.Nilf(t, err, "fail to set roles for cluster")
@@ -3030,8 +3031,8 @@ func TestWatcher_Unregister(t *testing.T) {
30303031
roles := []bool{true, false, false, true, true}
30313032

30323033
opts := connOpts.Clone()
3033-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
3034-
tarantool.WatchersFeature,
3034+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
3035+
iproto.IPROTO_FEATURE_WATCHERS,
30353036
}
30363037
err := test_helpers.SetClusterRO(servers, opts, roles)
30373038
require.Nilf(t, err, "fail to set roles for cluster")
@@ -3091,8 +3092,8 @@ func TestConnectionPool_NewWatcher_concurrent(t *testing.T) {
30913092
roles := []bool{true, false, false, true, true}
30923093

30933094
opts := connOpts.Clone()
3094-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
3095-
tarantool.WatchersFeature,
3095+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
3096+
iproto.IPROTO_FEATURE_WATCHERS,
30963097
}
30973098
err := test_helpers.SetClusterRO(servers, opts, roles)
30983099
require.Nilf(t, err, "fail to set roles for cluster")
@@ -3133,8 +3134,8 @@ func TestWatcher_Unregister_concurrent(t *testing.T) {
31333134
roles := []bool{true, false, false, true, true}
31343135

31353136
opts := connOpts.Clone()
3136-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
3137-
tarantool.WatchersFeature,
3137+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
3138+
iproto.IPROTO_FEATURE_WATCHERS,
31383139
}
31393140
err := test_helpers.SetClusterRO(servers, opts, roles)
31403141
require.Nilf(t, err, "fail to set roles for cluster")

pool/example_test.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package pool_test
22

33
import (
44
"fmt"
5+
"strings"
56
"time"
67

8+
"github.com/tarantool/go-iproto"
9+
710
"github.com/tarantool/go-tarantool/v2"
811
"github.com/tarantool/go-tarantool/v2/pool"
912
"github.com/tarantool/go-tarantool/v2/test_helpers"
@@ -92,8 +95,8 @@ func ExampleConnectionPool_NewWatcher() {
9295
const value = "bar"
9396

9497
opts := connOpts.Clone()
95-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
96-
tarantool.WatchersFeature,
98+
opts.RequiredProtocolInfo.Features = []iproto.Feature{
99+
iproto.IPROTO_FEATURE_WATCHERS,
97100
}
98101

99102
connPool, err := examplePool(testRoles, connOpts)
@@ -123,7 +126,7 @@ func ExampleConnectionPool_NewWatcher_noWatchersFeature() {
123126
const key = "foo"
124127

125128
opts := connOpts.Clone()
126-
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{}
129+
opts.RequiredProtocolInfo.Features = []iproto.Feature{}
127130

128131
connPool, err := examplePool(testRoles, connOpts)
129132
if err != nil {
@@ -134,10 +137,19 @@ func ExampleConnectionPool_NewWatcher_noWatchersFeature() {
134137
callback := func(event tarantool.WatchEvent) {}
135138
watcher, err := connPool.NewWatcher(key, callback, pool.ANY)
136139
fmt.Println(watcher)
137-
fmt.Println(err)
140+
if err != nil {
141+
// Need to split the error message into two lines to pass
142+
// golangci-lint.
143+
str := err.Error()
144+
fmt.Println(strings.Trim(str[:56], " "))
145+
fmt.Println(str[56:])
146+
} else {
147+
fmt.Println(err)
148+
}
138149
// Output:
139150
// <nil>
140-
// the feature WatchersFeature must be required by connection options to create a watcher
151+
// the feature IPROTO_FEATURE_WATCHERS must be required by
152+
// connection options to create a watcher
141153
}
142154

143155
func getTestTxnOpts() tarantool.Opts {
@@ -146,9 +158,9 @@ func getTestTxnOpts() tarantool.Opts {
146158
// Assert that server supports expected protocol features
147159
txnOpts.RequiredProtocolInfo = tarantool.ProtocolInfo{
148160
Version: tarantool.ProtocolVersion(1),
149-
Features: []tarantool.ProtocolFeature{
150-
tarantool.StreamsFeature,
151-
tarantool.TransactionsFeature,
161+
Features: []iproto.Feature{
162+
iproto.IPROTO_FEATURE_STREAMS,
163+
iproto.IPROTO_FEATURE_TRANSACTIONS,
152164
},
153165
}
154166

0 commit comments

Comments
 (0)