Skip to content

Commit

Permalink
fix: refactor redis usage in cleaner
Browse files Browse the repository at this point in the history
Closes #47
  • Loading branch information
palkan authored Dec 13, 2024
1 parent fae6f96 commit c5d10d2
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions lib/graphql/anycable/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,51 @@ def clean_channels
return unless config.subscription_expiration_seconds
return unless config.use_redis_object_on_cleanup

redis.scan_each(match: "#{redis_key(adapter::CHANNEL_PREFIX)}*") do |key|
idle = redis.object("IDLETIME", key)
next if idle&.<= config.subscription_expiration_seconds
AnyCable.with_redis do |redis|
redis.scan_each(match: "#{redis_key(adapter::CHANNEL_PREFIX)}*") do |key|
idle = redis.object("IDLETIME", key)
next if idle&.<= config.subscription_expiration_seconds

redis.del(key)
redis.del(key)
end
end
end

def clean_subscriptions
return unless config.subscription_expiration_seconds
return unless config.use_redis_object_on_cleanup

redis.scan_each(match: "#{redis_key(adapter::SUBSCRIPTION_PREFIX)}*") do |key|
idle = redis.object("IDLETIME", key)
next if idle&.<= config.subscription_expiration_seconds
AnyCable.with_redis do |redis|
redis.scan_each(match: "#{redis_key(adapter::SUBSCRIPTION_PREFIX)}*") do |key|
idle = redis.object("IDLETIME", key)
next if idle&.<= config.subscription_expiration_seconds

redis.del(key)
end
redis.del(key)
end
end
end

def clean_fingerprint_subscriptions
redis.scan_each(match: "#{redis_key(adapter::SUBSCRIPTIONS_PREFIX)}*") do |key|
redis.smembers(key).each do |subscription_id|
next if redis.exists?(redis_key(adapter::SUBSCRIPTION_PREFIX) + subscription_id)
AnyCable.with_redis do |redis|
redis.scan_each(match: "#{redis_key(adapter::SUBSCRIPTIONS_PREFIX)}*") do |key|
redis.smembers(key).each do |subscription_id|
next if redis.exists?(redis_key(adapter::SUBSCRIPTION_PREFIX) + subscription_id)

redis.srem(key, subscription_id)
redis.srem(key, subscription_id)
end
end
end
end

def clean_topic_fingerprints
redis.scan_each(match: "#{redis_key(adapter::FINGERPRINTS_PREFIX)}*") do |key|
redis.zremrangebyscore(key, "-inf", "0")
redis.zrange(key, 0, -1).each do |fingerprint|
next if redis.exists?(redis_key(adapter::SUBSCRIPTIONS_PREFIX) + fingerprint)

redis.zrem(key, fingerprint)
AnyCable.with_redis do |redis|
redis.scan_each(match: "#{redis_key(adapter::FINGERPRINTS_PREFIX)}*") do |key|
redis.zremrangebyscore(key, "-inf", "0")
redis.zrange(key, 0, -1).each do |fingerprint|
next if redis.exists?(redis_key(adapter::SUBSCRIPTIONS_PREFIX) + fingerprint)

redis.zrem(key, fingerprint)
end
end
end
end
Expand All @@ -63,10 +71,6 @@ def adapter
GraphQL::Subscriptions::AnyCableSubscriptions
end

def redis
GraphQL::AnyCable.redis
end

def config
GraphQL::AnyCable.config
end
Expand Down

0 comments on commit c5d10d2

Please sign in to comment.