Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
156 lines
3.7 KiB
Go
156 lines
3.7 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package platform
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/v8/einterfaces"
|
|
)
|
|
|
|
const (
|
|
TimestampFormat = "Mon Jan 2 15:04:05 -0700 MST 2006"
|
|
)
|
|
|
|
// Busy represents the busy state of the server. A server marked busy
|
|
// will have non-critical services disabled. If a Cluster is provided
|
|
// any changes will be propagated to each node.
|
|
type Busy struct {
|
|
busy int32 // protected via atomic for fast IsBusy calls
|
|
mux sync.RWMutex
|
|
timer *time.Timer
|
|
expires time.Time
|
|
|
|
cluster einterfaces.ClusterInterface
|
|
}
|
|
|
|
// NewBusy creates a new Busy instance with optional cluster which will
|
|
// be notified of busy state changes.
|
|
func NewBusy(cluster einterfaces.ClusterInterface) *Busy {
|
|
return &Busy{cluster: cluster}
|
|
}
|
|
|
|
// IsBusy returns true if the server has been marked as busy.
|
|
func (b *Busy) IsBusy() bool {
|
|
if b == nil {
|
|
return false
|
|
}
|
|
return atomic.LoadInt32(&b.busy) != 0
|
|
}
|
|
|
|
// Set marks the server as busy for dur duration and notifies cluster nodes.
|
|
func (b *Busy) Set(dur time.Duration) {
|
|
b.mux.Lock()
|
|
defer b.mux.Unlock()
|
|
|
|
// minimum 1 second
|
|
if dur < (time.Second * 1) {
|
|
dur = time.Second * 1
|
|
}
|
|
|
|
b.setWithoutNotify(dur)
|
|
|
|
if b.cluster != nil {
|
|
sbs := &model.ServerBusyState{Busy: true, Expires: b.expires.Unix(), ExpiresTS: b.expires.UTC().Format(TimestampFormat)}
|
|
b.notifyServerBusyChange(sbs)
|
|
}
|
|
}
|
|
|
|
// must hold mutex
|
|
func (b *Busy) setWithoutNotify(dur time.Duration) {
|
|
b.clearWithoutNotify()
|
|
atomic.StoreInt32(&b.busy, 1)
|
|
b.expires = time.Now().Add(dur)
|
|
b.timer = time.AfterFunc(dur, func() {
|
|
b.mux.Lock()
|
|
b.clearWithoutNotify()
|
|
b.mux.Unlock()
|
|
})
|
|
}
|
|
|
|
// ClearBusy marks the server as not busy and notifies cluster nodes.
|
|
func (b *Busy) Clear() {
|
|
b.mux.Lock()
|
|
defer b.mux.Unlock()
|
|
|
|
b.clearWithoutNotify()
|
|
|
|
if b.cluster != nil {
|
|
sbs := &model.ServerBusyState{Busy: false, Expires: time.Time{}.Unix(), ExpiresTS: ""}
|
|
b.notifyServerBusyChange(sbs)
|
|
}
|
|
}
|
|
|
|
// must hold mutex
|
|
func (b *Busy) clearWithoutNotify() {
|
|
if b.timer != nil {
|
|
b.timer.Stop() // don't drain timer.C channel for AfterFunc timers.
|
|
}
|
|
b.timer = nil
|
|
b.expires = time.Time{}
|
|
atomic.StoreInt32(&b.busy, 0)
|
|
}
|
|
|
|
// Expires returns the expected time that the server
|
|
// will be marked not busy. This expiry can be extended
|
|
// via additional calls to SetBusy.
|
|
func (b *Busy) Expires() time.Time {
|
|
b.mux.RLock()
|
|
defer b.mux.RUnlock()
|
|
return b.expires
|
|
}
|
|
|
|
// notifyServerBusyChange informs all cluster members of a server busy state change.
|
|
func (b *Busy) notifyServerBusyChange(sbs *model.ServerBusyState) {
|
|
if b.cluster == nil {
|
|
return
|
|
}
|
|
buf, _ := json.Marshal(sbs)
|
|
msg := &model.ClusterMessage{
|
|
Event: model.ClusterEventBusyStateChanged,
|
|
SendType: model.ClusterSendReliable,
|
|
WaitForAllToSend: true,
|
|
Data: buf,
|
|
}
|
|
b.cluster.SendClusterMessage(msg)
|
|
}
|
|
|
|
// ClusterEventChanged is called when a CLUSTER_EVENT_BUSY_STATE_CHANGED is received.
|
|
func (b *Busy) ClusterEventChanged(sbs *model.ServerBusyState) {
|
|
b.mux.Lock()
|
|
defer b.mux.Unlock()
|
|
|
|
if sbs.Busy {
|
|
expires := time.Unix(sbs.Expires, 0)
|
|
dur := time.Until(expires)
|
|
if dur > 0 {
|
|
b.setWithoutNotify(dur)
|
|
}
|
|
} else {
|
|
b.clearWithoutNotify()
|
|
}
|
|
}
|
|
|
|
func (b *Busy) ToJSON() ([]byte, error) {
|
|
b.mux.RLock()
|
|
defer b.mux.RUnlock()
|
|
|
|
sbs := &model.ServerBusyState{
|
|
Busy: atomic.LoadInt32(&b.busy) != 0,
|
|
Expires: b.expires.Unix(),
|
|
ExpiresTS: b.expires.UTC().Format(TimestampFormat),
|
|
}
|
|
sbsJSON, jsonErr := json.Marshal(sbs)
|
|
if jsonErr != nil {
|
|
return []byte{}, fmt.Errorf("failed to encode server busy state to JSON: %w", jsonErr)
|
|
}
|
|
|
|
return sbsJSON, nil
|
|
}
|