Skip to content

Commit 0be04da

Browse files
authored
Service architecture refactoring (ortuman#104)
1 parent 06ef8c4 commit 0be04da

File tree

146 files changed

+3819
-6912
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+3819
-6912
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ os:
1010
- osx
1111

1212
go:
13-
- 1.13.x
13+
- 1.14.x
1414

1515
before_script:
1616
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-darwin-amd64 > ./cc-test-reporter; fi

CHANGELOG.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7-
## [0.8.2] - 2020-01-14
8-
### Fixed
9-
- Blocking Command implementation
10-
11-
## [0.8.1] - 2020-01-07
12-
### Added
13-
- Added context support
7+
## [0.9.0] - 2020-02-15
8+
### Changed
9+
- Router implementation refactoring
1410

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
install:
2-
@export GO111MODULE=on && go install -ldflags="-s -w" github.com/ortuman/jackal
2+
@go install -ldflags="-s -w" github.com/ortuman/jackal
33

44
install-tools:
5-
@export GO111MODULE=on && go get -u \
5+
@go get -u \
66
golang.org/x/lint/golint \
77
golang.org/x/tools/cmd/goimports
88

@@ -14,7 +14,7 @@ fmt: install-tools
1414

1515
build:
1616
@echo "Building binary..."
17-
@export GO111MODULE=on && go build -ldflags="-s -w"
17+
@export go build -ldflags="-s -w"
1818

1919
test:
2020
@echo "Running tests..."
@@ -34,7 +34,7 @@ lint: install-tools
3434

3535
dockerimage:
3636
@echo "Building binary..."
37-
@env GO111MODULE=on GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-s -w"
37+
@env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-s -w"
3838
@echo "Building docker image..."
3939
@docker build -f dockerfiles/Dockerfile -t ortuman/jackal .
4040

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,6 @@ However there's a chance to forward offline messages to some external service by
128128

129129
Each time a message is sent to an offline user a `POST` http request to the `pass` URL is made, using the specified `Authorization` header and including the message stanza into the request body.
130130

131-
## Cluster configuration
132-
133-
The purpose of clustering is to be able to use several servers for fault-tolerance and scalability.
134-
135-
To run `jackal` in clustering mode make sure to add a `cluster` section configuration in each of the cluster nodes.
136-
137-
Here is an example of how this section should look like:
138-
```yaml
139-
cluster:
140-
name: node1
141-
port: 5010
142-
hosts: [node2:5010, node3:5010]
143-
```
144-
145-
Do not forget to include all cluster nodes, excluding the local one, in the `hosts` array field. Otherwise the expected behavior will be undefined.
146-
147131
## Run jackal in Docker
148132

149133
Set up `jackal` in the cloud in under 5 minutes with zero knowledge of Golang or Linux shell using our [jackal Docker image](https://hub.docker.com/r/ortuman/jackal/).

app/app.go

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ import (
2020
"syscall"
2121
"time"
2222

23+
"github.com/google/uuid"
2324
"github.com/ortuman/jackal/c2s"
24-
"github.com/ortuman/jackal/cluster"
25+
c2srouter "github.com/ortuman/jackal/c2s/router"
2526
"github.com/ortuman/jackal/component"
2627
"github.com/ortuman/jackal/log"
2728
"github.com/ortuman/jackal/module"
2829
"github.com/ortuman/jackal/router"
30+
"github.com/ortuman/jackal/router/host"
2931
"github.com/ortuman/jackal/s2s"
32+
s2srouter "github.com/ortuman/jackal/s2s/router"
3033
"github.com/ortuman/jackal/storage"
3134
"github.com/ortuman/jackal/version"
3235
"github.com/pkg/errors"
3336
)
3437

3538
const (
39+
envAllocationID = "JACKAL_ALLOCATION_ID"
40+
3641
defaultShutDownWaitTime = time.Duration(5) * time.Second
3742
)
3843

@@ -60,10 +65,10 @@ type Application struct {
6065
output io.Writer
6166
args []string
6267
logger log.Logger
63-
cluster *cluster.Cluster
64-
router *router.Router
68+
router router.Router
6569
mods *module.Modules
6670
comps *component.Components
71+
s2sOutProvider *s2s.OutProvider
6772
s2s *s2s.S2S
6873
c2s *c2s.C2S
6974
debugSrv *http.Server
@@ -121,6 +126,7 @@ func (a *Application) Run() error {
121126
if err != nil {
122127
return err
123128
}
129+
124130
// create PID file
125131
if err := a.createPIDFile(cfg.PIDFile); err != nil {
126132
return err
@@ -131,50 +137,56 @@ func (a *Application) Run() error {
131137
return err
132138
}
133139

140+
// set allocation identifier
141+
allocID := os.Getenv(envAllocationID)
142+
if len(allocID) == 0 {
143+
allocID = uuid.New().String()
144+
}
145+
134146
// show jackal's fancy logo
135-
a.printLogo()
147+
a.printLogo(allocID)
136148

137149
// initialize storage
138150
repContainer, err := storage.New(&cfg.Storage)
139151
if err != nil {
140152
return err
141153
}
142-
// initialize router
143-
a.router, err = router.New(&cfg.Router, repContainer.User(), repContainer.BlockList())
154+
if err := repContainer.Presences().ClearPresences(context.Background()); err != nil {
155+
return err
156+
}
157+
158+
// initialize hosts
159+
hosts, err := host.New(cfg.Hosts)
144160
if err != nil {
145161
return err
146162
}
163+
// initialize router
164+
var s2sRouter router.S2SRouter
147165

148-
// initialize cluster
149-
if cfg.Cluster != nil {
150-
if repContainer.IsClusterCompatible() {
151-
a.cluster, err = cluster.New(cfg.Cluster, a.router.ClusterDelegate())
152-
if err != nil {
153-
return err
154-
}
155-
if a.cluster != nil {
156-
a.router.SetCluster(a.cluster)
157-
if err := a.cluster.Join(); err != nil {
158-
log.Warnf("%v", err)
159-
}
160-
}
161-
} else {
162-
log.Warnf("cluster mode disabled: storage type '%s' is not compatible", cfg.Storage.Type)
163-
}
166+
if cfg.S2S != nil {
167+
a.s2sOutProvider = s2s.NewOutProvider(cfg.S2S, hosts)
168+
s2sRouter = s2srouter.New(a.s2sOutProvider)
169+
}
170+
a.router, err = router.New(
171+
hosts,
172+
c2srouter.New(repContainer.User(), repContainer.BlockList()),
173+
s2sRouter,
174+
)
175+
if err != nil {
176+
return err
164177
}
165178

166179
// initialize modules & components...
167-
a.mods = module.New(&cfg.Modules, a.router, repContainer)
180+
a.mods = module.New(&cfg.Modules, a.router, repContainer, allocID)
168181
a.comps = component.New(&cfg.Components, a.mods.DiscoInfo)
169182

170183
// start serving s2s...
171-
a.s2s = s2s.New(cfg.S2S, a.mods, a.router)
172-
if a.s2s != nil {
173-
a.router.SetOutS2SProvider(a.s2s)
184+
if cfg.S2S != nil {
185+
a.s2s = s2s.New(cfg.S2S, a.mods, a.s2sOutProvider, a.router)
174186
a.s2s.Start()
175187
}
176188
// start serving c2s...
177-
a.c2s, err = c2s.New(cfg.C2S, a.mods, a.comps, a.router, repContainer.User())
189+
a.c2s, err = c2s.New(cfg.C2S, a.mods, a.comps, a.router, repContainer.User(), repContainer.BlockList())
178190
if err != nil {
179191
return err
180192
}
@@ -240,12 +252,12 @@ func (a *Application) initLogger(config *loggerConfig, output io.Writer) error {
240252
return nil
241253
}
242254

243-
func (a *Application) printLogo() {
255+
func (a *Application) printLogo(allocID string) {
244256
for i := range logoStr {
245257
log.Infof("%s", logoStr[i])
246258
}
247259
log.Infof("")
248-
log.Infof("jackal %v\n", version.ApplicationVersion)
260+
log.Infof("jackal %v - allocation_id: %s\n", version.ApplicationVersion, allocID)
249261
}
250262

251263
func (a *Application) initDebugServer(port int) error {
@@ -280,21 +292,34 @@ func (a *Application) gracefullyShutdown() error {
280292
func (a *Application) shutdown(ctx context.Context) <-chan bool {
281293
c := make(chan bool, 1)
282294
go func() {
283-
if a.debugSrv != nil {
284-
_ = a.debugSrv.Shutdown(ctx)
285-
}
286-
a.c2s.Shutdown(ctx)
287-
if a.s2s != nil {
288-
a.s2s.Shutdown(ctx)
295+
if err := a.doShutdown(ctx); err != nil {
296+
log.Warnf("failed to shutdown: %s", err)
289297
}
290-
if a.cluster != nil {
291-
_ = a.cluster.Shutdown(ctx)
292-
}
293-
_ = a.comps.Shutdown(ctx)
294-
_ = a.mods.Shutdown(ctx)
295-
296-
log.Unset()
297298
c <- true
298299
}()
299300
return c
300301
}
302+
303+
func (a *Application) doShutdown(ctx context.Context) error {
304+
if a.debugSrv != nil {
305+
if err := a.debugSrv.Shutdown(ctx); err != nil {
306+
return err
307+
}
308+
}
309+
a.c2s.Shutdown(ctx)
310+
311+
if err := a.comps.Shutdown(ctx); err != nil {
312+
return err
313+
}
314+
if err := a.mods.Shutdown(ctx); err != nil {
315+
return err
316+
}
317+
318+
if outProvider := a.s2sOutProvider; outProvider != nil {
319+
if err := outProvider.Shutdown(ctx); err != nil {
320+
return err
321+
}
322+
}
323+
log.Unset()
324+
return nil
325+
}

app/config.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import (
99
"bytes"
1010
"io/ioutil"
1111

12-
"github.com/ortuman/jackal/cluster"
13-
1412
"github.com/ortuman/jackal/c2s"
1513
"github.com/ortuman/jackal/component"
1614
"github.com/ortuman/jackal/module"
17-
"github.com/ortuman/jackal/router"
15+
"github.com/ortuman/jackal/router/host"
1816
"github.com/ortuman/jackal/s2s"
1917
"github.com/ortuman/jackal/storage"
20-
yaml "gopkg.in/yaml.v2"
18+
"gopkg.in/yaml.v2"
2119
)
2220

2321
// debugConfig represents debug server configuration.
@@ -36,16 +34,14 @@ type Config struct {
3634
Debug debugConfig `yaml:"debug"`
3735
Logger loggerConfig `yaml:"logger"`
3836
Storage storage.Config `yaml:"storage"`
39-
Cluster *cluster.Config `yaml:"cluster"`
40-
Router router.Config `yaml:"router"`
37+
Hosts []host.Config `yaml:"hosts"`
4138
Modules module.Config `yaml:"modules"`
4239
Components component.Config `yaml:"components"`
4340
C2S []c2s.Config `yaml:"c2s"`
4441
S2S *s2s.Config `yaml:"s2s"`
4542
}
4643

47-
// FromFile loads default global configuration from
48-
// a specified file.
44+
// FromFile loads default global configuration from a specified file.
4945
func (cfg *Config) FromFile(configFile string) error {
5046
b, err := ioutil.ReadFile(configFile)
5147
if err != nil {
@@ -54,8 +50,7 @@ func (cfg *Config) FromFile(configFile string) error {
5450
return yaml.Unmarshal(b, cfg)
5551
}
5652

57-
// FromBuffer loads default global configuration from
58-
// a specified byte buffer.
53+
// FromBuffer loads default global configuration from a specified byte buffer.
5954
func (cfg *Config) FromBuffer(buf *bytes.Buffer) error {
6055
return yaml.Unmarshal(buf.Bytes(), cfg)
6156
}

auth/auth_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"context"
1010
"testing"
1111

12+
"github.com/google/uuid"
1213
"github.com/ortuman/jackal/model"
1314
memorystorage "github.com/ortuman/jackal/storage/memory"
1415
"github.com/ortuman/jackal/stream"
1516
"github.com/ortuman/jackal/xmpp/jid"
16-
"github.com/pborman/uuid"
1717
"github.com/stretchr/testify/require"
1818
)
1919

@@ -24,7 +24,7 @@ func authTestSetup(user *model.User) (*stream.MockC2S, *memorystorage.User) {
2424

2525
j, _ := jid.New("mariana", "localhost", "res", true)
2626

27-
testStm := stream.NewMockC2S(uuid.New(), j)
27+
testStm := stream.NewMockC2S(uuid.New().String(), j)
2828

2929
testStm.SetJID(j)
3030
return testStm, s

auth/scram.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
"hash"
1818
"strings"
1919

20+
"github.com/google/uuid"
2021
"github.com/ortuman/jackal/model"
2122
"github.com/ortuman/jackal/storage/repository"
2223
"github.com/ortuman/jackal/stream"
2324
"github.com/ortuman/jackal/transport"
2425
utilrand "github.com/ortuman/jackal/util/rand"
2526
utilstring "github.com/ortuman/jackal/util/string"
2627
"github.com/ortuman/jackal/xmpp"
27-
"github.com/pborman/uuid"
2828
"golang.org/x/crypto/pbkdf2"
2929
)
3030

@@ -222,7 +222,7 @@ func (s *Scram) handleStart(ctx context.Context, elem xmpp.XElement) error {
222222
}
223223
s.user = user
224224

225-
s.srvNonce = cNonce + "-" + uuid.New()
225+
s.srvNonce = cNonce + "-" + uuid.New().String()
226226
s.salt = utilrand.RandomBytes(32)
227227
sb64 := base64.StdEncoding.EncodeToString(s.salt)
228228
s.firstMessage = fmt.Sprintf("r=%s,s=%s,i=%d", s.srvNonce, sb64, iterationsCount)

c2s/c2s.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ type c2sServer interface {
3333
shutdown(ctx context.Context) error
3434
}
3535

36-
var createC2SServer = func(config *Config, mods *module.Modules, comps *component.Components, router *router.Router, userRep repository.User) c2sServer {
37-
return &server{cfg: config, mods: mods, comps: comps, router: router, userRep: userRep}
38-
}
36+
var createC2SServer = newC2SServer
3937

4038
// C2S represents a client-to-server connection manager.
4139
type C2S struct {
@@ -45,13 +43,13 @@ type C2S struct {
4543
}
4644

4745
// New returns a new instance of a c2s connection manager.
48-
func New(configs []Config, mods *module.Modules, comps *component.Components, router *router.Router, userRep repository.User) (*C2S, error) {
46+
func New(configs []Config, mods *module.Modules, comps *component.Components, router router.Router, userRep repository.User, blockListRep repository.BlockList) (*C2S, error) {
4947
if len(configs) == 0 {
5048
return nil, errors.New("at least one c2s configuration is required")
5149
}
5250
c := &C2S{servers: make(map[string]c2sServer)}
5351
for _, config := range configs {
54-
srv := createC2SServer(&config, mods, comps, router, userRep)
52+
srv := createC2SServer(&config, mods, comps, router, userRep, blockListRep)
5553
c.servers[config.ID] = srv
5654
}
5755
return c, nil

0 commit comments

Comments
 (0)