{"id":18804898,"url":"https://github.com/leonardpahlke/gobully","last_synced_at":"2026-05-01T04:33:53.522Z","repository":{"id":133704224,"uuid":"268620827","full_name":"leonardpahlke/goBully","owner":"leonardpahlke","description":"distributed systems bully algorithm \u0026 distributed mutex","archived":false,"fork":false,"pushed_at":"2024-05-28T09:30:35.000Z","size":3308,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-29T20:45:09.487Z","etag":null,"topics":["bully-algorithm","distributed-systems","docker","golang","swagger"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leonardpahlke.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-01T20:06:11.000Z","updated_at":"2024-05-28T09:30:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"b3238b6d-7c84-46d2-be37-161a6cd29681","html_url":"https://github.com/leonardpahlke/goBully","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardpahlke%2FgoBully","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardpahlke%2FgoBully/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardpahlke%2FgoBully/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardpahlke%2FgoBully/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leonardpahlke","download_url":"https://codeload.github.com/leonardpahlke/goBully/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239737358,"owners_count":19688617,"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":["bully-algorithm","distributed-systems","docker","golang","swagger"],"created_at":"2024-11-07T22:40:58.948Z","updated_at":"2026-01-06T22:30:13.215Z","avatar_url":"https://github.com/leonardpahlke.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# goBully\n\nLearning project to understand how to implement the bully algorithm and a distributed mutex with docker containers\n\nThis project implements the bully algorithm as well as a distributed mutex with docker containers\nSeveral containers are served, each of which is accessible over a REST API\nFor more information about that, take a look at the code comments and the swagger documentation `task swagger`.\n\nDetails about the implementation of the Bully algorithm and the distributed mutex are shown below  \n\n## Install\n\n1. **GO** [installation](https://golang.org/doc/install) getting started - *run project binary*  \n2. **Docker** [installation](https://docs.docker.com/get-docker/) getting started - *be able to run docker containers*\n3. **Task** [installation](https://taskfile.dev/#/installation) doc - *build tool Taskfile.yml*\n4. **Go Swagger** [installation](https://goswagger.io/install.html) doc - *swagger api documentation*\n\n## Build\n\nexecute commands within the project root directory\n\n### Check commands\n\n```shell\ntask --list\ntask: Available tasks for this project:\n* build:        Build docker container\n* run:          Start docker container\n* sdown:        Stop docker-compose scenario\n* sup:          Start docker-compose scenario\n* swagger:      Generate swagger.yml and start local server\n* update:       Update project dependencies\n```\n\n### Run commands\n\n```shell\n// run listed commands\ntask \u003ctask\u003e\n// like\ntask build\n```\n\n### Stop Docker container leonardpahlke/gobully:latest\n\n```shell\ndocker stop $(docker ps -a -q --filter ancestor=leonardpahlke/gobully:latest --format=\"{{.ID}}\")\n```\n\n## Features\n\n- docker container as user in the network to run the bully algorithm\n- bully algorithm scenario with docker-compose simulated\n- detailed swagger documentation [Swagger yml](api/swagger.yml) with [go-swagger](https://github.com/go-swagger/go-swagger)\n\n## Project folder structure\n\n```shell\n── goBully\n├── api\n│   └── swagger.yml             // swagger api dcumentation\n├── assets\n│   └── goBully.jpg             // pictures and stuff\n├── cmd\n│   └── main.go                 // starting point of the application\n├── internal\n│   ├── election\n│   │   ├── election.go         // election private functions\n│   │   └── election_client.go  // election public functions\n│   ├── identity\n│   │   ├── register.go         // user register workflow\n│   │   └── user.go             // user definition\n│   ├── api\n│   │   ├── doc.go              // rest general documentation info\n│   │   ├── rest_client.go      // api setup\n│   │   ├── rest_election.go    // election rest endpoints\n│   │   ├── rest_mutex.go       // mutex rest endpoints\n│   │   └── rest_user.go        // user rest endpoints\n│   └── mutex\n│       ├── mutex.go            // mutex private functions\n│       └── mutex_client.go     // mutex public functions\n├── pkg\n│   └── request.go              // rest http calls\n├── .gitignore\n├── docker-compose.yml          // docker-compose szenario\n├── Dockerfile                  // docker container script\n├── Taskfile.yml                // build scripts\n├── go.mod                      // go module information\n├── go.sum                      // go module libary imports\n└── README.md\n```\n\n**Project Dependencies**\n![goBullyDependencies](assets/project-dependencies.jpg)\n\n## Bully Algorithm implementation\n\nScenario info:\n\n- user1 -\u003e null\n- user2 -\u003e user1\n- user3 -\u003e user1\n\nIf a user connects to another (register to network), new user information gets send to all network participants\n\n![goBully](assets/goBully.jpg)\n\n`internal/election/election.go`\n\n- receiveMessage()            // get a message from a api (election, coordinator)\n- receiveMessageElection()    // handle incoming election message\n- sendMessageElection()       // send a election message to another user\n- receiveMessageCoordinator() // set local coordinator reference with incoming details\n- sendMessagesCoordinator()   // send coordinator messages to other users\n\n### Election details\n\n**receiveMessageElection** - election message received\n\n1. filter users to send election messages to (UserID \u003e YourID)\n2. if |filtered users| \u003c= 0\n2.1 YES: you have the highest ID and win - send coordinatorMessages - exit\n2.2 NO: transform message and create POST payload\n2.3 add user information to local callbackList\n2.4 GO - sendElectionMessage(callbackResponse, msgPayload)\n2.4.1 send POST request to client\n2.4.2 if response is OK add client to client who have responded responded\n2.5 wait a few seconds (enough time users can answer request)\n2.6 Sort users who have called back and who are not\n2.7 if |answered users| \u003c= 0\n2.7.1 YES: send coordinatorMessages\n2.8 remove all users how didn't answered from userList\n2.9 clear callback list\n3. send response back (answer)\n\n## Mutex implementation\n\nIt goes like this with 3 clients (**A**,**B**,**C**):\n\n- Client **A** wants to enter the critical section\n- **A** sends _request_ with his clock to **A**,**B**,**C**\n- **B** is currently `in` the critical section, does store the _request_\n- **C** is `idle` and sends _reply-ok_\n- **A** sends himself an _reply-ok_\n- **C** wants to enter the critical section \u0026 sends _request_ to **A**,**B**,**C**\n- **A** `waits` for the mutex and his request has a lower clock, therefore stores the _request_\n- **B** is `in` the critical section, therefore stores the _request_\n- **B** finishes his critical section\n- **B** sends _reply-ok_ to the stored requests of **A** and **C**\n- **A** got all required _reply-ok_ and may now enter the critical section\n- **C** still `waits`.\n- **A** has finished his critical section and sends _reply-ok_ to the stored request of **C**\n- **C** got all required _reply-ok_ and may now enter the critical section\n\n`internal/mutex/**`\n\n### Mutex details\n\n**requestCriticalArea** - tell all users that this user wants to enter the critical section\n\n1. set state to 'wanting'\n2. increment clock, you are about to send mutex-messages\n3. create a request mutex-message\n4. create a response channel for every user (including yourself)\n5. create new object to manage responses of this request (containing all user response channels)\n6. add new requestResponseChannel to replyOkwaitingList\n7. GO - send all users the request mutex-message\n8. wait for all users to reply-ok to your request\n9. remove the waiting reponses object from the list\n10. enterCriticalSection()\n\n**sendRequestToUser** - send request message to a user\n\n1. send POST to user and wait for reply-ok answer\n2. start checking if user answered\n\n**checkClientIfResponded** - listen if client reply-ok'ed and check with him back if not\n\n1. GO - clientHealthCheck() - sends periodic beats to check whether the user has responded\n2. receiving message send through the channel\n3. if message is reply-ok, return\n4. ping user mutexState\n5. wait some time to get response back\n6. if answered: loopback to 2.\n7. remove user from waiting list\n8. delete user from local user management (inactive)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonardpahlke%2Fgobully","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleonardpahlke%2Fgobully","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonardpahlke%2Fgobully/lists"}