-
Notifications
You must be signed in to change notification settings - Fork 18
/
broker_client.go
137 lines (126 loc) · 4.31 KB
/
broker_client.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***************************************************************
*
* Copyright (C) 2024, Pelican Project, Morgridge Institute for Research
*
* 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 origin
import (
"context"
"io"
"net"
"net/http"
"strconv"
"sync"
"github.com/pelicanplatform/pelican/broker"
"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/param"
"github.com/pelicanplatform/pelican/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"golang.org/x/time/rate"
)
var (
// We have a custom transport object to force all our connections to the
// localhost to avoid potentially going over the external network to talk
// to our xrootd child process.
proxyTransport *http.Transport
onceTransport sync.Once
// It's possible to overwhelm the XRootD listen socket with requests. This rate
// limiter will allow no more than 32 requests / second and 8 new ones in a burst
xrdConnLimit *rate.Limiter = rate.NewLimiter(32, 8)
)
// Return a custom HTTP transport object; starts with the default transport for
// Pelican but forces all connections to go to the local xrootd port.
func getTransport() *http.Transport {
onceTransport.Do(func() {
proxyTransport = config.GetTransport().Clone()
// When creating a new socket out to the remote server, ignore the actual
// requested address and return a localhost socket.
proxyTransport.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
dialer := net.Dialer{}
if err := xrdConnLimit.Wait(ctx); err != nil {
err = errors.Wrap(err, "Failed to rate-limit local connection")
return nil, err
}
return dialer.DialContext(ctx, "tcp", "localhost:"+strconv.Itoa(param.Origin_Port.GetInt()))
}
})
return proxyTransport
}
func proxyOrigin(resp http.ResponseWriter, req *http.Request) {
url := req.URL
url.Scheme = "https"
url.Host = param.Server_Hostname.GetString() + ":" + strconv.Itoa(param.Origin_Port.GetInt())
log.Debugln("Will proxy request to URL", url.String())
transport := getTransport()
xrdResp, err := transport.RoundTrip(req)
if err != nil {
log.Infoln("Failed to talk to xrootd service:", err)
resp.WriteHeader(http.StatusServiceUnavailable)
if _, err := resp.Write([]byte(`Failed to connect to local xrootd instance`)); err != nil {
log.Infoln("Failed to write response to client:", err)
}
return
}
defer xrdResp.Body.Close()
utils.CopyHeader(resp.Header(), xrdResp.Header)
resp.WriteHeader(xrdResp.StatusCode)
if _, err = io.Copy(resp, xrdResp.Body); err != nil {
log.Warningln("Failed to copy response body from Xrootd to remote cache:", err)
}
}
// Launch goroutines that continuously poll the broker
func LaunchBrokerListener(ctx context.Context, egrp *errgroup.Group) (err error) {
listenerChan := make(chan any)
// Startup 5 continuous polling routines
for cnt := 0; cnt < 5; cnt += 1 {
err = broker.LaunchRequestMonitor(ctx, egrp, listenerChan)
if err != nil {
return
}
}
// Start routine which receives the reverse listener and then launches
// a simple proxying HTTPS server for that connection
egrp.Go(func() (err error) {
for {
select {
case <-ctx.Done():
return
case res := <-listenerChan:
if err, ok := res.(error); ok {
log.Errorln("Callback failed:", err)
break
}
listener, ok := res.(net.Listener)
if !ok {
log.Errorln("Failed to determine callback result:", res)
break
}
srv := http.Server{
Handler: http.HandlerFunc(proxyOrigin),
}
go func() {
// A one-shot listener should do a single "accept" then shutdown.
err = srv.Serve(listener)
if !errors.Is(err, net.ErrClosed) {
log.Errorln("Failed to serve reverse connection:", err)
}
}()
}
}
})
return
}