Skip to content

Commit 622fdd7

Browse files
authored
Merge pull request #26 from guilt/master
Add hooks for onUnsubscribe, before unsubscribe happens.
2 parents 132bfa3 + a19d6da commit 622fdd7

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Gmqtt implements the following hooks:
9595
* OnSessionTerminated
9696
* OnSubscribe
9797
* OnSubscribed
98+
* OnUnsubscribe
9899
* OnUnsubscribed
99100
* OnMsgArrived
100101
* OnAcked

README_ZH.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Gmqtt实现了下列钩子方法
9494
* OnSessionTerminated
9595
* OnSubscribe
9696
* OnSubscribed
97+
* OnUnsubscribe
9798
* OnUnsubscribed
9899
* OnMsgArrived
99100
* OnAcked
@@ -129,4 +130,4 @@ $ go test -race .
129130
* 支持MQTT V3和V5
130131
* 桥接模式,集群模式(看情况)
131132

132-
*暂时不保证向后兼容,在添加上述新功能时可能会有breaking changes。*
133+
*暂时不保证向后兼容,在添加上述新功能时可能会有breaking changes。*

client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,9 @@ func (client *client) unsubscribeHandler(unSub *packets.Unsubscribe) {
667667
unSuback := unSub.NewUnSubBack()
668668
client.write(unSuback)
669669
for _, topicName := range unSub.Topics {
670+
if srv.hooks.OnUnsubscribe != nil {
671+
srv.hooks.OnUnsubscribe(context.Background(), client, topicName)
672+
}
670673
srv.subscriptionsDB.Unsubscribe(client.opts.clientID, topicName)
671674
if srv.hooks.OnUnsubscribed != nil {
672675
srv.hooks.OnUnsubscribed(context.Background(), client, topicName)

hook.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Hooks struct {
1212
OnStop
1313
OnSubscribe
1414
OnSubscribed
15+
OnUnsubscribe
1516
OnUnsubscribed
1617
OnMsgArrived
1718
OnConnect
@@ -55,6 +56,11 @@ type OnSubscribed func(ctx context.Context, client Client, topic packets.Topic)
5556

5657
type OnSubscribedWrapper func(OnSubscribed) OnSubscribed
5758

59+
// OnUnsubscribe will be called when the topic is being unsubscribed
60+
type OnUnsubscribe func(ctx context.Context, client Client, topicName string)
61+
62+
type OnUnsubscribeWrapper func(OnUnsubscribe) OnUnsubscribe
63+
5864
// OnUnsubscribed will be called after the topic has been unsubscribed
5965
type OnUnsubscribed func(ctx context.Context, client Client, topicName string)
6066

plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type HookWrapper struct {
99
OnSessionTerminatedWrapper OnSessionTerminatedWrapper
1010
OnSubscribeWrapper OnSubscribeWrapper
1111
OnSubscribedWrapper OnSubscribedWrapper
12+
OnUnsubscribeWrapper OnUnsubscribeWrapper
1213
OnUnsubscribedWrapper OnUnsubscribedWrapper
1314
OnMsgArrivedWrapper OnMsgArrivedWrapper
1415
OnAckedWrapper OnAckedWrapper

server.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ func (srv *server) loadPlugins() error {
689689
onSessionTerminatedWrapper []OnSessionTerminatedWrapper
690690
onSubscribeWrappers []OnSubscribeWrapper
691691
onSubscribedWrappers []OnSubscribedWrapper
692+
onUnsubscribeWrappers []OnUnsubscribeWrapper
692693
onUnsubscribedWrappers []OnUnsubscribedWrapper
693694
onMsgArrivedWrappers []OnMsgArrivedWrapper
694695
onDeliverWrappers []OnDeliverWrapper
@@ -729,6 +730,9 @@ func (srv *server) loadPlugins() error {
729730
if hooks.OnSubscribedWrapper != nil {
730731
onSubscribedWrappers = append(onSubscribedWrappers, hooks.OnSubscribedWrapper)
731732
}
733+
if hooks.OnUnsubscribeWrapper != nil {
734+
onUnsubscribeWrappers = append(onUnsubscribeWrappers, hooks.OnUnsubscribeWrapper)
735+
}
732736
if hooks.OnUnsubscribedWrapper != nil {
733737
onUnsubscribedWrappers = append(onUnsubscribedWrappers, hooks.OnUnsubscribedWrapper)
734738
}
@@ -751,6 +755,7 @@ func (srv *server) loadPlugins() error {
751755
onStopWrappers = append(onStopWrappers, hooks.OnStopWrapper)
752756
}
753757
}
758+
754759
// onAccept
755760
if onAcceptWrappers != nil {
756761
onAccept := func(ctx context.Context, conn net.Conn) bool {
@@ -761,6 +766,7 @@ func (srv *server) loadPlugins() error {
761766
}
762767
srv.hooks.OnAccept = onAccept
763768
}
769+
764770
// onConnect
765771
if onConnectWrappers != nil {
766772
onConnect := func(ctx context.Context, client Client) (code uint8) {
@@ -771,6 +777,7 @@ func (srv *server) loadPlugins() error {
771777
}
772778
srv.hooks.OnConnect = onConnect
773779
}
780+
774781
// onConnected
775782
if onConnectedWrappers != nil {
776783
onConnected := func(ctx context.Context, client Client) {}
@@ -779,6 +786,7 @@ func (srv *server) loadPlugins() error {
779786
}
780787
srv.hooks.OnConnected = onConnected
781788
}
789+
782790
// onSessionCreated
783791
if onSessionCreatedWrapper != nil {
784792
onSessionCreated := func(ctx context.Context, client Client) {}
@@ -816,6 +824,7 @@ func (srv *server) loadPlugins() error {
816824
}
817825
srv.hooks.OnSubscribe = onSubscribe
818826
}
827+
819828
// onSubscribed
820829
if onSubscribedWrappers != nil {
821830
onSubscribed := func(ctx context.Context, client Client, topic packets.Topic) {}
@@ -824,6 +833,16 @@ func (srv *server) loadPlugins() error {
824833
}
825834
srv.hooks.OnSubscribed = onSubscribed
826835
}
836+
837+
//onUnsubscribe
838+
if onUnsubscribeWrappers != nil {
839+
onUnsubscribe := func(ctx context.Context, client Client, topicName string) {}
840+
for i := len(onUnsubscribeWrappers); i > 0; i-- {
841+
onUnsubscribe = onUnsubscribeWrappers[i-1](onUnsubscribe)
842+
}
843+
srv.hooks.OnUnsubscribe = onUnsubscribe
844+
}
845+
827846
//onUnsubscribed
828847
if onUnsubscribedWrappers != nil {
829848
onUnsubscribed := func(ctx context.Context, client Client, topicName string) {}
@@ -832,6 +851,7 @@ func (srv *server) loadPlugins() error {
832851
}
833852
srv.hooks.OnUnsubscribed = onUnsubscribed
834853
}
854+
835855
// onMsgArrived
836856
if onMsgArrivedWrappers != nil {
837857
onMsgArrived := func(ctx context.Context, client Client, msg packets.Message) (valid bool) {
@@ -842,6 +862,7 @@ func (srv *server) loadPlugins() error {
842862
}
843863
srv.hooks.OnMsgArrived = onMsgArrived
844864
}
865+
845866
// onDeliver
846867
if onDeliverWrappers != nil {
847868
onDeliver := func(ctx context.Context, client Client, msg packets.Message) {}
@@ -850,6 +871,7 @@ func (srv *server) loadPlugins() error {
850871
}
851872
srv.hooks.OnDeliver = onDeliver
852873
}
874+
853875
// onAcked
854876
if onAckedWrappers != nil {
855877
onAcked := func(ctx context.Context, client Client, msg packets.Message) {}
@@ -858,6 +880,7 @@ func (srv *server) loadPlugins() error {
858880
}
859881
srv.hooks.OnAcked = onAcked
860882
}
883+
861884
// onClose hooks
862885
if onCloseWrappers != nil {
863886
onClose := func(ctx context.Context, client Client, err error) {}
@@ -866,6 +889,7 @@ func (srv *server) loadPlugins() error {
866889
}
867890
srv.hooks.OnClose = onClose
868891
}
892+
869893
// onStop
870894
if onStopWrappers != nil {
871895
onStop := func(ctx context.Context) {}

0 commit comments

Comments
 (0)