/// <summary> /// Sets the key value in etcd /// </summary> /// <param name="key">Key for which value need to be set</param> /// <param name="val">Value corresponding the key</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public PutResponse Put(string key, string val, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { return(Put(new PutRequest { Key = ByteString.CopyFromUtf8(key), Value = ByteString.CopyFromUtf8(val) }, headers, deadline, cancellationToken)); }
public async Task <Photo> UpdateAsync(Photo photo, string tokenValue) { Grpc.Core.Metadata headers = new Grpc.Core.Metadata(); headers.Add("Authorization", $"Bearer {tokenValue}"); UpdateReply p = await photosThingClient.UpdateAsync(photo.ToUpdateRequest(), headers); return(p.ToPhoto()); }
public async Task <Comment> UpdateAsync(Comment comment, string tokenValue) { Grpc.Core.Metadata headers = new Grpc.Core.Metadata(); headers.Add("Authorization", $"Bearer {tokenValue}"); UpdateReply c = await commentsThingClient.UpdateAsync(comment.ToUpdateRequest(), headers); return(c.ToComment()); }
/// <summary> /// Sets the key value in etcd in async /// </summary> /// <param name="key">Key for which value need to be set</param> /// <param name="val">Value corresponding the key</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public async Task <PutResponse> PutAsync(string key, string val, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { return(await PutAsync(new PutRequest { Key = ByteString.CopyFromUtf8(key), Value = ByteString.CopyFromUtf8(val) }, headers, deadline, cancellationToken).ConfigureAwait(false)); }
protected Grpc.Core.Metadata GetRequestMetadata() { var metadata = new Grpc.Core.Metadata(); var routeAs = _httpContextAccessor.HttpContext.Request.Headers["azds-route-as"].ToString(); if (!string.IsNullOrEmpty(routeAs)) { metadata.Add("azds-route-as", routeAs); } return(metadata); }
/// <summary> /// Gets the range of keys with the specified prefix /// </summary> /// <param name="prefixKey">Prefix key</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>RangeResponse containing range of key-values</returns> public RangeResponse GetRange(string prefixKey, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { string rangeEnd = GetRangeEnd(prefixKey); return(Get(new RangeRequest { Key = GetStringByteForRangeRequests(prefixKey), RangeEnd = ByteString.CopyFromUtf8(rangeEnd), }, headers, deadline, cancellationToken)); }
/// <summary> /// Gets the range of keys with the specified prefix in async /// </summary> /// <param name="prefixKey">Prefix key</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>RangeResponse containing range of key-values</returns> public async Task <RangeResponse> GetRangeAsync(string prefixKey, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { string rangeEnd = GetRangeEnd(prefixKey); return(await GetAsync(new RangeRequest { Key = GetStringByteForRangeRequests(prefixKey), RangeEnd = ByteString.CopyFromUtf8(rangeEnd) }, headers, deadline, cancellationToken).ConfigureAwait(false)); }
/// <summary> /// Gets the range of keys with the specified prefix in async /// </summary> /// <param name="prefixKey">Prefix key</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>Dictionary containing range of key-values</returns> public async Task <IDictionary <string, string> > GetRangeValAsync(string prefixKey, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { string rangeEnd = GetRangeEnd(prefixKey); return(RangeRespondToDictionary(await GetAsync(new RangeRequest { Key = GetStringByteForRangeRequests(prefixKey), RangeEnd = ByteString.CopyFromUtf8(rangeEnd) }, headers, deadline, cancellationToken))); }
public async Task <Comment> RemoveAsync(int id, string tokenValue) { Grpc.Core.Metadata headers = new Grpc.Core.Metadata(); headers.Add("Authorization", $"Bearer {tokenValue}"); RemoveReply c = await commentsThingClient.RemoveAsync(new RemoveRequest() { Id = id }, headers); return(c.ToComment()); }
/// <summary> /// Deletes all keys with the specified prefix in async /// </summary> /// <param name="prefixKey">Commin prefix of all keys that need to be deleted</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public async Task <DeleteRangeResponse> DeleteRangeAsync(string prefixKey, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { string rangeEnd = GetRangeEnd(prefixKey); return(await DeleteAsync(new DeleteRangeRequest { Key = ByteString.CopyFromUtf8(prefixKey), RangeEnd = ByteString.CopyFromUtf8(rangeEnd) }, headers, deadline, cancellationToken)); }
public async Task <UserPagedOutput> GetUserPaged(int paged = 1, int rows = 20) { var _header = new Grpc.Core.Metadata() { { "Authorization", $"{HttpContext.Request.Headers["Authorization"]}" } }; var userServiceClient = await grpcServiceHelper.GetUserService(); return(await userServiceClient.GetUserListAsync(new GetUserPagedForm() { Paged = paged, Rows = rows }, _header)); }
/// <summary> /// Snapshot sends a snapshot of the entire backend from a member over a stream to a client. /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="method"></param> /// <param name="cancellationToken"></param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> public async Task Snapshot(SnapshotRequest request, Action <SnapshotResponse> method, CancellationToken cancellationToken, Grpc.Core.Metadata headers = null, DateTime?deadline = null) { await CallEtcdAsync(async (connection) => { using (AsyncServerStreamingCall <SnapshotResponse> snapshotter = connection .maintenanceClient.Snapshot(request, headers, deadline, cancellationToken)) { while (await snapshotter.ResponseStream.MoveNext(cancellationToken)) { SnapshotResponse update = snapshotter.ResponseStream.Current; method(update); } } }); }
// Additional configuration is required to successfully run gRPC on macOS. // For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682 public static IHostBuilder CreateHostBuilder(string[] args, K2Config config) { return(Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder .UseKestrel(options => { options.ListenAnyIP(config.ListeningPort); }) .ConfigureServices(sc => { var backendHeader = new Grpc.Core.Metadata(); backendHeader.Add(nameof(config.BackendGroupId), config.BackendGroupId); // key 는 소문자로 변환되어 들어간다 // K2Config 종속적인 resource 들 ; asp.net core 3.0 이후 직접 StartUp 에 parameter 를 전달할 수 없음 (https://andrewlock.net/avoiding-startup-service-injection-in-asp-net-core-3/) sc.AddSingleton(config); sc.AddSingleton(backendHeader); }) .UseStartup <Startup>() ; })); }
/// <summary> /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client /// to the server and streaming keep alive responses from the server to the client. /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="method"></param> /// <param name="cancellationToken"></param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> public async Task LeaseKeepAlive(LeaseKeepAliveRequest request, Action<LeaseKeepAliveResponse> method, CancellationToken cancellationToken, Grpc.Core.Metadata headers = null) { await CallEtcdAsync(async (connection) => { using (AsyncDuplexStreamingCall<LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser = connection.leaseClient .LeaseKeepAlive(headers, cancellationToken: cancellationToken)) { Task leaserTask = Task.Run(async () => { while (await leaser.ResponseStream.MoveNext(cancellationToken)) { LeaseKeepAliveResponse update = leaser.ResponseStream.Current; method(update); } }, cancellationToken); await leaser.RequestStream.WriteAsync(request); await leaser.RequestStream.CompleteAsync(); await leaserTask; } }); }
/// <summary> /// Compact compacts the event history in the etcd key-value store. The key-value /// store should be periodically compacted or the event history will continue to grow /// indefinitely. /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public CompactionResponse Compact(CompactionRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => CallEtcd((connection) => connection._kvClient .Compact(request, headers, deadline, cancellationToken));
/// <summary> /// UserChangePasswordAsync changes the password of a specified user in async /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public async Task <AuthUserChangePasswordResponse> UserChangePasswordAsync(AuthUserChangePasswordRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => await CallEtcdAsync(async (connection) => await connection._authClient .UserChangePasswordAsync(request, headers, deadline, cancellationToken)).ConfigureAwait(false);
/// <summary> /// Unlock takes a key returned by Lock and releases the hold on lock. The /// next Lock caller waiting for the lock will then be woken up and given /// ownership of the lock. /// </summary> /// <param name="request">The request to send to the server</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public UnlockResponse Unlock(UnlockRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { return(CallEtcd((connection) => connection.lockClient .Unlock(request, headers, deadline, cancellationToken))); }
private async void UploadFile(string fileName) { var md5 = Helper.GetMD5HashFromFile(fileName); var checkFileResult = client.CheckFileExist(new CheckFileExistRequest() { Md5 = md5 }); if (!checkFileResult.Result) { if (checkFileResult.ErrorCode != ErrorCodes.NoError) { Console.WriteLine($"check file:{fileName} error,error code:{checkFileResult.ErrorCode}"); return; } CancellationTokenSource tokenSource = new CancellationTokenSource() { }; var header = new Grpc.Core.Metadata(); header.Add(Const.MD5_KEY, md5); var call = client.UploadWithStream(cancellationToken: tokenSource.Token, headers: header); var result = await call.ResponseStream.MoveNext(tokenSource.Token); if (!call.ResponseStream.Current.Result) { return; } int size = 102400; long totalLenth = new FileInfo(fileName).Length; long hasUploadedLenth = 0; using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { while (hasUploadedLenth < totalLenth) { byte[] bytes = new byte[size]; var count = fs.Read(bytes, 0, size); if (count > 0) { var bytesToSend = new byte[count]; Array.Copy(bytes, 0, bytesToSend, 0, count); await call.RequestStream.WriteAsync(new UploadWithStreamRequest() { Data = Google.Protobuf.ByteString.CopyFrom(bytesToSend) }); if (await call.ResponseStream.MoveNext(tokenSource.Token)) { var response = call.ResponseStream.Current; if (!response.Result) { Console.WriteLine("传输失败:" + response.ErrorCode); break; } } hasUploadedLenth += count; } } } await call.RequestStream.CompleteAsync(); if (hasUploadedLenth == totalLenth) { client.FinishUpload(new FinishUploadRequest() { ClientFileName = fileName, IsFastUpload = false, Md5 = md5, RecognizeId = 123456789, }); } } else { client.FinishUpload(new FinishUploadRequest() { ClientFileName = fileName + "test", IsFastUpload = true, Md5 = md5, RecognizeId = 123456789, }); } }
/// <summary> /// RoleRevokePermission revokes a key or range permission of a specified role /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public AuthRoleRevokePermissionResponse RoleRevokePermission(AuthRoleRevokePermissionRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => CallEtcd((connection) => connection._authClient .RoleRevokePermission(request, headers, deadline, cancellationToken));
/// <summary> /// UserAdd adds a new user /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public AuthUserAddResponse UserAdd(AuthUserAddRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => CallEtcd((connection) => connection._authClient .UserAdd(request, headers, deadline, cancellationToken));
/// <summary> /// RoleRevokePermissionAsync revokes a key or range permission of a specified role in async /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public async Task <AuthRoleRevokePermissionResponse> RoleRevokePermissionAsync( AuthRoleRevokePermissionRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => await CallEtcdAsync(async (connection) => await connection._authClient .RoleRevokePermissionAsync(request, headers, deadline, cancellationToken)).ConfigureAwait(false);
/// <summary> /// Sets the key value in etcd /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public PutResponse Put(PutRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { return(CallEtcd((connection) => connection._kvClient.Put(request, headers, deadline, cancellationToken))); }
/// <summary> /// Alarm activates, deactivates, and queries alarms regarding cluster health /// </summary> /// <param name="request">Alarm request</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>Alarm Response</returns> public AlarmResponse Alarm(AlarmRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { return(CallEtcd((connection) => connection.maintenanceClient .Alarm(request, headers, deadline, cancellationToken))); }
/// <summary> /// Delete the specified key in etcd /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public DeleteRangeResponse Delete(DeleteRangeRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => CallEtcd((connection) => connection._kvClient .DeleteRange(request, headers, deadline, cancellationToken));
/// <summary> /// Get the etcd response for a specified key /// </summary> /// <param name="key">Key for which value need to be fetched</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The etcd response for the specified key</returns> public RangeResponse Get(string key, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => Get(new RangeRequest { Key = ByteString.CopyFromUtf8(key) }, headers, deadline, cancellationToken);
/// <summary> /// Txn processes multiple requests in a single transaction. /// A txn request increments the revision of the key-value store /// and generates events with the same revision for every completed request. /// It is not allowed to modify the same key several times within one txn. /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public TxnResponse Transaction(TxnRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { return(CallEtcd((connection) => connection.kvClient.Txn(request, headers, deadline, cancellationToken))); }
/// <summary> /// Compact compacts the event history in the etcd key-value store in async. The key-value /// store should be periodically compacted or the event history will continue to grow /// indefinitely. /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public async Task <CompactionResponse> CompactAsync(CompactionRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { return(await CallEtcdAsync(async (connection) => await connection.kvClient .CompactAsync(request, headers, deadline, cancellationToken))); }
/// <summary> /// Get the etcd response for a specified key in async /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The etcd response for the specified request</returns> public async Task <RangeResponse> GetAsync(RangeRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => await CallEtcdAsync(async (connection) => await connection._kvClient .RangeAsync(request, headers, deadline, cancellationToken)).ConfigureAwait(false);
/// <summary> /// MemberAddAsync adds a member into the cluster in async /// </summary> /// <param name="request">The request to send to the server.</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The response received from the server.</returns> public async Task <MemberAddResponse> MemberAddAsync(MemberAddRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) { return(await CallEtcdAsync(async (connection) => await connection.clusterClient .MemberAddAsync(request, headers, deadline, cancellationToken))); }
/// <summary> /// Get the etcd response for a specified key in async /// </summary> /// <param name="key">Key for which value need to be fetched</param> /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param> /// <param name="cancellationToken">An optional token for canceling the call.</param> /// <returns>The etcd response for the specified key</returns> public async Task <RangeResponse> GetAsync(string key, Grpc.Core.Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default) => await GetAsync(new RangeRequest { Key = ByteString.CopyFromUtf8(key) }, headers, deadline, cancellationToken).ConfigureAwait(false);