Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergey
committed
Nov 8, 2018
0 parents
commit 740b8fe
Showing
11 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/docs/ | ||
/lib/ | ||
/bin/ | ||
/.shards/ | ||
*.dwarf | ||
*.retry | ||
|
||
# Mac OS | ||
.DS_Store | ||
|
||
# Stat log | ||
*.snap | ||
*.xlog | ||
*.log | ||
*.pid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
language: crystal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 'Konstantin Makarchev' | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Сord | ||
|
||
Crystal shard for Tarantool with auto reconnect and connections pool based on original shard for Redis by [kostya/redisoid](https://github.com/kostya/redisoid) but slightly adapted. Also shard uses [ysbaddaden/pool](https://github.com/ysbaddaden/pool) and [vladfaust/tarantool.cr](https://github.com/vladfaust/tarantool.cr) of course. | ||
|
||
## Installation | ||
|
||
Add this to your application's `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
cord: | ||
github: creadone/cord | ||
``` | ||
|
||
## Usage | ||
|
||
```crystal | ||
require "cord" | ||
tarantool = Сord::Connection.new(host: "localhost", port: 6379, pool: 50) | ||
tarantool.ping #=> 00:00:00.000159976 | ||
``` | ||
|
||
## Tests | ||
|
||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: 1.0 | ||
shards: | ||
msgpack: | ||
github: crystal-community/msgpack-crystal | ||
version: 0.10.0 | ||
|
||
pool: | ||
github: ysbaddaden/pool | ||
version: 0.2.3 | ||
|
||
tarantool: | ||
github: vladfaust/tarantool.cr | ||
version: 0.1.1 | ||
|
||
time_format: | ||
github: vladfaust/time_format.cr | ||
version: 0.1.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: cord | ||
|
||
version: 0.0.1 | ||
crystal: 0.26.1 | ||
|
||
authors: | ||
- Konstantin Makarchev kostya27@gmail.com | ||
- Sergey Fedorov creadone@gmail.com | ||
|
||
description: | | ||
Crystal shard for Tarantool with auto reconnect and connections pool based on original shard for Redis by kostya/redisoid but slightly adapted. | ||
dependencies: | ||
pool: | ||
github: ysbaddaden/pool | ||
tarantool: | ||
github: vladfaust/tarantool.cr | ||
version: ~> 0.1.0 | ||
|
||
license: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env tarantool | ||
|
||
-- Expose Tarantool | ||
box.cfg { | ||
listen = 3301, | ||
background = false, | ||
custom_proc_title = 'cord', | ||
} | ||
|
||
-- Initial setup | ||
box.once("bootstrap", function() | ||
local links = box.schema.create_space('test') | ||
box.schema.user.create('web', {password = '123456'}) | ||
box.schema.user.grant('web', 'read,write,execute', 'universe') | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "./spec_helper" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "spec" | ||
require "../src/сord" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "pool/connection" | ||
require "./reconnect.cr" | ||
|
||
module Сord | ||
VERSION = "0.0.1" | ||
|
||
class Connection | ||
@cp : ConnectionPool(Сord::Reconnect) | ||
|
||
def initialize(@host : String = "localhost", | ||
@port : Int32 = 6379, | ||
@user : String? = nil, | ||
@password : String? = nil, | ||
@timeout : Time::Span = 1.second, | ||
@pool : Int32 = 10) | ||
@cp = ConnectionPool(Сord::Reconnect).new(capacity: @pool) do | ||
Сord::Reconnect.new(host: @host, port: @port, user: @user, password: @password) | ||
end | ||
end | ||
|
||
macro method_missing(call) | ||
@cp.connection do |cn| | ||
return cn.{{call}} | ||
end | ||
end | ||
|
||
def pool_size | ||
@cp.size | ||
end | ||
|
||
def pool_pending | ||
@cp.pending | ||
end | ||
|
||
def stats | ||
{capacity: @pool, available: pool_pending, used: pool_size} | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "tarantool" | ||
|
||
module Сord | ||
class Reconnect | ||
|
||
@client : Tarantool::Connection | ||
|
||
def initialize(@host = "localhost", @port = 6379, @user : String? = nil, @password : String? = nil) | ||
@client = Tarantool::Connection.new(@host, @port, @user, @password) | ||
end | ||
|
||
def reconnect! | ||
@client.close rescue nil | ||
@client = Tarantool::Connection.new(@host, @port, @user, @password) | ||
end | ||
|
||
macro method_missing(call) | ||
safe_call { @client.{{call}} } | ||
end | ||
|
||
private def safe_call | ||
yield | ||
rescue | ||
reconnect! | ||
yield | ||
end | ||
|
||
end | ||
end |