-
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 8dc2063
Showing
9 changed files
with
542 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,39 @@ | ||
# compiled output | ||
/dist | ||
/node_modules | ||
|
||
# Logs | ||
logs | ||
*.log | ||
log/* | ||
npm-debug.log* | ||
pnpm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# OS | ||
.DS_Store | ||
|
||
# Tests | ||
/coverage | ||
/.nyc_output | ||
|
||
# IDEs and editors | ||
/.idea | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
env/* | ||
!env/.env.example |
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 @@ | ||
The MIT License (MIT) | ||
Copyright © 2022 [email protected] | ||
|
||
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,117 @@ | ||
# chdist | ||
|
||
## Channel Distributor | ||
|
||
Make golang channel be able to be subscribed with multiple subscribers. | ||
|
||
It uses generic so Go1.18+ needed. | ||
|
||
## Description | ||
|
||
Go channel applys only one subscriber at once. `chdist` implements pub-sub model of go channel, so it can be used like a small message queue system. Also, `chdist` provides sync and async method both. This system can be used just like handling a normal channel. | ||
|
||
## Distributor | ||
|
||
`Distributor` is a publisher. The constructor of `Distributor` receives generic type and a channel (whose type is given generic type) to generate an instance, and it can be subscribed as sync or async. | ||
|
||
``` | ||
channel := make(chan int) | ||
distributor := chdist.NewDistributor[int](channel) | ||
``` | ||
|
||
You can publish using the channel and the distributor both. | ||
|
||
``` | ||
channel <- 5 | ||
// or | ||
distributor.In() <- 5 | ||
``` | ||
|
||
Multiple subscribing is possible. | ||
|
||
``` | ||
// sync | ||
distributor.Subscribe(func(value int) { | ||
fmt.Println(value) | ||
}) | ||
// async | ||
for value := range distributor.AsyncSubscribe.Out() { | ||
fmt.Println(value) | ||
} | ||
``` | ||
|
||
And it can be closed by using `Close()`. | ||
|
||
``` | ||
distributor.Close() | ||
``` | ||
|
||
## Subscription | ||
|
||
When a distributor is subscribed, it returns a `Subscription` instance. This can be closed, health-checked, or something else with its methods. | ||
|
||
``` | ||
subscription := distributor.Subscribe(func(value int) { /* logics */ }) | ||
if subscription.IsAlive() { | ||
subscription.Close() | ||
} | ||
``` | ||
|
||
`AsyncSubscription` can be used like a common channel. | ||
|
||
``` | ||
asyncSubscription := distributor.AsyncSubscribe() | ||
fmt.Println(<-asyncSubscription.Out()) | ||
``` | ||
|
||
## JsonDistributor | ||
|
||
`JsonDistributor` can handle JSON data. This receives a type of the JSON data will be after unmarshaled. | ||
|
||
It applys `string` and `[]byte` type of JSON data, each of them can be generated by `NewJsonStringDistributor` and `NewJsonBytesDistributor`. | ||
|
||
It uses `encoding/json` package to unmarshal which can occur an error, so `JsonDistributor` uses a struct `Item` representing both value and error. | ||
|
||
``` | ||
type Item[T any] struct { | ||
Value T | ||
Error error | ||
} | ||
``` | ||
|
||
`JsonStringDistributor`'s constructor receives `string` type `Distributor` and `JsonBytesDistributor`'s receives `[]byte` type `Distributor`. | ||
|
||
``` | ||
type Sample struct { | ||
Num int `json:"num"` | ||
} | ||
channel := make(chan string) | ||
jsonStringDistributor := chdist.NewJsonStringDistributor[Sample]( | ||
chdist.NewDistributor[string](channel), | ||
) | ||
go func() { | ||
for { | ||
channel <- `{"num":123}` | ||
time.Sleep(time.Second * 5) | ||
} | ||
}() | ||
jsonStringDistributor.Subscribe(func(item chdist.Item[Sample]) { | ||
fmt.Println(item.Value) // type of `Sample` | ||
fmt.Println(item.Error) // if error occured while unmarshaling, it goes to | ||
}) | ||
``` | ||
|
||
## How to use | ||
|
||
``` | ||
go get github.com/p9595jh/chdist | ||
``` | ||
|
||
``` | ||
import "github.com/p9595jh/chdist" | ||
``` |
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,168 @@ | ||
package chdist_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/p9595jh/chdist" | ||
) | ||
|
||
func TestDistribution(t *testing.T) { | ||
d := chdist.NewDistributor(make(chan int)) | ||
|
||
s1 := d.Subscribe(func(value int) { | ||
t.Log(1, value) | ||
}) | ||
d.Subscribe(func(value int) { | ||
t.Log(2, value) | ||
}) | ||
|
||
_ = s1 | ||
|
||
go func() { | ||
for i := 0; i < 3; i++ { | ||
d.In() <- i | ||
time.Sleep(time.Second) | ||
} | ||
}() | ||
|
||
time.Sleep(time.Second * 3) | ||
d.Close() | ||
time.Sleep(time.Second * 5) | ||
} | ||
|
||
func TestAsync(t *testing.T) { | ||
d := chdist.NewDistributor(make(chan int)) | ||
|
||
s1 := d.AsyncSubscribe() | ||
go func() { | ||
for a := range s1.Out() { | ||
t.Log(1, a) | ||
} | ||
}() | ||
|
||
s2 := d.AsyncSubscribe() | ||
go func() { | ||
for a := range s2.Out() { | ||
t.Log(2, a) | ||
} | ||
}() | ||
|
||
d.Subscribe(func(value int) { | ||
t.Log(3, value) | ||
}) | ||
|
||
go func() { | ||
for i := 0; i < 3; i++ { | ||
d.In() <- i | ||
time.Sleep(time.Second) | ||
} | ||
}() | ||
|
||
// d.Close() | ||
time.Sleep(time.Second * 5) | ||
} | ||
|
||
type Model struct { | ||
Num int `json:"num"` | ||
Str string `json:"str"` | ||
} | ||
|
||
func TestJsonString(t *testing.T) { | ||
jsonify := func(i int, s string) string { | ||
return fmt.Sprintf(`{"num":%d,"str":"%s"}`, i, s) | ||
} | ||
|
||
ch := make(chan string) | ||
d := chdist.NewJsonStringDistributor[Model](chdist.NewDistributor(ch)) | ||
|
||
go func() { | ||
for i := 0; ; i++ { | ||
time.Sleep(time.Second * 2) | ||
ch <- jsonify(i, "hello") | ||
} | ||
}() | ||
|
||
d.Subscribe(func(i chdist.Item[Model]) { | ||
t.Log(i.Value) | ||
}) | ||
time.Sleep(time.Second * 7) | ||
} | ||
|
||
func TestJsonStringAsync(t *testing.T) { | ||
jsonify := func(i int, s string) string { | ||
return fmt.Sprintf(`{"num":%d,"str":"%s"}`, i, s) | ||
} | ||
|
||
ch := make(chan string) | ||
d := chdist.NewJsonStringDistributor[Model](chdist.NewDistributor(ch)) | ||
|
||
go func() { | ||
for i := 0; ; i++ { | ||
time.Sleep(time.Second * 2) | ||
ch <- jsonify(i, "hello") | ||
} | ||
}() | ||
|
||
go func() { | ||
for item := range d.AsyncSubscribe() { | ||
t.Log(item) | ||
} | ||
}() | ||
time.Sleep(time.Second * 7) | ||
} | ||
|
||
func TestJsonBytes(t *testing.T) { | ||
jsonify := func(i int, s string) []byte { | ||
return []byte(fmt.Sprintf(`{"num":%d,"str":"%s"}`, i, s)) | ||
} | ||
|
||
ch := make(chan []byte) | ||
d := chdist.NewJsonBytesDistributor[Model](chdist.NewDistributor(ch)) | ||
|
||
go func() { | ||
for i := 0; ; i++ { | ||
time.Sleep(time.Second * 2) | ||
ch <- jsonify(i, "hello") | ||
} | ||
}() | ||
|
||
d.Subscribe(func(item chdist.Item[Model]) { | ||
t.Log(item) | ||
}) | ||
time.Sleep(time.Second * 7) | ||
} | ||
|
||
func TestClose(t *testing.T) { | ||
d := chdist.NewDistributor(make(chan int)) | ||
sub1 := d.Subscribe(func(value int) { | ||
t.Log(1, value) | ||
}) | ||
sub2 := d.Subscribe(func(value int) { | ||
t.Log(2, value) | ||
}) | ||
d.Subscribe(func(value int) { | ||
t.Log(3, value) | ||
}) | ||
// _ = sub2 | ||
|
||
go func() { | ||
for i := 0; ; i++ { | ||
time.Sleep(time.Second * 2) | ||
d.In() <- i | ||
} | ||
}() | ||
|
||
for _, sub := range d.Subscriptions() { | ||
// t.Log(sub, sub.IsAlive()) | ||
t.Log(sub) | ||
} | ||
time.Sleep(time.Second * 6) | ||
|
||
sub1.Close() | ||
t.Log(d.Subscriptions()) | ||
|
||
time.Sleep(time.Second * 6) | ||
_ = sub2 | ||
} |
Oops, something went wrong.