cluster

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2020 License: Apache-2.0 Imports: 6 Imported by: 14

Documentation

Overview

package cluster exposes synchronization primitives to ensure correct behavior across multiple plugin instances in a Mattermost cluster.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	// contains filtered or unexported fields
}

Job is a scheduled job whose callback function is executed on a configured interval by at most one plugin instance at a time.

Use scheduled jobs to perform background activity on a regular interval without having to explicitly coordinate with other instances of the same plugin that might repeat that effort.

func Schedule

func Schedule(pluginAPI JobPluginAPI, key string, config JobConfig, callback func()) (*Job, error)

Schedule creates a scheduled job.

Example
// Use p.API from your plugin instead.
pluginAPI := NewMockMutexPluginAPI(nil)

callback := func() {
	// periodic work to do
}

job, err := Schedule(pluginAPI, "key", JobConfig{Interval: 5 * time.Minute}, callback)
if err != nil {
	panic("failed to schedule job")
}

// main thread

defer job.Close()

func (*Job) Close

func (j *Job) Close() error

Close terminates a scheduled job, preventing it from being scheduled on this plugin instance.

type JobConfig

type JobConfig struct {
	// Interval is the period of execution for the job.
	Interval time.Duration
}

JobConfig defines the configuration of a scheduled job.

type JobPluginAPI

type JobPluginAPI interface {
	MutexPluginAPI
	KVGet(key string) ([]byte, *model.AppError)
}

JobPluginAPI is the plugin API interface required to schedule jobs.

type Mutex

type Mutex struct {
	// contains filtered or unexported fields
}

Mutex is similar to sync.Mutex, except usable by multiple plugin instances across a cluster.

Internally, a mutex relies on an atomic key-value set operation as exposed by the Mattermost plugin API.

Mutexes with different names are unrelated. Mutexes with the same name from different plugins are unrelated. Pick a unique name for each mutex your plugin requires.

A Mutex must not be copied after first use.

Example
package main

import (
	"github.com/mattermost/mattermost-plugin-api/cluster"

	"github.com/mattermost/mattermost-server/v5/plugin"
)

func main() {
	// Use p.API from your plugin instead.
	pluginAPI := plugin.API(nil)

	m := cluster.NewMutex(pluginAPI, "key")
	m.Lock()
	// critical section
	m.Unlock()
}

func NewMutex

func NewMutex(pluginAPI MutexPluginAPI, key string) *Mutex

NewMutex creates a mutex with the given name.

func (*Mutex) Lock

func (m *Mutex) Lock()

Lock locks m. If the mutex is already locked by any plugin instance, including the current one, the calling goroutine blocks until the mutex can be locked.

func (*Mutex) Unlock

func (m *Mutex) Unlock()

Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock.

Just like sync.Mutex, a locked Lock is not associated with a particular goroutine or plugin instance. It is allowed for one goroutine or plugin instance to lock a Lock and then arrange for another goroutine or plugin instance to unlock it. In practice, ownership of the lock should remain within a single plugin instance.

type MutexPluginAPI

type MutexPluginAPI interface {
	KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError)
	LogError(msg string, keyValuePairs ...interface{})
}

MutexPluginAPI is the plugin API interface required to manage mutexes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL