-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit d6dd7ff
Showing
11 changed files
with
365 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,9 @@ | ||
root = true | ||
|
||
[*.cr] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true |
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,54 @@ | ||
name: Cable.cr RedisBackend CI | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: "*" | ||
|
||
jobs: | ||
specs: | ||
env: | ||
CABLE_BACKEND_URL: redis://redis:6379 | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
shard_file: | ||
- shard.yml | ||
crystal_version: | ||
- 1.10.0 | ||
- latest | ||
experimental: | ||
- false | ||
include: | ||
- crystal_version: nightly | ||
experimental: true | ||
runs-on: ubuntu-latest | ||
container: crystallang/crystal:${{ matrix.crystal_version }} | ||
continue-on-error: ${{ matrix.experimental }} | ||
services: | ||
redis: | ||
image: redis | ||
ports: | ||
- 6379:6379 | ||
# Set health checks to wait until redis has started | ||
options: >- | ||
--health-cmd "redis-cli ping" | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Cache Crystal | ||
uses: actions/cache@v4 | ||
with: | ||
path: ~/.cache/crystal | ||
key: ${{ runner.os }}-crystal | ||
- name: Install shards | ||
run: shards install | ||
- name: Format | ||
run: crystal tool format --check | ||
- name: Lint | ||
run: ./bin/ameba | ||
- name: Run tests | ||
run: crystal spec |
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,9 @@ | ||
/docs/ | ||
/lib/ | ||
/bin/ | ||
/.shards/ | ||
*.dwarf | ||
|
||
# Libraries don't need dependency lock | ||
# Dependencies will be locked in applications that use them | ||
/shard.lock |
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 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Jeremy Woertink <jeremywoertink@gmail.com> | ||
|
||
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,51 @@ | ||
# cable-redis | ||
|
||
This is a [Cable.cr](https://github.com/cable-cr/cable) backend extension for [jgaskins/redis](https://github.com/jgaskins/redis). | ||
|
||
## Installation | ||
|
||
1. Add the dependency to your `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
cable: | ||
github: cable-cr/cable | ||
cable-redis: | ||
github: cable-cr/cable-redis | ||
``` | ||
2. Run `shards install` | ||
|
||
## Usage | ||
|
||
```crystal | ||
require "cable" | ||
require "cable-redis" | ||
Cable.configure do |settings| | ||
settings.url = ENV.fetch("CABLE_BACKEND_URL", "redis://localhost:6379") | ||
settings.backend_class = Cable::RedisBackend | ||
# ... all other Cable config settings | ||
end | ||
``` | ||
|
||
## Development | ||
|
||
1. Make the update | ||
2. Add a spec and run `crystal spec` | ||
3. Format it `crystal tool format spec/ src/` | ||
4. Ameba `./bin/ameba` | ||
5. Commit it | ||
6. GO TO 1 | ||
|
||
## Contributing | ||
|
||
1. Fork it (<https://github.com/cable-cr/cable-redis/fork>) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request | ||
|
||
## Contributors | ||
|
||
- [Jeremy Woertink](https://github.com/jwoertink) - creator and maintainer |
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,24 @@ | ||
name: cable-redis | ||
version: 0.1.0 | ||
|
||
authors: | ||
- Jeremy Woertink <jeremywoertink@gmail.com> | ||
|
||
crystal: '>= 1.10.0' | ||
|
||
dependencies: | ||
redis: | ||
github: jgaskins/redis | ||
version: ~> 0.6 | ||
|
||
development_dependencies: | ||
cable: | ||
github: cable-cr/cable | ||
branch: master | ||
# version: ~> 0.4 | ||
|
||
ameba: | ||
github: crystal-ameba/ameba | ||
version: ~> 1.5.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,56 @@ | ||
require "./spec_helper" | ||
|
||
include RequestHelpers | ||
|
||
describe Cable::RedisBackend do | ||
it "connects and publishes through Redis" do | ||
connect do |connection, socket| | ||
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json) | ||
sleep 0.1 | ||
json_message = %({"foo": "bar"}) | ||
Cable.server.publish(channel: "chat_1", message: json_message) | ||
sleep 0.1 | ||
|
||
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json) | ||
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => JSON.parse(%({"foo": "bar"}))}.to_json) | ||
end | ||
end | ||
end | ||
|
||
private class ChatChannel < Cable::Channel | ||
def subscribed | ||
stream_from "chat_#{params["room"]}" | ||
end | ||
|
||
def receive(message) | ||
end | ||
|
||
def perform(action, action_params) | ||
end | ||
|
||
def unsubscribed | ||
end | ||
end | ||
|
||
private class ConnectionTest < Cable::Connection | ||
identified_by :identifier | ||
|
||
def connect | ||
if tk = token | ||
self.identifier = tk | ||
end | ||
end | ||
|
||
def broadcast_to(channel, message) | ||
end | ||
end | ||
|
||
def connect(&) | ||
socket = DummySocket.new(IO::Memory.new) | ||
connection = ConnectionTest.new(builds_request(token: "test-token"), socket) | ||
|
||
yield connection, socket | ||
|
||
connection.close | ||
socket.close | ||
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,16 @@ | ||
require "spec" | ||
require "../src/cable-redis" | ||
require "./support/*" | ||
|
||
Cable.configure do |settings| | ||
settings.route = "/updates" | ||
settings.token = "test_token" | ||
settings.url = ENV.fetch("CABLE_BACKEND_URL", "redis://localhost:6379") | ||
settings.backend_class = Cable::RedisBackend | ||
settings.backend_ping_interval = 2.seconds | ||
settings.restart_error_allowance = 2 | ||
end | ||
|
||
Spec.before_each do | ||
Cable.restart | ||
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,8 @@ | ||
class DummySocket < HTTP::WebSocket | ||
getter messages : Array(String) = Array(String).new | ||
|
||
def send(message) | ||
return if closed? | ||
@messages << message | ||
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,25 @@ | ||
require "http" | ||
|
||
module RequestHelpers | ||
def builds_request(token : String) : HTTP::Request | ||
headers = HTTP::Headers{ | ||
"Upgrade" => "websocket", | ||
"Connection" => "Upgrade", | ||
"Sec-WebSocket-Key" => "OqColdEJm3i9e/EqMxnxZw==", | ||
"Sec-WebSocket-Protocol" => "actioncable-v1-json, actioncable-unsupported", | ||
"Sec-WebSocket-Version" => "13", | ||
} | ||
HTTP::Request.new("GET", "#{Cable.settings.route}?test_token=#{token}", headers) | ||
end | ||
|
||
def builds_request(token : Nil) : HTTP::Request | ||
headers = HTTP::Headers{ | ||
"Upgrade" => "websocket", | ||
"Connection" => "Upgrade", | ||
"Sec-WebSocket-Key" => "OqColdEJm3i9e/EqMxnxZw==", | ||
"Sec-WebSocket-Protocol" => "actioncable-v1-json, actioncable-unsupported", | ||
"Sec-WebSocket-Version" => "13", | ||
} | ||
HTTP::Request.new("GET", Cable.settings.route, headers) | ||
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,92 @@ | ||
require "cable" | ||
require "redis" | ||
|
||
module Cable | ||
class RedisBackend < Cable::BackendCore | ||
VERSION = "0.1.0" | ||
|
||
register "redis" # redis:// | ||
register "rediss" # rediss:// | ||
|
||
# connection management | ||
getter redis_subscribe : Redis::Connection = Redis::Connection.new(URI.parse(Cable.settings.url)) | ||
getter redis_publish : Redis::Client = Redis::Client.new(URI.parse(Cable.settings.url)) | ||
|
||
# connection management | ||
def subscribe_connection : Redis::Connection | ||
redis_subscribe | ||
end | ||
|
||
def publish_connection : Redis::Client | ||
redis_publish | ||
end | ||
|
||
def close_subscribe_connection | ||
return if redis_subscribe.nil? | ||
|
||
redis_subscribe.unsubscribe | ||
redis_subscribe.close | ||
end | ||
|
||
def close_publish_connection | ||
return if redis_publish.nil? | ||
|
||
redis_publish.close | ||
end | ||
|
||
# internal pub/sub | ||
def open_subscribe_connection(channel) | ||
return if redis_subscribe.nil? | ||
|
||
redis_subscribe.subscribe(channel) do |subscription| | ||
subscription.on_message do |sub_channel, message| | ||
if sub_channel == Cable::INTERNAL[:channel] && message == "ping" | ||
Cable::Logger.debug { "Cable::Server#subscribe -> PONG" } | ||
elsif sub_channel == Cable::INTERNAL[:channel] && message == "debug" | ||
Cable.server.debug | ||
else | ||
Cable.server.fiber_channel.send({sub_channel, message}) | ||
Cable::Logger.debug { "Cable::Server#subscribe channel:#{sub_channel} message:#{message}" } | ||
end | ||
end | ||
end | ||
end | ||
|
||
# external pub/sub | ||
def publish_message(stream_identifier : String, message : String) | ||
return if redis_subscribe.nil? | ||
|
||
redis_publish.publish(stream_identifier, message) | ||
end | ||
|
||
# channel management | ||
def subscribe(stream_identifier : String) | ||
return if redis_subscribe.nil? | ||
|
||
redis_subscribe.subscribe(stream_identifier) | ||
end | ||
|
||
def unsubscribe(stream_identifier : String) | ||
return if redis_subscribe.nil? | ||
|
||
redis_subscribe.unsubscribe(stream_identifier) | ||
end | ||
|
||
# ping/pong | ||
|
||
# since @server.redis_subscribe connection is called on a block loop | ||
# we basically cannot call ping outside of the block | ||
# instead, we just spin up another new redis connection | ||
# then publish a special channel/message broadcast | ||
# the @server.redis_subscribe picks up this special combination | ||
# and calls ping on the block loop for us | ||
def ping_subscribe_connection | ||
Cable.server.publish(Cable::INTERNAL[:channel], "ping") | ||
end | ||
|
||
def ping_publish_connection | ||
result = redis_publish.run({"ping"}) | ||
Cable::Logger.debug { "Cable::BackendPinger.ping_publish_connection -> #{result}" } | ||
end | ||
end | ||
end |