Repository navigation
Expand file tree
/
Copy pathdiscovery.go
More file actions
62 lines (52 loc) · 2.26 KB
/
Copy pathdiscovery.go
File metadata and controls
62 lines (52 loc) · 2.26 KB
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
package sdk
import (
"context"
"encoding/json"
"github.com/nginxui/plugin-sdk-go/protocol"
)
// DiscoveryRequest is the payload of discovery.resolve.
type DiscoveryRequest = protocol.DiscoveryResolveParams
// DiscoveryResult is the reply to discovery.resolve.
type DiscoveryResult = protocol.DiscoveryResolveResult
// DiscoveryTarget is one server a DiscoveryResult lists.
type DiscoveryTarget = protocol.DiscoveryTarget
// DiscoveryHandler resolves services into their servers through the
// providers the manifest declares in its discovery block. req.Provider is the
// provider code, req.Config holds the values of its form and req.Service
// names the service. The manifest must request the network permission.
type DiscoveryHandler interface {
// Resolve returns every server that backs the service right now: an IP
// address or a host name without a port, a port from 1 to 65535 and a
// weight from 0 to 1000, where 0 means 1. When the service cannot be
// resolved return an error, so the host keeps the servers it has:
// InvalidConfig for a bad field (UnknownService for a service the
// provider does not know), any other error for a provider failure.
Resolve(ctx context.Context, req DiscoveryRequest) (DiscoveryResult, error)
}
// Target is a DiscoveryTarget with weight 1.
func Target(address string, port int, tags ...string) DiscoveryTarget {
return DiscoveryTarget{Address: address, Port: port, Weight: 1, Tags: tags}
}
// UnknownService reports a service the provider does not know. It is an
// invalid configuration naming the service field, since the person typed it.
func UnknownService(service string) *protocol.Error {
return InvalidConfig("service", "unknown service: "+service)
}
// registerDiscovery wires the upstream.discovery method onto the connection.
func (rt *runtime) registerDiscovery() {
rt.conn.Handle(protocol.MethodDiscoveryResolve, rt.track(rt.onDiscoveryResolve))
}
func (rt *runtime) onDiscoveryResolve(ctx context.Context, raw json.RawMessage) (any, error) {
req, err := decode[DiscoveryRequest](raw)
if err != nil {
return nil, err
}
result, err := rt.plugin.Discovery.Resolve(WithHost(ctx, rt.host), req)
if err != nil {
return nil, err
}
if result.Targets == nil {
result.Targets = []DiscoveryTarget{}
}
return result, nil
}