fix p2p permission refresh during load tests
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tinode/chat/server/logs"
|
||||
"github.com/tinode/chat/server/store/types"
|
||||
)
|
||||
|
||||
const p2pPermissionsRefreshedEvent = "p2p.permissions.refreshed"
|
||||
|
||||
type p2pPermissionsPayload struct {
|
||||
UserIDs []string `json:"user_ids"`
|
||||
}
|
||||
|
||||
// publishIMP2PPermissionsRefresh asks the topic master to reload permissions
|
||||
// after the HTTP API has repaired or created the two P2P subscriptions.
|
||||
func publishIMP2PPermissionsRefresh(tenantID types.TenantID, topic string, uids ...types.Uid) {
|
||||
topic = strings.TrimSpace(topic)
|
||||
if !tenantID.IsValid() || topic == "" {
|
||||
return
|
||||
}
|
||||
payload := p2pPermissionsPayload{UserIDs: make([]string, 0, len(uids))}
|
||||
for _, uid := range uids {
|
||||
if !uid.IsZero() {
|
||||
payload.UserIDs = append(payload.UserIDs, uid.UserId())
|
||||
}
|
||||
}
|
||||
if len(payload.UserIDs) == 0 {
|
||||
return
|
||||
}
|
||||
encoded, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
logs.Warn.Printf("p2p permissions refresh: encode topic=%s: %v", topic, err)
|
||||
return
|
||||
}
|
||||
message := &ServerComMessage{
|
||||
TenantID: tenantID,
|
||||
RcptTo: topic,
|
||||
Timestamp: time.Now().UTC().Round(time.Millisecond),
|
||||
Info: &MsgServerInfo{
|
||||
Topic: topic, What: "p2p", Event: p2pPermissionsRefreshedEvent,
|
||||
Payload: json.RawMessage(encoded),
|
||||
},
|
||||
}
|
||||
if globals.hub != nil {
|
||||
select {
|
||||
case globals.hub.routeSrv <- message:
|
||||
default:
|
||||
logs.Warn.Printf("p2p permissions refresh: hub queue full topic=%s", topic)
|
||||
}
|
||||
}
|
||||
publishRealtimeTopicMessage(topic, message)
|
||||
}
|
||||
Reference in New Issue
Block a user