This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more replicaset and replicaset's methods to replicaset.go
- Loading branch information
1 parent
79c9437
commit 0a01e4e
Showing
2 changed files
with
88 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package vshard_router | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/tarantool/go-tarantool/v2" | ||
"github.com/tarantool/go-tarantool/v2/pool" | ||
) | ||
|
||
type ReplicasetInfo struct { | ||
Name string | ||
UUID uuid.UUID | ||
} | ||
|
||
type Replicaset struct { | ||
conn *pool.ConnectionPool | ||
info ReplicasetInfo | ||
|
||
bucketCount atomic.Int32 | ||
} | ||
|
||
type ReplicasetCallOpts struct { | ||
PoolMode pool.Mode | ||
Timeout time.Duration | ||
} | ||
|
||
// ReplicasetCallImpl perform function on remote storage | ||
func (rs *Replicaset) ReplicasetCallImpl( | ||
ctx context.Context, | ||
opts ReplicasetCallOpts, | ||
fnc string, | ||
args interface{}, | ||
) (interface{}, StorageResultTypedFunc, error) { | ||
if opts.Timeout == 0 { | ||
opts.Timeout = CallTimeoutMin | ||
} | ||
|
||
timeout := opts.Timeout | ||
timeStart := time.Now() | ||
|
||
req := tarantool.NewCallRequest(fnc) | ||
req = req.Context(ctx) | ||
req = req.Args(args) | ||
|
||
var ( | ||
respData []interface{} | ||
err error | ||
) | ||
|
||
for { | ||
if since := time.Since(timeStart); since > timeout { | ||
return nil, nil, err | ||
} | ||
|
||
future := rs.conn.Do(req, opts.PoolMode) | ||
|
||
respData, err = future.Get() | ||
if err != nil { | ||
continue | ||
} | ||
|
||
if len(respData) != 2 { | ||
err = fmt.Errorf("invalid length of response data: must be = 2, current: %d", len(respData)) | ||
continue | ||
} | ||
|
||
if respData[1] != nil { | ||
assertErr := &StorageCallAssertError{} | ||
|
||
err = mapstructure.Decode(respData[1], assertErr) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
err = assertErr | ||
continue | ||
} | ||
|
||
return respData[0], func(result interface{}) error { | ||
return future.GetTyped(&[]interface{}{&result}) | ||
}, 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