Skip to content

Commit 347f890

Browse files
committed
UPT: updated readme
1 parent d8bfc0b commit 347f890

File tree

1 file changed

+109
-70
lines changed

1 file changed

+109
-70
lines changed

README.md

+109-70
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22

33
[![GoDoc](https://img.shields.io/badge/api-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/ewe-studios/sabuhp)
44

5-
Power your backend with a hybrid service layer communicating across a message bus regardless of protocol.
5+
Power your backend with a simple service architecture that provides direct connection into a function/processor network
6+
through supported protocols (HTTP, WebSocket, ServerSent Events).
67

7-
It exposes a your services across both a http, server-sent events and websocket endpoints, allowing varying clients to communicate across your services over a message bus.
8+
SabuHP exposes a two server system by providing a `Client Server` and a `Worker Server` architecture that allow
9+
better scaling of client connections and business logic processing in the new age of message busses as backbone of
10+
communications.
811

12+
The `client server` exists to allow direct connections from clients (CLI, Browsers) which can directly send desired
13+
request payload to desired topics and receive response from a target message bus. This allows us decouple the definition
14+
of our APIs, and their desired behaviour from how clients interact and connect to with them. The client servers purpose is
15+
to hide way the needed intricacies to access this message queues or buses, providing a clear and familiar APIs that clients
16+
can work with such systems with ease.
17+
18+
The `worker server` exists to provided scalable services that can be horizontal scaled with only required to be able to
19+
connect to a message bus to listen and process request payload for target topics with ease. This allows us decouple entirely
20+
how we connect and process messages or work within a software systems.
921

1022
## Protocols
1123

@@ -22,98 +34,125 @@ SabuHP supports the following protocols for communicating with the service serve
2234
go get -u github.com/ewe-studios/sabuhp
2335
```
2436

25-
## Using
37+
## Client Server
2638

27-
Create a sample hello service
39+
Client servers provides a server which hosts all necessary client protocols (http, websocket, server-sent event routes)
40+
which allows clients (browsers, CLI agents) to connect into the SabuHP networks allowing these clients to deliver
41+
requests and receive responses for their requests
2842

2943
```go
44+
3045
package main
3146

3247
import (
3348
"context"
3449
"log"
35-
"net/http"
36-
"time"
3750

3851
"github.com/influx6/npkg/ndaemon"
39-
"github.com/influx6/npkg/nerror"
40-
"github.com/influx6/npkg/njson"
41-
"github.com/influx6/npkg/nxid"
4252

4353
"github.com/ewe-studios/sabuhp"
44-
"github.com/ewe-studios/sabuhp/ochestrator"
45-
"github.com/ewe-studios/sabuhp/slaves"
46-
"github.com/ewe-studios/sabuhp/testingutils"
47-
)
4854

49-
var (
50-
mainLogger = &testingutils.LoggerPub{}
55+
"github.com/ewe-studios/sabuhp/bus/redispub"
56+
"github.com/ewe-studios/sabuhp/servers/clientServer"
57+
redis "github.com/go-redis/redis/v8"
5158
)
5259

5360
func main() {
54-
var logStack = njson.Log(mainLogger)
55-
defer njson.ReleaseLogStack(logStack)
56-
57-
// worker template registry
58-
var workerRegistry = slaves.NewWorkerTemplateRegistry()
59-
workerRegistry.Register(slaves.WorkerRequest{
60-
ActionName: "hello_world",
61-
PubSubTopic: "hello",
62-
WorkerCreator: func(config slaves.WorkerConfig) *slaves.WorkerGroup {
63-
config.Instance = slaves.ScalingInstances
64-
config.Behaviour = slaves.RestartAll
65-
config.Action = slaves.ActionFunc(func(ctx context.Context, to string, message *sabuhp.Message, t sabuhp.Transport) {
66-
if sendErr := t.SendToAll(&sabuhp.Message{
67-
ID: nxid.New(),
68-
Topic: message.FromAddr,
69-
FromAddr: to,
70-
Payload: []byte("hello world"),
71-
Metadata: nil,
72-
Params: nil,
73-
}, 5*time.Second); sendErr != nil {
74-
logStack.New().
75-
LError().
76-
Message("failed to send response message").
77-
String("error", nerror.WrapOnly(sendErr).Error()).
78-
End()
79-
}
80-
})
81-
return slaves.NewWorkGroup(config)
82-
},
61+
var ctx, canceler = context.WithCancel(context.Background())
62+
ndaemon.WaiterForKillWithSignal(ndaemon.WaitForKillChan(), canceler)
63+
64+
var logger sabuhp.GoLogImpl
65+
66+
var redisBus, busErr = redispub.Stream(redispub.Config{
67+
Logger: logger,
68+
Ctx: ctx,
69+
Redis: redis.Options{},
70+
Codec: clientServer.DefaultCodec,
8371
})
8472

85-
// register for terminal kill signal
86-
var masterCtx, masterEnder = context.WithCancel(context.Background())
87-
ndaemon.WaiterForKillWithSignal(ndaemon.WaitForKillChan(), masterEnder)
73+
if busErr != nil {
74+
log.Fatalf("Failed to create bus connection: %q\n", busErr.Error())
75+
}
8876

89-
var workerId = nxid.New()
90-
var station = ochestrator.DefaultStation(masterCtx, workerId, ":7800", mainLogger, workerRegistry)
77+
var cs = clientServer.New(
78+
ctx,
79+
logger,
80+
redisBus,
81+
clientServer.WithHttpAddr("0.0.0.0:9650"),
82+
)
9183

92-
// use json encoder
93-
station.CreateCodec = ochestrator.JsonCodec
84+
cs.Start()
9485

95-
if stationInitErr := station.Init(); stationInitErr != nil {
96-
var wrapErr = nerror.WrapOnly(stationInitErr)
97-
log.Fatalf("Closing application due to station initialization: %+q", wrapErr)
86+
log.Println("Starting client server")
87+
if err := cs.ErrGroup.Wait(); err != nil {
88+
log.Fatalf("service group finished with error: %+s", err.Error())
9889
}
90+
}
91+
```
92+
93+
94+
## Worker Server
95+
96+
Worker servers exposes a server with different registered workers (Functions, Processors) who will listen to the
97+
connected message bus for new requests to be processed. These servers can be scaled horizontally and grouped into
98+
listen groups based on support by the underline message bus to create a cloud of processors that allow endless scaling.
99+
100+
101+
```go
102+
package main
103+
104+
import (
105+
"context"
106+
"fmt"
107+
"log"
99108

100-
// create a http to event route redirect to an event
101-
station.Router().RedirectTo("hello", "/hello")
102-
103-
// create a normal http route
104-
station.Router().Http("/pop", sabuhp.HandlerFunc(func(
105-
writer http.ResponseWriter,
106-
request *http.Request,
107-
params sabuhp.Params,
108-
) {
109-
writer.WriteHeader(http.StatusOK)
110-
_, _ = writer.Write([]byte("stay forever!"))
111-
}))
112-
113-
if err := station.Wait(); err != nil {
114-
var wrapErr = nerror.WrapOnly(err)
115-
log.Fatalf("Closing application: %+q", wrapErr)
109+
"github.com/ewe-studios/sabuhp/actions"
110+
111+
"github.com/ewe-studios/sabuhp/servers/serviceServer"
112+
113+
"github.com/influx6/npkg/ndaemon"
114+
115+
"github.com/ewe-studios/sabuhp"
116+
117+
"github.com/ewe-studios/sabuhp/bus/redispub"
118+
"github.com/ewe-studios/sabuhp/servers/clientServer"
119+
redis "github.com/go-redis/redis/v8"
120+
)
121+
122+
func main() {
123+
var ctx, canceler = context.WithCancel(context.Background())
124+
ndaemon.WaiterForKillWithSignal(ndaemon.WaitForKillChan(), canceler)
125+
126+
var logger sabuhp.GoLogImpl
127+
128+
var redisBus, busErr = redispub.Stream(redispub.Config{
129+
Logger: logger,
130+
Ctx: ctx,
131+
Redis: redis.Options{},
132+
Codec: clientServer.DefaultCodec,
133+
})
134+
135+
if busErr != nil {
136+
log.Fatalf("Failed to create bus connection: %q\n", busErr.Error())
116137
}
138+
139+
var workers = actions.NewWorkerTemplateRegistry()
140+
var cs = serviceServer.New(
141+
ctx,
142+
logger,
143+
redisBus,
144+
serviceServer.WithWorkerRegistry(workers),
145+
)
146+
147+
fmt.Println("Starting worker service")
148+
cs.Start()
149+
150+
fmt.Println("Started worker service")
151+
if err := cs.ErrGroup.Wait(); err != nil {
152+
log.Fatalf("service group finished with error: %+s", err.Error())
153+
}
154+
155+
fmt.Println("Closed worker service")
117156
}
118157

119158
```

0 commit comments

Comments
 (0)