-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup_service.go
96 lines (78 loc) · 2.35 KB
/
lookup_service.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
package danube
import (
"context"
"errors"
"sync/atomic"
"github.com/danrusei/danube-go/proto" // Path to your generated proto package
)
// LookupResult holds the result of a topic lookup
type LookupResult struct {
ResponseType proto.TopicLookupResponse_LookupType
Addr string
}
// LookupService handles lookup operations
type lookupService struct {
cnxManager *connectionManager
requestID atomic.Uint64
}
// NewLookupService creates a new instance of LookupService
func NewLookupService(cnxManager *connectionManager) *lookupService {
return &lookupService{
cnxManager: cnxManager,
requestID: atomic.Uint64{},
}
}
// LookupTopic performs the topic lookup request
func (ls *lookupService) lookupTopic(ctx context.Context, addr string, topic string) (*LookupResult, error) {
conn, err := ls.cnxManager.getConnection(addr, addr)
if err != nil {
return nil, err
}
client := proto.NewDiscoveryClient(conn.grpcConn)
lookupRequest := &proto.TopicLookupRequest{
RequestId: ls.requestID.Add(1),
Topic: topic,
}
response, err := client.TopicLookup(ctx, lookupRequest)
if err != nil {
return nil, err
}
return &LookupResult{
ResponseType: response.GetResponseType(),
Addr: response.GetBrokerServiceUrl(),
}, nil
}
// LookupTopic performs the topic lookup request
func (ls *lookupService) topicPartitions(ctx context.Context, addr string, topic string) ([]string, error) {
conn, err := ls.cnxManager.getConnection(addr, addr)
if err != nil {
return nil, err
}
client := proto.NewDiscoveryClient(conn.grpcConn)
lookupRequest := &proto.TopicLookupRequest{
RequestId: ls.requestID.Add(1),
Topic: topic,
}
response, err := client.TopicPartitions(ctx, lookupRequest)
if err != nil {
return nil, err
}
return response.GetPartitions(), nil
}
// HandleLookup processes the lookup request and returns the appropriate URI
func (ls *lookupService) handleLookup(ctx context.Context, addr string, topic string) (string, error) {
lookupResult, err := ls.lookupTopic(ctx, addr, topic)
if err != nil {
return "", err
}
switch lookupResult.ResponseType {
case proto.TopicLookupResponse_Redirect:
return lookupResult.Addr, nil
case proto.TopicLookupResponse_Connect:
return addr, nil
case proto.TopicLookupResponse_Failed:
return "", errors.New("lookup failed")
default:
return "", errors.New("unknown lookup type")
}
}