-
Notifications
You must be signed in to change notification settings - Fork 86
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 #830 from arkhipov/master
feat: coordination session client
- Loading branch information
Showing
14 changed files
with
3,326 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package options | ||
|
||
import ( | ||
"math" | ||
"time" | ||
|
||
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Coordination" | ||
) | ||
|
||
// WithDescription returns an OpenSessionOption that specifies a user-defined description that may be used to describe | ||
// the client. | ||
func WithDescription(description string) OpenSessionOption { | ||
return func(c *OpenSessionOptions) { | ||
c.Description = description | ||
} | ||
} | ||
|
||
// WithSessionTimeout returns an OpenSessionOption that specifies the timeout during which client may restore a | ||
// detached session. The client is forced to terminate the session if the last successful session request occurred | ||
// earlier than this time. | ||
// | ||
// If this is not set, the client uses the default 5 seconds. | ||
func WithSessionTimeout(timeout time.Duration) OpenSessionOption { | ||
return func(c *OpenSessionOptions) { | ||
c.SessionTimeout = timeout | ||
} | ||
} | ||
|
||
// WithSessionStartTimeout returns an OpenSessionOption that specifies the time that the client should wait for a | ||
// response to the StartSession request from the server before it terminates the gRPC stream and tries to reconnect. | ||
// | ||
// If this is not set, the client uses the default time 1 second. | ||
func WithSessionStartTimeout(timeout time.Duration) OpenSessionOption { | ||
return func(c *OpenSessionOptions) { | ||
c.SessionStartTimeout = timeout | ||
} | ||
} | ||
|
||
// WithSessionStopTimeout returns an OpenSessionOption that specifies the time that the client should wait for a | ||
// response to the StopSession request from the server before it terminates the gRPC stream and tries to reconnect. | ||
// | ||
// If this is not set, the client uses the default time 1 second. | ||
func WithSessionStopTimeout(timeout time.Duration) OpenSessionOption { | ||
return func(c *OpenSessionOptions) { | ||
c.SessionStartTimeout = timeout | ||
} | ||
} | ||
|
||
// WithSessionKeepAliveTimeout returns an OpenSessionOption that specifies the time that the client will wait before it | ||
// terminates the gRPC stream and tries to reconnect if no successful responses have been received from the server. | ||
// | ||
// If this is not set, the client uses the default time 10 seconds. | ||
func WithSessionKeepAliveTimeout(timeout time.Duration) OpenSessionOption { | ||
return func(c *OpenSessionOptions) { | ||
c.SessionKeepAliveTimeout = timeout | ||
} | ||
} | ||
|
||
// WithSessionReconnectDelay returns an OpenSessionOption that specifies the time that the client will wait before it | ||
// tries to reconnect the underlying gRPC stream in case of error. | ||
// | ||
// If this is not set, the client uses the default time 500 milliseconds. | ||
func WithSessionReconnectDelay(delay time.Duration) OpenSessionOption { | ||
return func(c *OpenSessionOptions) { | ||
c.SessionReconnectDelay = delay | ||
} | ||
} | ||
|
||
// OpenSessionOption configures how we open a new session. | ||
type OpenSessionOption func(c *OpenSessionOptions) | ||
|
||
// OpenSessionOptions configure an OpenSession call. OpenSessionOptions are set by the OpenSessionOption values passed | ||
// to the OpenSession function. | ||
type OpenSessionOptions struct { | ||
Description string | ||
SessionTimeout time.Duration | ||
SessionStartTimeout time.Duration | ||
SessionStopTimeout time.Duration | ||
SessionKeepAliveTimeout time.Duration | ||
SessionReconnectDelay time.Duration | ||
} | ||
|
||
// WithEphemeral returns an AcquireSemaphoreOption that causes to create an ephemeral semaphore. | ||
// | ||
// Ephemeral semaphores are created with the first acquire operation and automatically deleted with the last release | ||
// operation. Ephemeral semaphore are always created with the limit of coordination.MaxSemaphoreLimit. | ||
func WithEphemeral(ephemeral bool) AcquireSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_AcquireSemaphore) { | ||
c.Ephemeral = ephemeral | ||
} | ||
} | ||
|
||
// WithAcquireTimeout returns an AcquireSemaphoreOption which sets the timeout after which the operation fails if it | ||
// is still waiting in the queue. Use 0 to make the AcquireSemaphore method fail immediately if the semaphore is already | ||
// acquired by another session. | ||
// | ||
// If this is not set, the client waits for the acquire operation result until the operation or session context is done. | ||
// You can reset the default value of this timeout by calling the WithNoAcquireTimeout method. | ||
func WithAcquireTimeout(timeout time.Duration) AcquireSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_AcquireSemaphore) { | ||
c.TimeoutMillis = uint64(timeout.Milliseconds()) | ||
} | ||
} | ||
|
||
// WithNoAcquireTimeout returns an AcquireSemaphoreOption which disables the timeout after which the operation fails if | ||
// it is still waiting in the queue. | ||
// | ||
// This is the default behavior. You can set the specific timeout by calling the WithAcquireTimeout method. | ||
func WithNoAcquireTimeout() AcquireSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_AcquireSemaphore) { | ||
c.TimeoutMillis = math.MaxUint64 | ||
} | ||
} | ||
|
||
// WithAcquireData returns an AcquireSemaphoreOption which attaches user-defined data to the operation. | ||
func WithAcquireData(data []byte) AcquireSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_AcquireSemaphore) { | ||
c.Data = data | ||
} | ||
} | ||
|
||
// AcquireSemaphoreOption configures how we acquire a semaphore. | ||
type AcquireSemaphoreOption func(c *Ydb_Coordination.SessionRequest_AcquireSemaphore) | ||
|
||
// WithForceDelete return a DeleteSemaphoreOption which allows to delete a semaphore even if it is currently acquired | ||
// by other sessions. | ||
func WithForceDelete(force bool) DeleteSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_DeleteSemaphore) { | ||
c.Force = force | ||
} | ||
} | ||
|
||
// DeleteSemaphoreOption configures how we delete a semaphore. | ||
type DeleteSemaphoreOption func(c *Ydb_Coordination.SessionRequest_DeleteSemaphore) | ||
|
||
// WithCreateData return a CreateSemaphoreOption which attaches user-defined data to the semaphore. | ||
func WithCreateData(data []byte) CreateSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_CreateSemaphore) { | ||
c.Data = data | ||
} | ||
} | ||
|
||
// CreateSemaphoreOption configures how we create a semaphore. | ||
type CreateSemaphoreOption func(c *Ydb_Coordination.SessionRequest_CreateSemaphore) | ||
|
||
// WithUpdateData return a UpdateSemaphoreOption which changes user-defined data in the semaphore. | ||
func WithUpdateData(data []byte) UpdateSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_UpdateSemaphore) { | ||
c.Data = data | ||
} | ||
} | ||
|
||
// UpdateSemaphoreOption configures how we update a semaphore. | ||
type UpdateSemaphoreOption func(c *Ydb_Coordination.SessionRequest_UpdateSemaphore) | ||
|
||
// WithDescribeOwners return a DescribeSemaphoreOption which causes server send the list of owners in the response | ||
// to the DescribeSemaphore request. | ||
func WithDescribeOwners(describeOwners bool) DescribeSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_DescribeSemaphore) { | ||
c.IncludeOwners = describeOwners | ||
} | ||
} | ||
|
||
// WithDescribeWaiters return a DescribeSemaphoreOption which causes server send the list of waiters in the response | ||
// to the DescribeSemaphore request. | ||
func WithDescribeWaiters(describeWaiters bool) DescribeSemaphoreOption { | ||
return func(c *Ydb_Coordination.SessionRequest_DescribeSemaphore) { | ||
c.IncludeWaiters = describeWaiters | ||
} | ||
} | ||
|
||
// DescribeSemaphoreOption configures how we update a semaphore. | ||
type DescribeSemaphoreOption func(c *Ydb_Coordination.SessionRequest_DescribeSemaphore) |
Oops, something went wrong.