This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from askuy/feature/cronlock
cron redis lock
- Loading branch information
Showing
18 changed files
with
250 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ecronlock | ||
|
||
import ( | ||
"context" | ||
"github.com/gotomicro/ego-component/eredis" | ||
"github.com/gotomicro/ego/core/elog" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Component struct { | ||
name string | ||
config *Config | ||
logger *elog.Component | ||
client *eredis.Component | ||
mutuex sync.RWMutex | ||
locker *eredis.Lock | ||
} | ||
|
||
func newComponent(name string, config *Config, logger *elog.Component, client *eredis.Component) *Component { | ||
reg := &Component{ | ||
name: name, | ||
logger: logger, | ||
client: client, | ||
} | ||
return reg | ||
} | ||
|
||
func (c *Component) Lock(ctx context.Context, key string, ttl time.Duration) error { | ||
locker := c.client.LockClient() | ||
lock, err := locker.Obtain(ctx, key, ttl, eredis.WithLockOptionRetryStrategy(eredis.LinearBackoffRetry(ttl))) | ||
if err != nil { | ||
return err | ||
} | ||
c.mutuex.Lock() | ||
c.locker = lock | ||
c.mutuex.Unlock() | ||
return nil | ||
} | ||
|
||
func (c *Component) Unlock(ctx context.Context, key string) error { | ||
c.mutuex.Lock() | ||
locker := c.locker | ||
c.mutuex.Unlock() | ||
if locker == nil { | ||
return nil | ||
} | ||
|
||
err := c.locker.Release(ctx) | ||
if err != nil { | ||
c.logger.Warn("cron unlock warning", elog.FieldErr(err)) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Component) Refresh(ctx context.Context, key string, ttl time.Duration) error { | ||
c.mutuex.Lock() | ||
locker := c.locker | ||
c.mutuex.Unlock() | ||
if locker == nil { | ||
return nil | ||
} | ||
return locker.Refresh(ctx, ttl) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ecronlock | ||
|
||
// Config ... | ||
type Config struct { | ||
OnFailHandle string | ||
} | ||
|
||
// DefaultConfig ... | ||
func DefaultConfig() *Config { | ||
return &Config{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ecronlock | ||
|
||
import ( | ||
"github.com/gotomicro/ego-component/eredis" | ||
"github.com/gotomicro/ego/core/econf" | ||
"github.com/gotomicro/ego/core/elog" | ||
) | ||
|
||
type Option func(c *Container) | ||
|
||
type Container struct { | ||
config *Config | ||
name string | ||
logger *elog.Component | ||
client *eredis.Component | ||
} | ||
|
||
func DefaultContainer() *Container { | ||
return &Container{ | ||
config: DefaultConfig(), | ||
logger: elog.EgoLogger.With(elog.FieldComponent(eredis.PackageName)), | ||
} | ||
} | ||
|
||
func Load(key string) *Container { | ||
c := DefaultContainer() | ||
if err := econf.UnmarshalKey(key, &c.config); err != nil { | ||
c.logger.Panic("parse Config error", elog.FieldErr(err), elog.FieldKey(key)) | ||
return c | ||
} | ||
c.logger = c.logger.With(elog.FieldComponentName(key)) | ||
c.name = key | ||
return c | ||
} | ||
|
||
func WithClientRedis(kubernetes *eredis.Component) Option { | ||
return func(c *Container) { | ||
c.client = kubernetes | ||
} | ||
} | ||
|
||
// Build ... | ||
func (c *Container) Build(options ...Option) *Component { | ||
for _, option := range options { | ||
option(c) | ||
} | ||
if c.client == nil { | ||
if c.config.OnFailHandle == "panic" { | ||
c.logger.Panic("client redis nil", elog.FieldKey("use WithClientRedis method")) | ||
} else { | ||
c.logger.Error("client redis nil", elog.FieldKey("use WithClientRedis method")) | ||
} | ||
} | ||
return newComponent(c.name, c.config, c.logger, c.client) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[redis.test] | ||
debug = true # ego增加redis debug,打开后可以看到,配置名、地址、耗时、请求数据、响应数据 | ||
addr = "127.0.0.1:6379" | ||
enableAccessInterceptor = true | ||
enableAccessInterceptorReq = true | ||
enableAccessInterceptorRes = true | ||
[cron.default] | ||
withSeconds = false | ||
concurrentDelay = 1 | ||
immediatelyRun = false | ||
distributedTask = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gotomicro/ego" | ||
"github.com/gotomicro/ego-component/eredis/ecronlock" | ||
"github.com/gotomicro/ego/core/elog" | ||
"github.com/gotomicro/ego/task/ecron" | ||
"time" | ||
|
||
"github.com/gotomicro/ego-component/eredis" | ||
) | ||
|
||
// export EGO_DEBUG=true && go run main.go --config=config.toml | ||
func main() { | ||
err := ego.New().Cron(cronJob()).Run() | ||
if err != nil { | ||
elog.Panic("startup", elog.FieldErr(err)) | ||
} | ||
} | ||
|
||
var eredisClient *eredis.Component | ||
|
||
func cronJob() ecron.Ecron { | ||
lock := ecronlock.Load("").Build(ecronlock.WithClientRedis(eredis.Load("redis.test").Build())) | ||
cron := ecron.Load("cron.default").Build(ecron.WithLocker(lock)) | ||
cron.Schedule(ecron.Every(time.Second*10), ecron.FuncJob(helloWorld)) | ||
return cron | ||
} | ||
|
||
func helloWorld() error { | ||
elog.Info("info job") | ||
elog.Warn("warn job") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.