|
| 1 | +/* |
| 2 | + * Copyright 2025 RPCPlatform Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package rpcplatform |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "strings" |
| 22 | + "time" |
| 23 | + |
| 24 | + etcd "go.etcd.io/etcd/client/v3" |
| 25 | +) |
| 26 | + |
| 27 | +func (c *Client) stateInit() (map[string]string, int64, error) { |
| 28 | + ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) |
| 29 | + resp, err := c.etcd.Get(ctx, c.target, etcd.WithPrefix()) |
| 30 | + cancel() |
| 31 | + |
| 32 | + if err != nil { |
| 33 | + return nil, 0, err |
| 34 | + } |
| 35 | + |
| 36 | + serverInfo := make(map[string]string, len(resp.Kvs)) |
| 37 | + for _, kv := range resp.Kvs { |
| 38 | + trimKey := strings.TrimPrefix(string(kv.Key), c.target) |
| 39 | + serverInfo[trimKey] = string(kv.Value) |
| 40 | + } |
| 41 | + |
| 42 | + c.updateState(true, serverInfo) |
| 43 | + return serverInfo, resp.Header.Revision, nil |
| 44 | +} |
0 commit comments