|
| 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 QueueConnectionHandler struct { |
| 16 | + name string |
| 17 | + cfg queue.Cfg |
| 18 | + |
| 19 | + uuid uuid.UUID |
| 20 | + registered bool |
| 21 | + err error |
| 22 | + registerMutex sync.Mutex |
| 23 | + done chan struct{} |
| 24 | +} |
| 25 | + |
| 26 | +var _ connection_pool.ConnectionHandler = &QueueConnectionHandler{} |
| 27 | + |
| 28 | +func NewQueueConnectionHandler(name string, cfg queue.Cfg) *QueueConnectionHandler { |
| 29 | + return &QueueConnectionHandler{ |
| 30 | + name: name, |
| 31 | + cfg: cfg, |
| 32 | + done: make(chan struct{}), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (h *QueueConnectionHandler) Discovered(conn *tarantool.Connection, |
| 37 | + role connection_pool.Role) error { |
| 38 | + // The queue only works with a master instance. |
| 39 | + if role != connection_pool.MasterRole { |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + // We need to register a shared session only once. |
| 44 | + h.registerMutex.Lock() |
| 45 | + |
| 46 | + if h.err != nil { |
| 47 | + h.registerMutex.Unlock() |
| 48 | + return h.err |
| 49 | + } |
| 50 | + |
| 51 | + q := queue.New(conn, h.name) |
| 52 | + if !h.registered { |
| 53 | + opts := queue.CfgOpts{InReplicaset: true, Ttr: 60 * time.Second} |
| 54 | + if h.err = q.Cfg(opts); h.err == nil { |
| 55 | + if h.uuid, h.err = q.Identify(nil); h.err == nil { |
| 56 | + h.err = q.Create(h.cfg) |
| 57 | + } |
| 58 | + } |
| 59 | + h.registered = h.err == nil |
| 60 | + close(h.done) |
| 61 | + h.registerMutex.Unlock() |
| 62 | + return h.err |
| 63 | + } else { |
| 64 | + // We already registered. We don't need the lock any more. |
| 65 | + h.registerMutex.Unlock() |
| 66 | + if _, err := q.Identify(&h.uuid); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + if err := q.Create(h.cfg); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (h *QueueConnectionHandler) Deactivated(conn *tarantool.Connection, |
| 77 | + role connection_pool.Role) error { |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// Example demonstrates how to use the queue package with the connection_pool |
| 82 | +// package. First of all, you need to create a connection handler for the |
| 83 | +// a ConnectionPool object to process new connections from RW-instances. |
| 84 | +// |
| 85 | +// You need to register a shared session UUID at a first master connection. |
| 86 | +// It needs to be used to re-identify as the shared session on new |
| 87 | +// RW-instances. See QueueConnectionHandler.Discovered() implementation. |
| 88 | +// |
| 89 | +// After that, you need to create a ConnectorAdapter object with RW mode for |
| 90 | +// the ConnectionPool to send requests into RW-instances. This adapter can |
| 91 | +// be used to create a ready-to-work queue object. |
| 92 | +func Example_connectionPool() { |
| 93 | + servers := []string{ |
| 94 | + "127.0.0.1:3014", |
| 95 | + "127.0.0.1:3015", |
| 96 | + "127.0.0.1:3016", |
| 97 | + } |
| 98 | + connOpts := tarantool.Opts{ |
| 99 | + Timeout: 500 * time.Millisecond, |
| 100 | + User: "test", |
| 101 | + Pass: "test", |
| 102 | + } |
| 103 | + |
| 104 | + cfg := queue.Cfg{ |
| 105 | + Temporary: false, |
| 106 | + IfNotExists: true, |
| 107 | + Kind: queue.FIFO, |
| 108 | + Opts: queue.Opts{ |
| 109 | + Ttl: 10 * time.Second, |
| 110 | + }, |
| 111 | + } |
| 112 | + h := NewQueueConnectionHandler("test_queue", cfg) |
| 113 | + poolOpts := connection_pool.OptsPool{ |
| 114 | + CheckTimeout: 1 * time.Second, |
| 115 | + ConnectionHandler: h, |
| 116 | + } |
| 117 | + connPool, err := connection_pool.ConnectWithOpts(servers, connOpts, poolOpts) |
| 118 | + if err != nil { |
| 119 | + log.Fatalf("unable to connect to the pool: %s", err) |
| 120 | + } |
| 121 | + |
| 122 | + defer connPool.Close() |
| 123 | + |
| 124 | + <-h.done |
| 125 | + |
| 126 | + if h.err != nil { |
| 127 | + log.Fatalf("unable to identify in the pool: %s", h.err) |
| 128 | + } |
| 129 | + |
| 130 | + rw := connection_pool.NewConnectorAdapter(connPool, connection_pool.RW) |
| 131 | + q := queue.New(rw, "test_queue") |
| 132 | + |
| 133 | + fmt.Println("A Queue object is ready to work.") |
| 134 | + |
| 135 | + if _, err = q.Put("test_data"); err != nil { |
| 136 | + log.Fatalf("unable to put data into the queue: %s", err) |
| 137 | + } |
| 138 | + |
| 139 | + if task, err := q.Take(); err != nil { |
| 140 | + log.Fatalf("unable to take data from the queue: %s", err) |
| 141 | + } else { |
| 142 | + task.Ack() |
| 143 | + fmt.Println("data:", task.Data()) |
| 144 | + } |
| 145 | + // Output: |
| 146 | + // A Queue object is ready to work. |
| 147 | + // data: test_data |
| 148 | +} |
0 commit comments