Documentation
¶
Overview ¶
glloq implements advisory locks on various backends.
Checkout https://github.com/gilbsgilbs/glloq for a quick overview.
Example ¶
The simplest way to use glloq is by using a data source name (DSN). UseLocker will take care of opening and closing any connection or socket, will wait for the backend to be available and will hold the lock and release it when you're finished.
package main
import (
"time"
"github.com/gilbsgilbs/glloq"
"github.com/gilbsgilbs/glloq/anylocker"
)
func main() {
err := glloq.UseLocker(
&anylocker.Locker{},
&glloq.Options{
// This is a connection string to your backend. For SQL-based backends,
// dburl (https://github.com/xo/dburl) is used.
DSN: "postgres://user:password@localhost:5432/db?sslmode=disable",
// DSN: "mysql://user:password@localhost:3006/db",
// DSN: "file://.lock",
// Maximum time to wait for the backend and the lock. Defaults to 1 minute.
Timeout: 1 * time.Hour,
// An optional lock key, if supported by the backend.
Key: "someUniqueKey",
// backend-specific parameters.
Params: map[string]string{},
},
func() error {
// You can run any synchronized operation here, such as database migrations.
// It won't run concurrently.
return nil
},
)
if err != nil {
panic(err)
}
}
Output:
Example (FileLocker) ¶
The file locker allows you take a lock using a local file.
package main
import (
"context"
"github.com/gilbsgilbs/glloq"
"github.com/gilbsgilbs/glloq/filelocker"
)
func main() {
locker := filelocker.Locker{}
err := locker.WithLock(
context.Background(),
&glloq.Options{
DSN: "file:///tmp/myAppLockFile.lock",
},
func() error {
// ...
return nil
},
)
if err != nil {
panic(err)
}
}
Output:
Example (MysqlLocker) ¶
package main
import (
"context"
"database/sql"
"github.com/gilbsgilbs/glloq"
"github.com/gilbsgilbs/glloq/mysqllocker"
)
func main() {
var db *sql.DB
locker := mysqllocker.Locker{}
locker.DB = db
err := locker.WithLock(
context.Background(),
&glloq.Options{
Params: map[string]string{
// Name of the table that will be created to take locks.
// Defaults to glloq.
"table_name": "my_lock_table",
},
},
func() error {
// ...
return nil
},
)
if err != nil {
panic(err)
}
}
Output:
Example (PostgresLocker) ¶
package main
import (
"context"
"database/sql"
"github.com/gilbsgilbs/glloq"
"github.com/gilbsgilbs/glloq/postgreslocker"
)
func main() {
var db *sql.DB
locker := postgreslocker.Locker{}
locker.DB = db
err := locker.WithLock(
context.Background(),
&glloq.Options{
Params: map[string]string{
// Name of the table that will be created to take locks.
// Defaults to glloq.
"table_name": "my_lock_table",
},
},
func() error {
// ...
return nil
},
)
if err != nil {
panic(err)
}
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
Types ¶
type Locker ¶
type Locker interface {
// SupportsDSN returns true if the DSN is supported by the Locker.
SupportsDSN(dsn string) bool
// Open allows the locker to open a connection to the backend.
Open(ctx context.Context, dsn string) error
// Close allows the locker to close the connection to the backend
Close() error
// Holds the lock. Returns ErrTimeout if context is done.
WithLock(ctx context.Context, opts *Options, fn func() error) error
}
type Options ¶
type Options struct {
// DSN is the connection string to the database.
DSN string
// Key is a lock key.
Key string
// Timeout defines how long to wait for the backend to be up.
Timeout time.Duration
// Params are beckend-specific options.
Params map[string]string
}
Options lock options.
type SQLLocker ¶
SQLLocker implements some base locker methods for SQL-based backends.
func (*SQLLocker) DBUrlDriver ¶
Click to show internal directories.
Click to hide internal directories.