Skip to content

Commit d774023

Browse files
committed
queue: add an example with connection_pool
The example demonstrates how to use the queue package with the connection_pool package. Closes #176
1 parent 6173a8d commit d774023

File tree

4 files changed

+182
-9
lines changed

4 files changed

+182
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1515
ConnectionPool (#178)
1616
- Execute, ExecuteTyped and ExecuteAsync methods to ConnectionPool (#176)
1717
- ConnectorAdapter type to use ConnectionPool as Connector interface (#176)
18+
- An example how to use queue and connection_pool subpackages together (#176)
1819

1920
### Changed
2021

queue/config.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,15 @@ box.cfg{
2323
box.schema.func.create('queue.identify')
2424
box.schema.func.create('queue.state')
2525
box.schema.func.create('queue.statistics')
26-
box.schema.user.grant('test', 'create', 'space')
27-
box.schema.user.grant('test', 'write', 'space', '_schema')
28-
box.schema.user.grant('test', 'write', 'space', '_space')
29-
box.schema.user.grant('test', 'read,write', 'space', '_space_sequence')
30-
box.schema.user.grant('test', 'write', 'space', '_index')
26+
box.schema.user.grant('test', 'create,read,write,drop', 'space')
3127
box.schema.user.grant('test', 'read, write', 'space', '_queue_session_ids')
3228
box.schema.user.grant('test', 'execute', 'universe')
3329
box.schema.user.grant('test', 'read,write', 'space', '_queue')
3430
box.schema.user.grant('test', 'read,write', 'space', '_schema')
31+
box.schema.user.grant('test', 'read,write', 'space', '_space_sequence')
3532
box.schema.user.grant('test', 'read,write', 'space', '_space')
3633
box.schema.user.grant('test', 'read,write', 'space', '_index')
37-
box.schema.user.grant('test', 'read,write', 'space', '_queue_consumers')
3834
box.schema.user.grant('test', 'read,write', 'space', '_priv')
39-
box.schema.user.grant('test', 'read,write', 'space', '_queue_taken_2')
40-
box.schema.user.grant('test', 'read,write', 'space', '_queue_shared_sessions')
4135
if box.space._trigger ~= nil then
4236
box.schema.user.grant('test', 'read', 'space', '_trigger')
4337
end
@@ -56,3 +50,5 @@ end)
5650
box.cfg{
5751
listen = os.getenv("TEST_TNT_LISTEN"),
5852
}
53+
54+
require('console').start()

queue/example_connection_pool_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package queue_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"sync"
7+
"time"
8+
9+
"github.com/google/uuid"
10+
"github.com/tarantool/go-tarantool"
11+
"github.com/tarantool/go-tarantool/connection_pool"
12+
"github.com/tarantool/go-tarantool/queue"
13+
)
14+
15+
type QueueConnectionListener struct {
16+
name string
17+
cfg queue.Cfg
18+
queue queue.Queue
19+
20+
uuid uuid.UUID
21+
registered bool
22+
err error
23+
mutex sync.Mutex
24+
done chan struct{}
25+
}
26+
27+
func NewQueueConnectionListener(name string, cfg queue.Cfg) *QueueConnectionListener {
28+
return &QueueConnectionListener{
29+
name: name,
30+
cfg: cfg,
31+
done: make(chan struct{}),
32+
}
33+
}
34+
35+
func (l *QueueConnectionListener) Added(conn *tarantool.Connection,
36+
role connection_pool.Role) {
37+
if role != connection_pool.MasterRole {
38+
return
39+
}
40+
41+
l.mutex.Lock()
42+
defer l.mutex.Unlock()
43+
44+
if l.err != nil {
45+
return
46+
}
47+
48+
l.queue = queue.New(conn, l.name)
49+
if !l.registered {
50+
opts := queue.CfgOpts{InReplicaset: true, Ttr: 60 * time.Second}
51+
if l.err = l.queue.Cfg(opts); l.err == nil {
52+
if l.uuid, l.err = l.queue.Identify(nil); l.err == nil {
53+
l.err = l.queue.Create(l.cfg)
54+
}
55+
}
56+
l.registered = l.err == nil
57+
close(l.done)
58+
} else {
59+
l.queue.Identify(&l.uuid)
60+
l.queue.Create(l.cfg)
61+
}
62+
}
63+
64+
func (l *QueueConnectionListener) Removed(conn *tarantool.Connection,
65+
role connection_pool.Role) {
66+
}
67+
68+
func (l *QueueConnectionListener) Updated(conn *tarantool.Connection,
69+
oldRole, newRole connection_pool.Role) {
70+
}
71+
72+
// Example demonstrates how to use the queue package with the connection_pool
73+
// package. First of all, you need to create a connection listener for the
74+
// a ConnectionPool object to process new connections from RW-instances.
75+
//
76+
// You need to registry a shared session UUID at a first master connection.
77+
// It needs to be used to re-identify as the shared session on new
78+
// RW-instances. See QueueConnectionListener.Added() implementation.
79+
//
80+
// After that, you need to create a ConnectorAdapter object with RW mode for
81+
// the ConnectionPool to send requests into RW-instances. This adapter can
82+
// be used to create a ready-to-work queue object.
83+
func Example_connectionPool() {
84+
servers := []string{
85+
"127.0.0.1:3014",
86+
"127.0.0.1:3015",
87+
"127.0.0.1:3016",
88+
}
89+
connOpts := tarantool.Opts{
90+
Timeout: 500 * time.Millisecond,
91+
User: "test",
92+
Pass: "test",
93+
}
94+
95+
cfg := queue.Cfg{
96+
Temporary: false,
97+
IfNotExists: true,
98+
Kind: queue.FIFO,
99+
Opts: queue.Opts{
100+
Ttl: 10 * time.Second,
101+
},
102+
}
103+
l := NewQueueConnectionListener("test_queue", cfg)
104+
poolOpts := connection_pool.OptsPool{
105+
CheckTimeout: 1 * time.Second,
106+
ConnectionListener: l,
107+
}
108+
connPool, err := connection_pool.ConnectWithOpts(servers, connOpts, poolOpts)
109+
if err != nil {
110+
log.Fatalf("unable to connect to the pool: %s", err)
111+
}
112+
113+
defer connPool.Close()
114+
115+
<-l.done
116+
117+
if l.err != nil {
118+
log.Fatalf("unable to identify in the pool: %s", l.err)
119+
}
120+
121+
rw := connection_pool.NewConnectorAdapter(connPool, connection_pool.RW)
122+
q := queue.New(rw, "test_queue")
123+
124+
fmt.Println("A Queue object is ready to work.")
125+
126+
if _, err = q.Put("test_data"); err != nil {
127+
log.Fatalf("unable to put data into the queue: %s", err)
128+
}
129+
130+
if task, err := q.Take(); err != nil {
131+
log.Fatalf("unable to take data from the queue: %s", err)
132+
} else {
133+
task.Ack()
134+
fmt.Println("data:", task.Data())
135+
}
136+
// Output:
137+
// A Queue object is ready to work.
138+
// data: test_data
139+
}

queue/queue_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import (
1414
)
1515

1616
var server = "127.0.0.1:3013"
17+
var serversPool = []string{
18+
"127.0.0.1:3014",
19+
"127.0.0.1:3015",
20+
"127.0.0.1:3016",
21+
}
22+
23+
var instances []test_helpers.TarantoolInstance
24+
1725
var opts = Opts{
1826
Timeout: 2500 * time.Millisecond,
1927
User: "test",
@@ -899,12 +907,41 @@ func runTestMain(m *testing.M) int {
899907
ConnectRetry: 3,
900908
RetryTimeout: 500 * time.Millisecond,
901909
})
902-
defer test_helpers.StopTarantoolWithCleanup(inst)
903910

904911
if err != nil {
905912
log.Fatalf("Failed to prepare test tarantool: %s", err)
906913
}
907914

915+
defer test_helpers.StopTarantoolWithCleanup(inst)
916+
917+
workDirs := []string{"work_dir1", "work_dir2", "work_dir3"}
918+
poolOpts := test_helpers.StartOpts{
919+
InitScript: "config.lua",
920+
User: opts.User,
921+
Pass: opts.Pass,
922+
WaitStart: 100 * time.Millisecond,
923+
ConnectRetry: 3,
924+
RetryTimeout: 500 * time.Millisecond,
925+
}
926+
instances, err = test_helpers.StartTarantoolInstances(serversPool, workDirs, poolOpts)
927+
928+
if err != nil {
929+
log.Fatalf("Failed to prepare test tarantool pool: %s", err)
930+
}
931+
932+
defer test_helpers.StopTarantoolInstances(instances)
933+
934+
roles := []bool{true, false, false}
935+
connOpts := Opts{
936+
Timeout: 500 * time.Millisecond,
937+
User: "test",
938+
Pass: "test",
939+
}
940+
err = test_helpers.SetClusterRO(serversPool, connOpts, roles)
941+
942+
if err != nil {
943+
log.Fatalf("Failed to set roles in tarantool pool: %s", err)
944+
}
908945
return m.Run()
909946
}
910947

0 commit comments

Comments
 (0)