Skip to content

Commit e0189f3

Browse files
committed
proxy: warmup fixes
1 parent 52c11fe commit e0189f3

3 files changed

Lines changed: 60 additions & 11 deletions

File tree

proxy/main.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,34 +102,40 @@ func main() {
102102

103103
glog.Infof("Listen api (:%d), socks5 (:%d), http (:%d), https (:%d)", ListenApiPort, ListenSocksPort, ListenHttpPort, ListenHttpsPort)
104104

105-
newApiServer(
105+
newSocks5Server(
106106
ctx,
107107
cancel,
108108
proxyDeviceManager,
109109
transportTls,
110110
settings,
111111
)
112112

113-
newSocks5Server(
113+
newHttpServer(
114114
ctx,
115115
cancel,
116116
proxyDeviceManager,
117117
transportTls,
118118
settings,
119119
)
120120

121-
newHttpServer(
121+
wg := newWgServer(
122122
ctx,
123123
cancel,
124124
proxyDeviceManager,
125-
transportTls,
126125
settings,
127126
)
128127

129-
wg := newWgServer(
128+
warmup := func(proxyClient *model.ProxyClient) error {
129+
wg.AddProxyClients(proxyClient)
130+
return nil
131+
}
132+
133+
newApiServer(
130134
ctx,
131135
cancel,
132136
proxyDeviceManager,
137+
transportTls,
138+
warmup,
133139
settings,
134140
)
135141

@@ -150,7 +156,7 @@ func main() {
150156
}
151157
}
152158

153-
wg.AddProxyClients(proxyClients)
159+
wg.AddProxyClients(proxyClients...)
154160
})
155161
defer sub()
156162

@@ -398,7 +404,7 @@ func (self *wgServer) run() {
398404
}
399405
}
400406

401-
func (self *wgServer) AddProxyClients(proxyClients []*model.ProxyClient) {
407+
func (self *wgServer) AddProxyClients(proxyClients ...*model.ProxyClient) {
402408
serverConfig := model.LoadServerProxyConfig()
403409

404410
clients := map[netip.Addr]*proxy.WgClient{}

proxy/proxy_api.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/urnetwork/server"
15-
// "github.com/urnetwork/server/model"
15+
"github.com/urnetwork/server/model"
1616
"github.com/urnetwork/server/router"
1717
)
1818

@@ -21,6 +21,7 @@ type apiServer struct {
2121
cancel context.CancelFunc
2222
proxyDeviceManager *ProxyDeviceManager
2323
transportTls *server.TransportTls
24+
warmupCallback func(*model.ProxyClient) error
2425
settings *ProxySettings
2526
}
2627

@@ -29,13 +30,15 @@ func newApiServer(
2930
cancel context.CancelFunc,
3031
proxyDeviceManager *ProxyDeviceManager,
3132
transportTls *server.TransportTls,
33+
warmupCallback func(*model.ProxyClient) error,
3234
settings *ProxySettings,
3335
) *apiServer {
3436
s := &apiServer{
3537
ctx: ctx,
3638
cancel: cancel,
3739
proxyDeviceManager: proxyDeviceManager,
3840
transportTls: transportTls,
41+
warmupCallback: warmupCallback,
3942
settings: settings,
4043
}
4144

@@ -112,6 +115,21 @@ func (self *apiServer) HandleWarmup(w http.ResponseWriter, r *http.Request) {
112115
return
113116
}
114117

118+
if self.warmupCallback != nil {
119+
proxyClient, err := model.GetProxyClient(self.ctx, proxyId)
120+
if err != nil {
121+
http.Error(w, err.Error(), http.StatusInternalServerError)
122+
return
123+
}
124+
if proxyClient != nil {
125+
err = self.warmupCallback(proxyClient)
126+
if err != nil {
127+
http.Error(w, err.Error(), http.StatusInternalServerError)
128+
return
129+
}
130+
}
131+
}
132+
115133
timeout := time.Duration(warmupRequest.TimeoutSeconds) * time.Second
116134
ready := proxyDevice.WaitForReady(r.Context(), timeout)
117135

proxy/proxy_client_notification.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,53 @@ import (
1717
type ProxyClientsFunction = func(proxyClients []*model.ProxyClient)
1818

1919
type proxyClientNotification struct {
20-
ctx context.Context
20+
ctx context.Context
21+
cancel context.CancelFunc
2122

2223
settings *ProxySettings
2324

2425
proxyClientsCallbacks *connect.CallbackList[ProxyClientsFunction]
2526
}
2627

2728
func newProxyClientNotification(ctx context.Context, settings *ProxySettings) *proxyClientNotification {
29+
cancelCtx, cancel := context.WithCancel(ctx)
2830
p := &proxyClientNotification{
29-
ctx: ctx,
31+
ctx: cancelCtx,
32+
cancel: cancel,
3033
settings: settings,
3134
proxyClientsCallbacks: connect.NewCallbackList[ProxyClientsFunction](),
3235
}
33-
go server.HandleError(p.run)
36+
go server.HandleError(p.run, cancel)
3437
return p
3538
}
3639

3740
func (self *proxyClientNotification) run() {
41+
defer self.cancel()
42+
3843
proxyHost := server.RequireHost()
3944
block := server.RequireBlock()
4045

46+
monitor := connect.NewMonitor()
47+
48+
go server.HandleError(func() {
49+
defer self.cancel()
50+
51+
event, sub := server.Subscribe(self.ctx, model.ProxyClientChannel(proxyHost, block))
52+
defer sub()
53+
54+
for {
55+
select {
56+
case <-self.ctx.Done():
57+
return
58+
case <-event:
59+
monitor.NotifyAll()
60+
}
61+
}
62+
})
63+
4164
nextChangeId := int64(0)
4265
for {
66+
notify := monitor.NotifyChannel()
4367
proxyClients, maxChangeId, err := model.GetProxyClientsSince(
4468
self.ctx,
4569
proxyHost,
@@ -57,6 +81,7 @@ func (self *proxyClientNotification) run() {
5781
select {
5882
case <-self.ctx.Done():
5983
return
84+
case <-notify:
6085
case <-time.After(self.settings.NotificationTimeout):
6186
}
6287
}

0 commit comments

Comments
 (0)