Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
e154 committed Jan 22, 2024
0 parents commit e66e722
Show file tree
Hide file tree
Showing 16 changed files with 1,855 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# These are supported funding model platforms

github: e154 # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: bus # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
32 changes: 32 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Lint

on:
push:
branches:
- "*"
tags:
- "*"

jobs:
lint:
name: lint
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21.0

- name: Install linter
run: |
make install_linter
- name: Prepary deps
run: |
echo -e "machine github.com\n login ${{ secrets.GITHUBTOKEN }}" > ~/.netrc
make get_deps
- name: Run linter
run: make lint-todo
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Testing

on:
push:
branches:
- "*"
tags:
- "*"

jobs:
build:
if: github.event.base_ref != 'refs/heads/master'
name: Testing
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21.0

- name: Unit tests
id: test-code
run: make test

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
tmp/
/coverage.out
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Alex Filippov <[email protected]>
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.PHONY: get_deps fmt
.DEFAULT_GOAL := build
tests: lint test

test:
@echo MARK: unit tests
go test -v $(shell go list ./... | grep -v /tmp | grep -v /tests) -timeout 60s -race -covermode=atomic -coverprofile=coverage.out

install_linter:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.55.2

lint-todo:
@echo MARK: make lint todo

lint:
golangci-lint run

get_deps:
go mod tidy

fmt:
@gofmt -l -w -s .
@goimports -w .

comments:
@echo MARK: update comments
@gocmt -i -d .

125 changes: 125 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Bus Library: Implementing the Publish/Subscribe Paradigm in Golang

[![Go Report Card](https://goreportcard.com/badge/github.com/e154/bus)](https://goreportcard.com/report/github.com/e154/bus)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)

| Branch | Status |
|--------|--------|
| master | ![Build Status](https://github.com/e154/bus/actions/workflows/test.yml/badge.svg?branch=master) |
| dev | ![Build Status](https://github.com/e154/bus/actions/workflows/test.yml/badge.svg?branch=develop) |

### Overview

The Bus library is a tool designed for convenient implementation of the publish/subscribe paradigm in the Go programming language. The publish/subscribe paradigm is widely used to facilitate efficient message exchange between system components, distinguishing message producers (publishers) and message consumers (subscribers).

### Features

1. The Bus library supports a powerful topic system. Topics can contain `+` and `#` symbols, allowing for flexible topic hierarchies. For example, the topic "home/+/temperature" can subscribe to all temperature changes in different rooms.

2. One of the key advantages of the Bus library is its ability to transmit any data type based on structures. This provides flexibility and allows for the transmission of complex data between system components.

### Library Interface

```go
type Bus interface {
Publish(topic string, args ...interface{})
CloseTopic(topic string)
Subscribe(topic string, fn interface{}, options ...interface{}) error
Unsubscribe(topic string, fn interface{}) error
Stat(ctx context.Context, limit, offset int64, orderBy, sort string) (stats Stats, total int64, err error)
Purge()
}
```

### Core Methods

- **Publish(topic string, args ...interface{})**: Publishes a message to the specified topic with arbitrary arguments.

- **CloseTopic(topic string)**: Closes a topic, causing all subscribers to unsubscribe from that topic.

- **Subscribe(topic string, fn interface{}, options ...interface{}) error**: Subscribes to a topic with a specified handler function and additional options.

- **Unsubscribe(topic string, fn interface{}) error**: Unsubscribes from a topic for the specified handler function.

### Additional Methods

- **Stat(ctx context.Context, limit, offset int64, orderBy, sort string) (stats Stats, total int64, err error)**: Retrieves statistics on the usage of topics with pagination and sorting options.

- **Purge()**: Clears all topics and unsubscribes from all subscriptions.

### Example

```go
type TemperatureReading struct {
RoomID string
Temperature float64
}

type HumidityReading struct {
RoomID string
Humidity float64
}

func main() {

var messageBus = bus.NewBus()

var eventHandler = func(topic string, msg interface{}) {
switch v := msg.(type) {
case TemperatureReading:
fmt.Printf("topic: \"%s\", message: %v\n", topic, v)
case HumidityReading:
fmt.Printf("topic: \"%s\", message: %v\n", topic, v)
}
}

messageBus.Subscribe("home/#", eventHandler)
defer messageBus.Unsubscribe("home/#", eventHandler)

go func() {
for i := 0; i < 5; i++ {
messageBus.Publish("home/living_room/temperature", TemperatureReading{
RoomID: "living_room",
Temperature: 20.0 + float64(i),
})
time.Sleep(time.Second * 2)
}
}()

go func() {
for i := 0; i < 5; i++ {
messageBus.Publish("home/kitchen/humidity", HumidityReading{
RoomID: "kitchen",
Humidity: 40.0 + float64(i),
})
time.Sleep(time.Second * 3)
}
}()

// ctrl + C
var gracefulStop = make(chan os.Signal, 10)
signal.Notify(gracefulStop, syscall.SIGINT, syscall.SIGTERM)

<-gracefulStop

}
```

### Conclusion

The Bus library provides efficient tools for implementing the publish/subscribe paradigm in the Go programming language. Its use simplifies communication between system components, providing flexibility and ease of use.

### Contributors

- [Alex Filippov](https://github.com/e154)

All contributors are welcome. If you would like to contribute, please adhere to the following rules.

- Pull requests will be accepted only in the "develop" branch.
- All modifications or additions should be tested.

Thank you for your understanding!

### LICENSE

[GPLv3 Public License](https://github.com/e154/bus/blob/master/LICENSE)
Loading

0 comments on commit e66e722

Please sign in to comment.