github.com/polarismesh/polaris@v1.17.8/service/batch/client_future.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package batch 19 20 import ( 21 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 22 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 23 24 "github.com/polarismesh/polaris/common/model" 25 ) 26 27 // InstanceFuture 创建实例的异步结构体 28 type ClientFuture struct { 29 request *apiservice.Client // api请求对象 30 client *model.Client // 从数据库中读取到的model信息 31 code apimodel.Code // 记录对外API的错误码 32 result chan error // 执行成功/失败的应答chan 33 } 34 35 // Reply future的应答 36 func (future *ClientFuture) Reply(code apimodel.Code, result error) { 37 future.code = code 38 39 select { 40 case future.result <- result: 41 default: 42 log.Warnf("[Batch] client(%s) future is not captured", future.request.GetId().GetValue()) 43 } 44 } 45 46 // Wait 外部调用者,需要调用Wait等待执行结果 47 func (future *ClientFuture) Wait() error { 48 return <-future.result 49 } 50 51 // SetClient 设置 client 信息 52 func (future *ClientFuture) SetClient(client *model.Client) { 53 future.client = client 54 } 55 56 // Client 获取 client 信息 57 func (future *ClientFuture) Client() *model.Client { 58 return future.client 59 } 60 61 // Code 获取code 62 func (future *ClientFuture) Code() apimodel.Code { 63 return future.code 64 } 65 66 // SendReply 批量答复futures 67 func SendClientReply(futures interface{}, code apimodel.Code, result error) { 68 switch futureType := futures.(type) { 69 case []*ClientFuture: 70 for _, entry := range futureType { 71 entry.Reply(code, result) 72 } 73 case map[string]*ClientFuture: 74 for _, entry := range futureType { 75 entry.Reply(code, result) 76 } 77 default: 78 log.Errorf("[Controller] not found reply client futures type: %T", futures) 79 } 80 }