/
agent_redis_request_reply.go
122 lines (114 loc) · 2.94 KB
/
agent_redis_request_reply.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright © 2023 Clyso GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpc
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"github.com/rs/xid"
"github.com/rs/zerolog"
"strings"
"time"
)
const (
agentPing = "rr:agent:ping"
)
type AgentClient struct {
client *redis.Client
}
func NewAgentClient(client *redis.Client) *AgentClient {
return &AgentClient{client: client}
}
type AgentInfo struct {
URL string
FromStorage string
}
func (a *AgentClient) Ping(ctx context.Context) ([]AgentInfo, error) {
replyChanName := xid.New().String()
ps := a.client.Subscribe(ctx, replyChanName)
defer ps.Close()
defer ps.Unsubscribe(context.Background(), replyChanName)
resCh := make(chan AgentInfo, 1)
reqCtx, cancel := context.WithTimeout(ctx, time.Second*3)
defer cancel()
go func() {
defer close(resCh)
for {
select {
case <-reqCtx.Done():
return
case msg, more := <-ps.Channel():
msgArr := strings.Split(msg.Payload, "|")
if len(msgArr) != 2 {
zerolog.Ctx(reqCtx).Error().Msgf("agent client received invalid payload: %s", msg.Payload)
return
}
resCh <- AgentInfo{
URL: msgArr[0],
FromStorage: msgArr[1],
}
if !more {
return
}
}
}
}()
received, err := a.client.Publish(reqCtx, agentPing, replyChanName).Result()
if err != nil {
return nil, err
}
if received == 0 {
// no agents registered
return nil, nil
}
var agents []AgentInfo
for {
select {
case res, more := <-resCh:
agents = append(agents, res)
if !more || len(agents) == int(received) {
return agents, nil
}
case <-reqCtx.Done():
return agents, nil
}
}
}
func AgentServe(ctx context.Context, client *redis.Client, url, fromStorage string) error {
ps := client.Subscribe(ctx, agentPing)
defer ps.Close()
defer ps.Unsubscribe(context.Background(), agentPing)
messages := ps.Channel()
for {
select {
case <-ctx.Done():
return ctx.Err()
case msg := <-messages:
if msg.Payload == "" {
zerolog.Ctx(ctx).Warn().Msg("request reply agent server: empty payload for proxy creds pubSub received")
continue
}
received, err := client.Publish(ctx, msg.Payload, fmt.Sprintf("%s|%s", url, fromStorage)).Result()
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("request reply agent server: unable to publish reply")
continue
}
if received == 0 {
zerolog.Ctx(ctx).Warn().Msg("request reply agent server: reply not received")
}
}
}
}