-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
44 lines (36 loc) · 899 Bytes
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package s2srouter
import (
"context"
"sync"
"github.com/dantin/cubit/router"
"github.com/dantin/cubit/xmpp"
)
type s2sRouter struct {
mu sync.RWMutex
outProvider OutProvider
remotes map[string]*remoteRouter
}
// New creates a router between two servers.
func New(outProvider OutProvider) router.S2SRouter {
return &s2sRouter{
outProvider: outProvider,
remotes: make(map[string]*remoteRouter),
}
}
func (r *s2sRouter) Route(ctx context.Context, stanza xmpp.Stanza, localDomain string) error {
remoteDomain := stanza.ToJID().Domain()
r.mu.RLock()
rr := r.remotes[remoteDomain]
r.mu.RUnlock()
if rr == nil {
r.mu.Lock()
rr = r.remotes[remoteDomain] // avoid double initialization
if rr == nil {
rr = newRemoteRouter(localDomain, remoteDomain, r.outProvider)
r.remotes[remoteDomain] = rr
}
r.mu.Unlock()
}
rr.route(ctx, stanza)
return nil
}