Skip to content

Commit

Permalink
Use ConfigFact instead of ServerConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Jul 27, 2023
1 parent 33bfa93 commit 96581eb
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 18 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![](_logo/logo.png)

# sputnik [![GoDev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/g41797/sputnik) [![Wiki](https://img.shields.io/badge/Wikipedia-%23000000.svg?style=for-the-badge&logo=wikipedia&logoColor=white)](https://en.wikipedia.org/wiki/Sputnik_1)
**sputnik** is tiny golang framework for building of **satellite** or as it's now fashionable to say **side-car** processes.
**sputnik** is tiny golang framework for building of **satellite** or as it's now fashionable to say **sidecar** processes.

## What do satellite processes have in common?
The same sometimes boring flow:
Expand Down Expand Up @@ -33,14 +33,14 @@ sputnik forces modular-monolith design:

### Satellites blueprint

sputnik supports common for all satellite processes functionality::
sputnik supports common for all satellite processes functionality:
* Deterministic initialization
* Connect/Reconnect flow
* Server heartbeat
* Convenient negotiation between blocks of the process
* Graceful shutdown

All this with minimal code size - actually the size of README and tests far exceeds size of sputnik's code.
All this with minimal code size - actually the size of README and tests far exceeds the size of sputnik's code.

## Why Sputnik?
* Launched by the Soviet Union on 4 October 1957, **Sputnik** became the first **satellite** in space and changed the world forever.
Expand All @@ -59,8 +59,12 @@ type ServerConfiguration any

In order to get configuration and provide it to the process, sputnik uses *Configuration Factory*:
```go
type ConfFactory func() ServerConfiguration
type ConfFactory func(confName string, result any) error
```
where
- confName - name of configuration
- result - unmarshaled configuration(usually struct)


This function should be supplied by caller of sputnik during initialization. We will talk about initialization later.

Expand Down Expand Up @@ -168,7 +172,7 @@ You can see that these callbacks reflect life cycle/flow of satellite process.
#### Init

```go
type Init func(conf any) error
type Init func(cf ConfFactory) error
```

Init callback is executed by sputnik once during initialization.
Expand Down
4 changes: 2 additions & 2 deletions activeblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type activeBlock struct {
controller *controller
}

func (abl *activeBlock) init(cnf ServerConfiguration) error {
err := abl.block.init(cnf)
func (abl *activeBlock) init(cf ConfFactory) error {
err := abl.block.init(cf)

if err != nil {
err = fmt.Errorf("Init of [%s,%s] failed with error %s", abl.descriptor.Name, abl.descriptor.Responsibility, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion block.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
// Some rules :
// - don't run hard processing within Init
// - don't work with server till call of OnServerConnect
type Init func(conf ServerConfiguration) error
type Init func(cf ConfFactory) error

// After successful initialization of ALL blocks, sputnik creates goroutine and calls Run
// Other callbacks will be executed on another goroutines
Expand Down
8 changes: 4 additions & 4 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func connectorBlockFactory() *Block {
type doIt func()

type connector struct {
conf ServerConfiguration
cf ConfFactory

mc chan Msg
cnr ServerConnector
Expand All @@ -63,9 +63,9 @@ type connector struct {
connected bool
}

func (c *connector) init(conf ServerConfiguration) error {
func (c *connector) init(cf ConfFactory) error {

c.conf = conf
c.cf = cf
c.next = c.connect
c.mc = make(chan Msg, 1)
c.bgfin = make(chan struct{}, 1)
Expand Down Expand Up @@ -156,7 +156,7 @@ func (c *connector) connect() {
}

if !c.connected {
conn, err := c.cnr.Connect(c.conf)
conn, err := c.cnr.Connect(c.cf)

if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion dumbBlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type dumbBlock struct {
// dumbBlock support all callbacks of Block:
//
// Init
func (dmb *dumbBlock) init(cnf sputnik.ServerConfiguration) error {
func (dmb *dumbBlock) init(_ sputnik.ConfFactory) error {
dmb.stop = make(chan struct{}, 1)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion finisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type finisher struct {
communicator BlockCommunicator
}

func (bl *finisher) init(conf ServerConfiguration) error {
func (bl *finisher) init(_ ConfFactory) error {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (inr *initiator) factory() *Block {
)
}

func (inr *initiator) init(_ ServerConfiguration) error {
func (inr *initiator) init(_ ConfFactory) error {

appBlks, err := inr.sputnik.createActiveBlocks()
if err != nil {
Expand All @@ -38,7 +38,7 @@ func (inr *initiator) init(_ ServerConfiguration) error {
ibs := make(activeBlocks, 0)

for _, abl := range appBlks {
err = abl.init(inr.sputnik.cnfFact())
err = abl.init(inr.sputnik.cnfFact)
if err != nil {
break
}
Expand Down
2 changes: 1 addition & 1 deletion sputnik.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type ServerConfiguration any

// Configuration Factory
type ConfFactory func() ServerConfiguration
type ConfFactory func(confName string, result any) error

type Sputnik struct {
// Configuration factory
Expand Down
2 changes: 1 addition & 1 deletion testBlocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ var blkList []sputnik.BlockDescriptor = []sputnik.BlockDescriptor{
}

// Configuration factory:
func dumbConf() sputnik.ServerConfiguration { return nil }
func dumbConf(confName string, result any) error { return nil }

func dumbSputnik(tb *testBlocks) sputnik.Sputnik {
sp, _ := sputnik.NewSputnik(
Expand Down

0 comments on commit 96581eb

Please sign in to comment.