-
-
Notifications
You must be signed in to change notification settings - Fork 688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement reusing redis client #742
base: master
Are you sure you want to change the base?
Implement reusing redis client #742
Conversation
0d583af
to
a89a3ac
Compare
This is already possible without any changes to E.g. package pool
type (
SharedRedisPoolOpts struct {
DSN string
MinIdleConns int
}
SharedRedisPool struct {
Client *redis.Client
}
)
func NewSharedRedisPool(o SharedRedisPoolOpts) (*SharedRedisPool, error) {
redisOpts, err := redis.ParseURL(o.DSN)
if err != nil {
return nil, err
}
redisOpts.MinIdleConns = o.MinIdleConns
redisClient := redis.NewClient(redisOpts)
return &SharedRedisPool{
Client: redisClient,
}, nil
}
// Fulfills asynq's Interface
func (r *SharedRedisPool) MakeRedisClient() interface{} {
return r.Client
}
// Somewhere else when creating an asynq server asynq.NewServer()
// use *pool.SharedRedisPool |
Thanks! I knew it is already possible but it's not really that clear to users how they can do it and it doesn't protect you against accidental closing of the client. |
Right, it is not documented in the wiki. Maybe it should be added there.
True. Btw looks like some tests are failing. |
a89a3ac
to
1890659
Compare
1890659
to
9e548fc
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #742 +/- ##
==========================================
+ Coverage 68.37% 68.49% +0.11%
==========================================
Files 27 27
Lines 3836 3856 +20
==========================================
+ Hits 2623 2641 +18
- Misses 929 930 +1
- Partials 284 285 +1 ☔ View full report in Codecov by Sentry. |
Yep, fixed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some feedback on godocs and the Scheduler shutdown behavior with this change.
return inspector | ||
} | ||
|
||
// NewFromRedisClient returns a new instance of Inspector. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// NewInspectorFromRedisClient returns a new instance of Inspector given a redis.UniversalClient
// Warning: The underlying redis connection pool will not be closed by Asynq, you are responsible for closing it.
} | ||
|
||
// NewClientFromRedisClient returns a new Client instance given a redis client. | ||
// Warning: the redis client will not be closed by Asynq, you are responsible for closing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// NewClientFromRedisClient returns a new instance of Client given a redis.UniversalClient
// Warning: The underlying redis connection pool will not be closed by Asynq, you are responsible for closing it.
return scheduler | ||
} | ||
|
||
// NewSchedulerFromRedisClient returns a new Scheduler instance given a redis client. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// NewSchedulerFromRedisClient returns a new instance of Scheduler given a redis.UniversalClient
// The parameter opts is optional, defaults will be used if opts is set to nil.
// Warning: The underlying redis connection pool will not be closed by Asynq, you are responsible for closing it.
@@ -262,7 +274,9 @@ func (s *Scheduler) Shutdown() { | |||
|
|||
s.clearHistory() | |||
s.client.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of scheduler.Shutdown
, the client is closed by Asynq. If it uses a shared pool, it will return an error (which is unhandled here). Perhaps it should be moved to the following if
clause?
If the scheduler ends up using a shared redis connection pool, perhaps it should only be stopped. Right now there doesn't seem to be an option to simply Stop/Pause the scheduler though
return server | ||
} | ||
|
||
// NewServerFromRedisClient returns a new Server given a redis client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// NewServerFromRedisClient returns a new instance of Server given a redis.UniversalClient
// and server configuration
// Warning: The underlying redis connection pool will not be closed by Asynq, you are responsible for closing it.
@jerbob92 This is ready to be merged. Could you update the inline docs (as per the suggestions) so that we have a uniform outlook. Thanks. |
This PR allows you to re-use an existing Redis client with Asynq, this gives you the benifit of less Redis connections and more control over the Redis configuration options since Asynq doesn't provide all the options.