| 1 |
package tntengine |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/FZambia/tarantool" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
defaultConnectTimeout = time.Second |
| 13 |
defaultRequestTimeout = time.Second |
| 14 |
defaultReadTimeout = 5 * time.Second |
| 15 |
defaultWriteTimeout = time.Second |
| 16 |
) |
| 17 |
|
| 18 |
// Shard represents single Tarantool instance. |
| 19 |
type Shard struct { |
| 20 |
config ShardConfig |
| 21 |
subCh chan subRequest |
| 22 |
mc *MultiConnection |
| 23 |
} |
| 24 |
|
| 25 |
// ShardConfig allows providing options to connect to Tarantool. |
| 26 |
type ShardConfig struct { |
| 27 |
// Addresses of Tarantool instances. |
| 28 |
Addresses []string |
| 29 |
// User for auth. |
| 30 |
User string |
| 31 |
// Password for auth. |
| 32 |
Password string |
| 33 |
// ConnectionMode for shard. |
| 34 |
ConnectionMode ConnectionMode |
| 35 |
} |
| 36 |
|
| 37 |
func NewShard(c ShardConfig) (*Shard, error) { |
| 38 |
shard := &Shard{ |
| 39 |
config: c, |
| 40 |
subCh: make(chan subRequest), |
| 41 |
} |
| 42 |
|
| 43 |
mc, err := Connect(c.Addresses, tarantool.Opts{ |
| 44 |
ConnectTimeout: defaultConnectTimeout, |
| 45 |
RequestTimeout: defaultRequestTimeout, |
| 46 |
ReadTimeout: defaultReadTimeout, |
| 47 |
WriteTimeout: defaultWriteTimeout, |
| 48 |
ReconnectDelay: 50 * time.Millisecond, |
| 49 |
User: c.User, |
| 50 |
Password: c.Password, |
| 51 |
SkipSchema: true, |
| 52 |
}, MultiOpts{ |
| 53 |
ConnectionMode: c.ConnectionMode, |
| 54 |
}) |
| 55 |
if err != nil { |
| 56 |
return nil, fmt.Errorf("error creating req connection to %#v: %w", c.Addresses, err) |
| 57 |
} |
| 58 |
shard.mc = mc |
| 59 |
return shard, nil |
| 60 |
} |
| 61 |
|
| 62 |
func (s *Shard) Exec(request *tarantool.Request) (*tarantool.Response, error) { |
| 63 |
conn, err := s.mc.LeaderConn() |
| 64 |
if err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
return conn.Exec(request) |
| 68 |
} |
| 69 |
|
| 70 |
func (s *Shard) ExecTyped(request *tarantool.Request, result interface{}) error { |
| 71 |
conn, err := s.mc.LeaderConn() |
| 72 |
if err != nil { |
| 73 |
return err |
| 74 |
} |
| 75 |
return conn.ExecTyped(request, result) |
| 76 |
} |
| 77 |
|
| 78 |
func (s *Shard) pubSubConn() (*tarantool.Connection, func(), error) { |
| 79 |
conn, err := s.mc.NewLeaderConn(tarantool.Opts{ |
| 80 |
ConnectTimeout: defaultConnectTimeout, |
| 81 |
RequestTimeout: 5 * time.Second, |
| 82 |
ReadTimeout: defaultReadTimeout, |
| 83 |
WriteTimeout: defaultWriteTimeout, |
| 84 |
ReconnectDelay: 0, |
| 85 |
User: s.config.User, |
| 86 |
Password: s.config.Password, |
| 87 |
SkipSchema: true, |
| 88 |
}) |
| 89 |
if err != nil { |
| 90 |
return nil, nil, err |
| 91 |
} |
| 92 |
ctx, cancel := context.WithCancel(context.Background()) |
| 93 |
go func() { |
| 94 |
for { |
| 95 |
select { |
| 96 |
case <-ctx.Done(): |
| 97 |
return |
| 98 |
case <-time.After(time.Second): |
| 99 |
ok, err := s.mc.IsLeader(conn) |
| 100 |
if err != nil || !ok { |
| 101 |
s.mc.LeaderChanged() |
| 102 |
_ = conn.Close() |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
}() |
| 107 |
return conn, cancel, nil |
| 108 |
} |
| 109 |
|
| 110 |
// subRequest is an internal request to subscribe or unsubscribe from one or more channels |
| 111 |
type subRequest struct { |
| 112 |
channels []string |
| 113 |
subscribe bool |
| 114 |
err chan error |
| 115 |
} |
| 116 |
|
| 117 |
// newSubRequest creates a new request to subscribe or unsubscribe form a channel. |
| 118 |
func newSubRequest(chIDs []string, subscribe bool) subRequest { |
| 119 |
return subRequest{ |
| 120 |
channels: chIDs, |
| 121 |
subscribe: subscribe, |
| 122 |
err: make(chan error, 1), |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// done should only be called once for subRequest. |
| 127 |
func (sr *subRequest) done(err error) { |
| 128 |
sr.err <- err |
| 129 |
} |
| 130 |
|
| 131 |
func (sr *subRequest) result() error { |
| 132 |
return <-sr.err |
| 133 |
} |