This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
/
service.go
124 lines (105 loc) · 3.31 KB
/
service.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2015-present Oursky Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package subscription
import (
"time"
"github.com/sirupsen/logrus"
"github.com/skygeario/skygear-server/pkg/server/skydb"
)
var timeNow = time.Now
// Service is responsible to send push notification to device whenever
// a record has been modified in db.
type Service struct {
ConnOpener func() (skydb.Conn, error)
Notifier Notifier
stop chan struct{}
}
// Run listens for Conn record event
func (s *Service) Run() {
// maximum number of events per second
const EventCountBits = 28
const EventCountMask = 1<<EventCountBits - 1
var (
// number of events processed, reset per second
eventCount uint
prevUnix = timeNow().Unix()
recordEventCh = s.subscribe()
)
s.stop = make(chan struct{})
defer func() { s.stop = nil }()
for {
select {
case event := <-recordEventCh:
switch event.Event {
case skydb.RecordCreated, skydb.RecordUpdated, skydb.RecordDeleted:
conn, err := s.ConnOpener()
if err != nil {
log.WithFields(logrus.Fields{
"event": event,
"err": err,
}).Errorln("subscription: failed to open skydb.Conn")
continue
}
currUnix := timeNow().Unix()
if currUnix != prevUnix {
eventCount = 0
prevUnix = currUnix
}
seqNum := uint64(currUnix)<<EventCountBits | uint64(eventCount)&EventCountMask
eventCount++
db := getDB(conn, event.Record)
s.handleRecordHook(db, event, seqNum)
default:
log.Panicf("subscription: unrecgonized event: %v", event)
}
case <-s.stop:
log.Infoln("subscription: stopping the service")
return
}
}
}
// Stop stops the running subscription service
func (s *Service) Stop() {
s.stop <- struct{}{}
}
func (s *Service) subscribe() chan skydb.RecordEvent {
conn, err := s.ConnOpener()
if err != nil {
log.Panicf("subscription: failed to obtain connection: %v", err)
}
ch := make(chan skydb.RecordEvent)
conn.Subscribe(ch)
return ch
}
func (s *Service) handleRecordHook(db skydb.Database, e skydb.RecordEvent, seqNum uint64) {
subscriptions := db.GetMatchingSubscriptions(e.Record)
device := skydb.Device{}
for _, subscription := range subscriptions {
log.Printf("subscription: got a matching sub id = %s", subscription.ID)
conn := db.Conn()
if err := conn.GetDevice(subscription.DeviceID, &device); err != nil {
log.Panicf("subscription: failed to get device with id = %v: %v", subscription.DeviceID, err)
}
notice := Notice{seqNum, subscription.ID, e.Event, e.Record}
if err := s.Notifier.Notify(device, notice); err != nil {
log.Errorf("subscription: failed to send notice to device id = %s", device.ID)
}
}
}
func getDB(conn skydb.Conn, record *skydb.Record) skydb.Database {
if record.DatabaseID == "" {
return conn.PublicDB()
}
return conn.PrivateDB(record.DatabaseID)
}