mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-10-21 23:30:15 +08:00
Inflght and retained messages persistence
This commit is contained in:
@@ -62,29 +62,8 @@ func (s *Store) Close() {
|
||||
s.db.Close()
|
||||
}
|
||||
|
||||
// StoreSubscriptions writes all subscriptions to the boltdb instance as
|
||||
// a bulk operation.
|
||||
func (s *Store) StoreSubscriptions() {
|
||||
|
||||
}
|
||||
|
||||
// StoreClients writes all clients to the boltdb instance as a bulk operation.
|
||||
func (s *Store) StoreClients() {
|
||||
|
||||
}
|
||||
|
||||
// StoreInflight writes all inflight messages to the boltdb instance as a bulk operation.
|
||||
func (s *Store) StoreInflight() {
|
||||
|
||||
}
|
||||
|
||||
// StoreInflight writes all inflight messages to the boltdb instance as a bulk operation.
|
||||
func (s *Store) StoreRetained() {
|
||||
|
||||
}
|
||||
|
||||
// StoreServerInfo writes the server info to the boltdb instance.
|
||||
func (s *Store) StoreServerInfo(v persistence.ServerInfo) error {
|
||||
// WriteServerInfo writes the server info to the boltdb instance.
|
||||
func (s *Store) WriteServerInfo(v persistence.ServerInfo) error {
|
||||
err := s.db.Save(&v)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -102,8 +81,21 @@ func (s *Store) WriteSubscription(v persistence.Subscription) error {
|
||||
}
|
||||
|
||||
// WriteInflight writes a single inflight message to the boltdb instance.
|
||||
func (s *Store) WriteInflight() {
|
||||
func (s *Store) WriteInflight(v persistence.Message) error {
|
||||
err := s.db.Save(&v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteRetained writes a single retained message to the boltdb instance.
|
||||
func (s *Store) WriteRetained(v persistence.Message) error {
|
||||
err := s.db.Save(&v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteClient writes a single client to the boltdb instance.
|
||||
@@ -111,28 +103,41 @@ func (s *Store) WriteClient() {
|
||||
|
||||
}
|
||||
|
||||
// LoadSubscriptions loads all the subscriptions from the boltdb instance.
|
||||
func (s *Store) LoadSubscriptions() (v []persistence.Subscription, err error) {
|
||||
err = s.db.Find("T", "subscription", &v)
|
||||
// ReadSubscriptions loads all the subscriptions from the boltdb instance.
|
||||
func (s *Store) ReadSubscriptions() (v []persistence.Subscription, err error) {
|
||||
err = s.db.Find("T", persistence.KSubscription, &v)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// LoadClients loads all the clients from the boltdb instance.
|
||||
func (s *Store) LoadClients() {
|
||||
// ReadClients loads all the clients from the boltdb instance.
|
||||
func (s *Store) ReadClients() {
|
||||
|
||||
}
|
||||
|
||||
// LoadInflight loads all the inflight messages from the boltdb instance.
|
||||
func (s *Store) LoadInflight() {
|
||||
|
||||
}
|
||||
|
||||
// LoadServerInfo loads the server info from the boltdb instance.
|
||||
func (s *Store) LoadServerInfo() (v persistence.ServerInfo, err error) {
|
||||
err = s.db.One("ID", "server_info", &v)
|
||||
// ReadInflight loads all the inflight messages from the boltdb instance.
|
||||
func (s *Store) ReadInflight() (v []persistence.Message, err error) {
|
||||
err = s.db.Find("T", persistence.KInflight, &v)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ReadRetained loads all the retained messages from the boltdb instance.
|
||||
func (s *Store) ReadRetained() (v []persistence.Message, err error) {
|
||||
err = s.db.Find("T", persistence.KRetained, &v)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//ReadServerInfo loads the server info from the boltdb instance.
|
||||
func (s *Store) ReadServerInfo() (v persistence.ServerInfo, err error) {
|
||||
err = s.db.One("ID", persistence.KServerInfo, &v)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@@ -58,10 +58,10 @@ func TestStoreAndRetrieveServerInfo(t *testing.T) {
|
||||
Version: "test",
|
||||
Started: 100,
|
||||
}
|
||||
err = s.StoreServerInfo(persistence.ServerInfo{v, "server_info"})
|
||||
err = s.WriteServerInfo(persistence.ServerInfo{v, persistence.KServerInfo})
|
||||
require.NoError(t, err)
|
||||
|
||||
r, err := s.LoadServerInfo()
|
||||
r, err := s.ReadServerInfo()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, r)
|
||||
require.Equal(t, v.Version, r.Version)
|
||||
@@ -79,7 +79,7 @@ func TestWriteAndRetrieveSubscription(t *testing.T) {
|
||||
Client: "test",
|
||||
Filter: "a/b/c",
|
||||
QoS: 1,
|
||||
T: "subscription",
|
||||
T: persistence.KSubscription,
|
||||
}
|
||||
err = s.WriteSubscription(v)
|
||||
require.NoError(t, err)
|
||||
@@ -89,13 +89,92 @@ func TestWriteAndRetrieveSubscription(t *testing.T) {
|
||||
Client: "test",
|
||||
Filter: "d/e/f",
|
||||
QoS: 2,
|
||||
T: "subscription",
|
||||
T: persistence.KSubscription,
|
||||
}
|
||||
err = s.WriteSubscription(v2)
|
||||
require.NoError(t, err)
|
||||
|
||||
subs, err := s.LoadSubscriptions()
|
||||
subs, err := s.ReadSubscriptions()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, persistence.KSubscription, subs[0].T)
|
||||
require.Equal(t, 2, len(subs))
|
||||
|
||||
}
|
||||
|
||||
func TestWriteAndRetrieveInflight(t *testing.T) {
|
||||
s := New(tmpPath, nil)
|
||||
err := s.Open()
|
||||
require.NoError(t, err)
|
||||
defer teardown(t)
|
||||
|
||||
v := persistence.Message{
|
||||
ID: "client1_if_0",
|
||||
T: persistence.KInflight,
|
||||
PacketID: 0,
|
||||
TopicName: "a/b/c",
|
||||
Payload: []byte{'h', 'e', 'l', 'l', 'o'},
|
||||
Sent: 100,
|
||||
Resends: 0,
|
||||
}
|
||||
err = s.WriteInflight(v)
|
||||
require.NoError(t, err)
|
||||
|
||||
v2 := persistence.Message{
|
||||
ID: "client1_if_100",
|
||||
T: persistence.KInflight,
|
||||
PacketID: 100,
|
||||
TopicName: "d/e/f",
|
||||
Payload: []byte{'y', 'e', 's'},
|
||||
Sent: 200,
|
||||
Resends: 1,
|
||||
}
|
||||
err = s.WriteInflight(v2)
|
||||
require.NoError(t, err)
|
||||
|
||||
msgs, err := s.ReadInflight()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, persistence.KInflight, msgs[0].T)
|
||||
require.Equal(t, 2, len(msgs))
|
||||
}
|
||||
|
||||
func TestWriteAndRetrievePersistent(t *testing.T) {
|
||||
s := New(tmpPath, nil)
|
||||
err := s.Open()
|
||||
require.NoError(t, err)
|
||||
defer teardown(t)
|
||||
|
||||
v := persistence.Message{
|
||||
ID: "client1_ret_200",
|
||||
T: persistence.KRetained,
|
||||
FixedHeader: persistence.FixedHeader{
|
||||
Retain: true,
|
||||
},
|
||||
PacketID: 200,
|
||||
TopicName: "a/b/c",
|
||||
Payload: []byte{'h', 'e', 'l', 'l', 'o'},
|
||||
Sent: 100,
|
||||
Resends: 0,
|
||||
}
|
||||
err = s.WriteInflight(v)
|
||||
require.NoError(t, err)
|
||||
|
||||
v2 := persistence.Message{
|
||||
ID: "client1_ret_300",
|
||||
T: persistence.KRetained,
|
||||
FixedHeader: persistence.FixedHeader{
|
||||
Retain: true,
|
||||
},
|
||||
PacketID: 100,
|
||||
TopicName: "d/e/f",
|
||||
Payload: []byte{'y', 'e', 's'},
|
||||
Sent: 200,
|
||||
Resends: 1,
|
||||
}
|
||||
err = s.WriteInflight(v2)
|
||||
require.NoError(t, err)
|
||||
|
||||
msgs, err := s.ReadRetained()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, persistence.KRetained, msgs[0].T)
|
||||
require.Equal(t, true, msgs[0].FixedHeader.Retain)
|
||||
require.Equal(t, 2, len(msgs))
|
||||
}
|
||||
|
@@ -6,6 +6,14 @@ import (
|
||||
"github.com/mochi-co/mqtt/server/system"
|
||||
)
|
||||
|
||||
const (
|
||||
KSubscription = "sub"
|
||||
KServerInfo = "srv"
|
||||
KRetained = "ret"
|
||||
KInflight = "ifm"
|
||||
KClient = "cl"
|
||||
)
|
||||
|
||||
// Store is an interface which details a persistent storage connector.
|
||||
type Store interface {
|
||||
Open() error
|
||||
@@ -13,17 +21,15 @@ type Store interface {
|
||||
|
||||
WriteSubscription() // including retained
|
||||
WriteClient()
|
||||
WriteInFlight()
|
||||
WriteInflight()
|
||||
WriteServerInfo()
|
||||
WriteRetained()
|
||||
|
||||
StoreSubscriptions()
|
||||
StoreInFlight()
|
||||
StoreClients()
|
||||
StoreServerInfo(v ServerInfo) error
|
||||
|
||||
ReadSubscriptions() (v []Subscription, err error)
|
||||
ReadInflight()
|
||||
ReadRetained()
|
||||
ReadClients()
|
||||
|
||||
ReadServerInfo()
|
||||
}
|
||||
|
||||
@@ -112,25 +118,6 @@ func (s *MockStore) Close() {
|
||||
s.Closed = true
|
||||
}
|
||||
|
||||
// StoreSubscriptions writes all subscriptions to the storage instance.
|
||||
func (s *MockStore) StoreSubscriptions() {
|
||||
|
||||
}
|
||||
|
||||
// StoreClients writes all clients to the storage instance.
|
||||
func (s *MockStore) StoreClients() {}
|
||||
|
||||
// StoreInFlight writes all Inflight messages to the storage instance.
|
||||
func (s *MockStore) StoreInflight() {}
|
||||
|
||||
// StoreRetained writes all Inflight messages to the storage instance.
|
||||
func (s *MockStore) StoreRetained() {}
|
||||
|
||||
// StoreServerInfo writes the server info to the storage instance.
|
||||
func (s *MockStore) StoreServerInfo(v ServerInfo) {
|
||||
|
||||
}
|
||||
|
||||
// WriteSubscription writes a single subscription to the storage instance.
|
||||
func (s *MockStore) WriteSubscription() {}
|
||||
|
||||
@@ -143,18 +130,18 @@ func (s *MockStore) WriteInflight() {}
|
||||
// WriteRetained writes a single retained message to the storage instance.
|
||||
func (s *MockStore) WriteRetained() {}
|
||||
|
||||
// LoadSubscriptions loads the subscriptions from the storage instance.
|
||||
func (s *MockStore) LoadSubscriptions() (v []Subscription, err error) {
|
||||
// ReadSubscriptions loads the subscriptions from the storage instance.
|
||||
func (s *MockStore) ReadSubscriptions() (v []Subscription, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// LoadClients loads the clients from the storage instance.
|
||||
func (s *MockStore) LoadClients() {}
|
||||
// ReadClients loads the clients from the storage instance.
|
||||
func (s *MockStore) ReadClients() {}
|
||||
|
||||
// LoadInflight loads the inflight messages from the storage instance.
|
||||
func (s *MockStore) LoadInflight() {}
|
||||
// ReadInflight loads the inflight messages from the storage instance.
|
||||
func (s *MockStore) ReadInflight() {}
|
||||
|
||||
// LoadServerInfo loads the server info from the storage instance.
|
||||
func (s *MockStore) LoadServerInfo() (v ServerInfo, err error) {
|
||||
//ReadServerInfo loads the server info from the storage instance.
|
||||
func (s *MockStore) ReadServerInfo() (v ServerInfo, err error) {
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user