-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced most pubsub handlers with tcf
- Loading branch information
1 parent
38a45cf
commit c03db86
Showing
8 changed files
with
184 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
package main | ||
|
||
import "github.com/TykTechnologies/tyk/pubsub" | ||
|
||
var PubSubServer *pubsub.PSServer | ||
|
||
|
||
package main | ||
|
||
import "github.com/TykTechnologies/tyk/pubsub" | ||
|
||
var PubSubServer *pubsub.PSServer | ||
var PubSubClient *pubsub.PSClient | ||
|
||
func StartPubSubServer() { | ||
if PubSubServer == nil { | ||
var err error | ||
p := config.PubSubServerPort | ||
if config.PubSubServerPort == "" { | ||
p = "1211" | ||
} | ||
PubSubServer, err = pubsub.NewPSServer(p) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
func StartGlobalClient(cs string) { | ||
PubSubClient = pubsub.NewPSClient() | ||
if err := PubSubClient.Start(cs); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/TykTechnologies/tyk-cluster-framework/payloads" | ||
) | ||
|
||
// RedisNotifier will use redis pub/sub channels to send notifications | ||
type TCFNotifier struct { | ||
channel string | ||
} | ||
|
||
// Notify will send a notification to a channel | ||
func (r *TCFNotifier) Notify(notification interface{}) bool { | ||
toSend, err := payloads.NewPayload(notification) | ||
if err != nil { | ||
log.Error("Problem marshalling notification: ", err) | ||
return false | ||
} | ||
log.Debug("Sending notification", notification) | ||
|
||
if PubSubClient == nil { | ||
log.Warning("Client is nil, can't send notification") | ||
return false | ||
} | ||
|
||
if err := PubSubClient.Publish(r.channel, toSend); err != nil { | ||
log.Error("Could not send notification: ", err) | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/Sirupsen/logrus" | ||
"github.com/TykTechnologies/tyk-cluster-framework/payloads" | ||
) | ||
|
||
func startSubscription() { | ||
if PubSubClient == nil { | ||
log.Info("Starting pub/sub client") | ||
// TODO: This must be set dynamically | ||
StartGlobalClient(config.PubSubMasterConnectionString) | ||
} | ||
|
||
if err := PubSubClient.Subscribe(RedisPubSubChannel, func(payload payloads.Payload) { | ||
handleNotificationEvent(payload, nil, nil) | ||
}); err != nil { | ||
log.WithFields(logrus.Fields{ | ||
"prefix": "pub-sub", | ||
"err": err, | ||
}).Error("Connection to Master pub/sub failed, reconnect in 10s") | ||
|
||
time.Sleep(10 * time.Second) | ||
log.WithFields(logrus.Fields{ | ||
"prefix": "pub-sub", | ||
}).Warning("Reconnecting") | ||
|
||
// TODO: This must be set dynamically | ||
PubSubClient.Start(config.PubSubMasterConnectionString) | ||
} | ||
} | ||
|
||
func handleNotificationEvent(v payloads.Payload, handled func(NotificationCommand), reloaded func()) { | ||
notif := Notification{} | ||
if err := v.DecodeMessage(¬if); err != nil { | ||
log.Error("Unmarshalling message body failed, malformed: ", err) | ||
return | ||
} | ||
|
||
// Add messages to ignore here | ||
switch notif.Command { | ||
case NoticeGatewayConfigResponse: | ||
return | ||
} | ||
|
||
// Check for a signature, if not signature found, handle | ||
if !isPayloadSignatureValid(notif) { | ||
log.WithFields(logrus.Fields{ | ||
"prefix": "pub-sub", | ||
}).Error("Payload signature is invalid!") | ||
return | ||
} | ||
|
||
switch notif.Command { | ||
case NoticeDashboardZeroConf: | ||
handleDashboardZeroConfMessage(notif.Payload) | ||
case NoticeConfigUpdate: | ||
handleNewConfiguration(notif.Payload) | ||
case NoticeDashboardConfigRequest: | ||
handleSendMiniConfig(notif.Payload) | ||
case NoticeGatewayDRLNotification: | ||
if config.ManagementNode { | ||
// DRL is not initialized, going through would | ||
// be mostly harmless but would flood the log | ||
// with warnings since DRLManager.Ready == false | ||
return | ||
} | ||
onServerStatusReceivedHandler(notif.Payload) | ||
case NoticeGatewayLENotification: | ||
onLESSLStatusReceivedHandler(notif.Payload) | ||
case NoticeApiUpdated, NoticeApiRemoved, NoticeApiAdded, NoticePolicyChanged, NoticeGroupReload: | ||
log.WithFields(logrus.Fields{ | ||
"prefix": "pub-sub", | ||
}).Info("Reloading endpoints") | ||
reloadURLStructure(reloaded) | ||
default: | ||
log.WithFields(logrus.Fields{ | ||
"prefix": "pub-sub", | ||
}).Warnf("Unknown tcf notification command: %q", notif.Command) | ||
return | ||
} | ||
if handled != nil { | ||
// went through. all others shoul have returned early. | ||
handled(notif.Command) | ||
} | ||
} |