{"id":16660438,"url":"https://github.com/killme2008/carmine-sentinel","last_synced_at":"2025-03-21T16:32:37.345Z","repository":{"id":62433794,"uuid":"70790382","full_name":"killme2008/carmine-sentinel","owner":"killme2008","description":"A Clojure library designed to connect redis by sentinel, make carmine to support sentinel.","archived":false,"fork":false,"pushed_at":"2020-09-14T09:23:31.000Z","size":118,"stargazers_count":14,"open_issues_count":5,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T03:13:43.975Z","etag":null,"topics":["carmine","clojure","redis-sentinel","sentinel"],"latest_commit_sha":null,"homepage":"","language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/killme2008.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-13T09:23:24.000Z","updated_at":"2021-03-10T20:43:27.000Z","dependencies_parsed_at":"2022-11-01T21:01:48.377Z","dependency_job_id":null,"html_url":"https://github.com/killme2008/carmine-sentinel","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Fcarmine-sentinel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Fcarmine-sentinel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Fcarmine-sentinel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Fcarmine-sentinel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/killme2008","download_url":"https://codeload.github.com/killme2008/carmine-sentinel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244829605,"owners_count":20517340,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["carmine","clojure","redis-sentinel","sentinel"],"created_at":"2024-10-12T10:29:16.811Z","updated_at":"2025-03-21T16:32:36.985Z","avatar_url":"https://github.com/killme2008.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# carmine-sentinel\n\nA Clojure library designed to connect redis by [sentinel](redis.io/topics/sentinel), make [carmine](https://github.com/ptaoussanis/carmine) to support [sentinel](redis.io/topics/sentinel)。\n\n## Usage\n\n```clojure\n[net.fnil/carmine-sentinel \"1.0.0\"]\n```\n\n**Carmine-sentinel require carmine version must be `\u003e= 2.15.0`right now.**\n\nFirst, require carmine and carmine-sentinel:\n\n```clojure\n(ns my-app\n  (:require [taoensso.carmine :as car]\n            [carmine-sentinel.core :as cs :refer [set-sentinel-groups!]]))\n```\n\nThe only difference compares with carmine is that we will use `carmine-sentinel.core/wcar` to replace `taoensso.carmine/wcar` and add a new function `set-sentinel-groups!`.\n\nSecond, configure sentinel groups:\n\n```clojure\n(set-sentinel-groups!\n  {:group1\n   {:specs [{:host \"127.0.0.1\" :port 5000} {:host \"127.0.0.1\" :port 5001} {:host \"127.0.0.1\" :port 5002}]\n    :pool  {\u003copts\u003e} }})\n```\n\nThere is only one group named `:group1` above, and it has three sentinel instances (port from 5000 to 5002 at 127.0.0.1). Optional, you can set the pool option values and add more sentinel groups.\n\nYou can use `add-sentinel-groups!` and `remove-sentinel-group!` to manage the configuration all the time.\n\nNext, we can define the `wcar*`:\n\n```clojure\n(def server1-conn {:pool {\u003copts\u003e} :spec {} :sentinel-group :group1 :master-name \"mymaster\"})\n(defmacro wcar* [\u0026 body] `(cs/wcar server1-conn ~@body))\n```\n\nThe spec in `server1-conn` can be left empty or contain general configurations, such as password or ssl function, and there are two new options in server1-conn:\n\n* `:sentinel-group` Which sentinel instances group to resolve master addr.Here is `:group1`.\n* `:master-name` Master name configured in that sentinel group.Here is `mymaster`.\n\nThe `spec` in server1-conn will be merged to resolved master spec at runtime.\nSo you can set `:password`,`:timeout-ms` etc. other options in it.\n\nAlso, you can define many `wcar*`-like macros to use other sentinel group and master name.\n\nAt last, you can use `wcar*` as the same in carmine.\n\n```clojure\n(wcar* (car/set \"key\" 1))\n(wcar* (car/get \"key\"))\n```\n\nIf you want to bypass sentinel and connect to redis server directly such as doing testing on your local machine, you can ignore `sentinel-group` and `master-name`, just provide redis server connection spec you want connect to directly like this:\n\n```clojure\n(def server1-conn {:pool {\u003copts\u003e} :spec {:host \"127.0.0.1\" :port 6379}})\n(defmacro wcar* [\u0026 body] `(cs/wcar server1-conn ~@body))\n```\n\n### Authentication\n\nDue to a bug fix in version `1.0.0` authentication requires slight modifications to the settings.\nNotice both the server connection and sentinel group require passing the authentication token:\n\n```clojure\n(let [token \"foobar\"\n          host \"127.0.0.1\"]\n\n      (def server1-conn\n        {:pool {}\n         :spec {:password token}\n         :sentinel-group :group1\n         :master-name \"mymaster\"})\n\n      (set-sentinel-groups!\n       {:group1\n        {:specs [{:host host :port 5000 :password token}\n                 {:host host :port 5001 :password token}\n                 {:host host :port 5002 :password token}]}}))\n```\n\n`wcar*` is defined normally\n\n## Pub/Sub\n\nPlease use `carmine-sentinel.core/with-new-pubsub-listener` to replace `taoensso.carmine/with-new-pubsub-listener` and provide `master-name`, `sentinel-group` to take advantage of sentinel cluster like this:\n\n```clojure\n(def server1-conn {:sentinel-group :group1 :master-name \"mymaster\"})\n\n;;Pub/Sub\n(def listener\n  (with-new-pubsub-listener server1-conn\n    {\"foobar\" (fn f1 [msg] (println \"Channel match: \" msg))\n     \"foo*\"   (fn f2 [msg] (println \"Pattern match: \" msg))}\n   (car/subscribe  \"foobar\" \"foobaz\")\n   (car/psubscribe \"foo*\")))\n```\n\n`carmine-sentinel.core/with-new-pubsub-listener` also support bypass sentinel and connect to redis server directly. You just need to provide the redis server spec you want connect to while ignore `sentinel-group` and `master-name`:\n\n```clojure\n(def server1-conn {:spec {:host \"127.0.0.1\" :port 6379}})\n\n;;Pub/Sub\n(def listener\n  (with-new-pubsub-listener server1-conn\n    {\"foobar\" (fn f1 [msg] (println \"Channel match: \" msg))\n     \"foo*\"   (fn f2 [msg] (println \"Pattern match: \" msg))}\n   (car/subscribe  \"foobar\" \"foobaz\")\n   (car/psubscribe \"foo*\")))\n```\n\n## MessageQueue and Lock\n\nYou have to invoke `update-conn-spec` before using other APIs in carmine:\n\n```clojure\n(def server1-conn {:pool {\u003copts\u003e} :spec {} :sentinel-group :group1 :master-name \"mymaster\"})\n\n\n;;Message queue\n(def my-worker\n  (car-mq/worker (cs/update-conn-spec server1-conn) \"my-queue\"\n   {:handler (fn [{:keys [message attempt]}]\n               (println \"Received\" message)\n               {:status :success})}))\n\n\n;;;Lock\n(locks/with-lock (cs/update-conn-spec server1-conn) \"my-lock\"\n  1000 ; Time to hold lock\n  500  ; Time to wait (block) for lock acquisition\n  (println \"This was printed under lock!\"))\n```\n\n## Reading From Slaves\n\nIf you want to read data from slave, you can set `prefer-slave?` to be true:\n\n```clojure\n(def slave-conn {:pool {\u003copts\u003e} :spec {}\n                 :sentinel-group :group1 :master-name \"mymaster\"\n                 :prefer-slave? true})\n\n(defmacro wcars* [\u0026 body] `(cs/wcar slave-conn ~@body))\n\n(wcars* (car/set \"key\" 1)) ;; ExceptionInfo READONLY You can't write against a read only slave\n```\n\nIf you have many slaves for one master, the default balancer is `first` function, but you can custom it by `slaves-balancer`,\nfor example, using random strategy:\n\n```clojure\n(def slave-conn {:pool {\u003copts\u003e} :spec {}\n                 :sentinel-group :group1\n                 :master-name \"mymaster\"\n                 :prefer-slave? true\n                 :slaves-balancer rand-nth)\n```\n\n## Listen on carmine-sentinel events\n\nYou can register a listener to listen carmine-sentinel events such as `error`, `get-master-addr-by-name`\nand `+switch-master` etc. :\n\n```clojure\n(cs/register-listener! (fn [e] (println \"Event \" e \" happens\")))\n```\n\n## Failover\n\nAt startup, carmine-sentinel will connect the first sentinel instance to resolve the master address, if if fails, carmine-sentinel will try the next sentinel until find a resolved master address or throw an exception.The resolved addr will be cached in memory.\n\nAnd Carmine-sentinel subcribes `+switch-master` channel in sentinel.When the master redis instance is down, sentinel will publish a `+switch-master` message, while carmine-sentinel receives this message, it will clean the last cached result and try to connect the new redis master at once.\n\nAt last, carmine-sentinel will refresh the sentinel instance list by the response from command `SENTINEL sentinels [name]`.\n\n## API docs\n\n* [Carmine-sentinel APIs](http://fnil.net/docs/carmine_sentinel/)\n\n## Testing\n\nRunning the Makefile tests requires having `make`, `lein` and a Linux or MacOS machine.\nThe test scenario in the is comprised of three sentinels and one master.\n\nTo run the tests simply run in shell:\n```shell\nmake test\n```\n\n## License\n\nCopyright © 2016 killme2008\n\nDistributed under the Eclipse Public License either version 1.0 or (at\nyour option) any later version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkillme2008%2Fcarmine-sentinel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkillme2008%2Fcarmine-sentinel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkillme2008%2Fcarmine-sentinel/lists"}