fix kafka message persistence completion
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tinode/chat/server/logs"
|
||||
@@ -20,6 +21,9 @@ const (
|
||||
|
||||
imYouthActionMessage = "message"
|
||||
imYouthActionCall = "call"
|
||||
|
||||
imSecurityPolicyCacheTTL = 5 * time.Second
|
||||
imSecurityPolicyErrorCacheTTL = 2 * time.Second
|
||||
)
|
||||
|
||||
type imPolicyDecision struct {
|
||||
@@ -29,6 +33,17 @@ type imPolicyDecision struct {
|
||||
Details map[string]any
|
||||
}
|
||||
|
||||
type imSecurityPolicyCacheEntry struct {
|
||||
policy *types.SecurityPolicy
|
||||
err error
|
||||
expires time.Time
|
||||
}
|
||||
|
||||
var imSecurityPolicyCache = struct {
|
||||
sync.Mutex
|
||||
entries map[string]imSecurityPolicyCacheEntry
|
||||
}{entries: make(map[string]imSecurityPolicyCacheEntry)}
|
||||
|
||||
func imAllowDecision() imPolicyDecision {
|
||||
return imPolicyDecision{Allowed: true}
|
||||
}
|
||||
@@ -119,7 +134,28 @@ func imSecurityPolicy(tenantID types.TenantID, tenantCode string) (*types.Securi
|
||||
if tenantCode == "" {
|
||||
return nil, types.ErrNotFound
|
||||
}
|
||||
key := fmt.Sprintf("%d:%s", tenantID, tenantCode)
|
||||
now := time.Now()
|
||||
imSecurityPolicyCache.Lock()
|
||||
if cached, ok := imSecurityPolicyCache.entries[key]; ok && now.Before(cached.expires) {
|
||||
imSecurityPolicyCache.Unlock()
|
||||
return cached.policy, cached.err
|
||||
}
|
||||
if store.ModerationConfig == nil {
|
||||
imSecurityPolicyCache.Unlock()
|
||||
return nil, types.ErrUnsupported
|
||||
}
|
||||
policy, err := store.ModerationConfig.SecurityPolicy(tenantCode)
|
||||
ttl := imSecurityPolicyCacheTTL
|
||||
if err != nil {
|
||||
ttl = imSecurityPolicyErrorCacheTTL
|
||||
}
|
||||
imSecurityPolicyCache.entries[key] = imSecurityPolicyCacheEntry{
|
||||
policy: policy,
|
||||
err: err,
|
||||
expires: now.Add(ttl),
|
||||
}
|
||||
imSecurityPolicyCache.Unlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user