Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey committed Nov 8, 2018
0 parents commit 740b8fe
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf
*.retry

# Mac OS
.DS_Store

# Stat log
*.snap
*.xlog
*.log
*.pid
1 change: 1 addition & 0 deletions .travis.yml
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
@@ -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.
26 changes: 26 additions & 0 deletions README.md
@@ -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
18 changes: 18 additions & 0 deletions shard.lock
@@ -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

20 changes: 20 additions & 0 deletions shard.yml
@@ -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
15 changes: 15 additions & 0 deletions spec/cord.lua
@@ -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)
1 change: 1 addition & 0 deletions spec/cord_spec.cr
@@ -0,0 +1 @@
require "./spec_helper"
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
@@ -0,0 +1,2 @@
require "spec"
require "../src/сord"
40 changes: 40 additions & 0 deletions src/cord.cr
@@ -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
29 changes: 29 additions & 0 deletions src/reconnect.cr
@@ -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

0 comments on commit 740b8fe

Please sign in to comment.