Skip to content

Commit 895ee1e

Browse files
authored
Merge pull request #544 from tobychui/v3.1.8
- Exposed timeout value from dpcore to UI - Added active load balancing (if uptime monitor is enabled on that rule) - Refactorized io stats and remove dependencies over wmic - Removed SMTP input validation - Fixed sticky session bug - Fixed passive load balancer bug - Fixed dockerfile bug
2 parents b0dc4d6 + caf4ab3 commit 895ee1e

23 files changed

+14358
-11832
lines changed

src/def.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
const (
4343
/* Build Constants */
4444
SYSTEM_NAME = "Zoraxy"
45-
SYSTEM_VERSION = "3.1.7"
45+
SYSTEM_VERSION = "3.1.8"
4646
DEVELOPMENT_BUILD = false /* Development: Set to false to use embedded web fs */
4747

4848
/* System Constants */

src/go.mod

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ require (
1616
github.com/grandcat/zeroconf v1.0.0
1717
github.com/likexian/whois v1.15.1
1818
github.com/microcosm-cc/bluemonday v1.0.26
19+
github.com/shirou/gopsutil/v4 v4.25.1
20+
github.com/syndtr/goleveldb v1.0.0
1921
golang.org/x/net v0.29.0
20-
golang.org/x/sys v0.25.0
22+
golang.org/x/sys v0.28.0
2123
golang.org/x/text v0.18.0
2224
)
2325

@@ -26,13 +28,15 @@ require (
2628
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
2729
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.9.0 // indirect
2830
github.com/benbjohnson/clock v1.3.0 // indirect
31+
github.com/ebitengine/purego v0.8.2 // indirect
32+
github.com/go-ole/go-ole v1.2.6 // indirect
2933
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
3034
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
3135
github.com/golang/snappy v0.0.1 // indirect
3236
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.114 // indirect
3337
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
38+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
3439
github.com/shopspring/decimal v1.3.1 // indirect
35-
github.com/syndtr/goleveldb v1.0.0 // indirect
3640
github.com/tidwall/btree v0.0.0-20191029221954-400434d76274 // indirect
3741
github.com/tidwall/buntdb v1.1.2 // indirect
3842
github.com/tidwall/gjson v1.12.1 // indirect
@@ -43,6 +47,7 @@ require (
4347
github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 // indirect
4448
github.com/tjfoc/gmsm v1.4.1 // indirect
4549
github.com/vultr/govultr/v3 v3.9.1 // indirect
50+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
4651
go.mongodb.org/mongo-driver v1.12.0 // indirect
4752
)
4853

@@ -175,7 +180,7 @@ require (
175180
github.com/softlayer/softlayer-go v1.1.5 // indirect
176181
github.com/softlayer/xmlrpc v0.0.0-20200409220501-5f089df7cb7e // indirect
177182
github.com/spf13/cast v1.6.0 // indirect
178-
github.com/stretchr/testify v1.9.0 // indirect
183+
github.com/stretchr/testify v1.10.0 // indirect
179184
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1002 // indirect
180185
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1002 // indirect
181186
github.com/transip/gotransip/v6 v6.26.0 // indirect

src/go.sum

+16-4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj
176176
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
177177
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
178178
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
179+
github.com/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I=
180+
github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
179181
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
180182
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
181183
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -221,6 +223,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
221223
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
222224
github.com/go-oauth2/oauth2/v4 v4.5.2 h1:CuZhD3lhGuI6aNLyUbRHXsgG2RwGRBOuCBfd4WQKqBQ=
223225
github.com/go-oauth2/oauth2/v4 v4.5.2/go.mod h1:wk/2uLImWIa9VVQDgxz99H2GDbhmfi/9/Xr+GvkSUSQ=
226+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
227+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
224228
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
225229
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
226230
github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g=
@@ -570,6 +574,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
570574
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
571575
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
572576
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
577+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
578+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
573579
github.com/pquerna/otp v1.4.0 h1:wZvl1TIVxKRThZIBiwOOHOGP/1+nZyWBil9Y2XNEDzg=
574580
github.com/pquerna/otp v1.4.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg=
575581
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
@@ -609,6 +615,8 @@ github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZ
609615
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
610616
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
611617
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
618+
github.com/shirou/gopsutil/v4 v4.25.1 h1:QSWkTc+fu9LTAWfkZwZ6j8MSUk4A2LV7rbH0ZqmLjXs=
619+
github.com/shirou/gopsutil/v4 v4.25.1/go.mod h1:RoUCUpndaJFtT+2zsZzzmhvbfGoDCJ7nFXKJf8GqJbI=
612620
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
613621
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
614622
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
@@ -661,8 +669,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
661669
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
662670
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
663671
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
664-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
665-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
672+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
673+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
666674
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
667675
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
668676
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
@@ -737,6 +745,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
737745
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
738746
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
739747
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
748+
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
749+
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
740750
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
741751
go.mongodb.org/mongo-driver v1.12.0 h1:aPx33jmn/rQuJXPQLZQ8NtfPQG8CaqgLThFtqRb0PiE=
742752
go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0=
@@ -893,6 +903,7 @@ golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7w
893903
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
894904
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
895905
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
906+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
896907
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
897908
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
898909
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -907,6 +918,7 @@ golang.org/x/sys v0.0.0-20200918174421-af09f7315aff/go.mod h1:h1NjWce9XRLGQEsW7w
907918
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
908919
golang.org/x/sys v0.0.0-20201110211018-35f3e6cf4a65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
909920
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
921+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
910922
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
911923
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
912924
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -927,8 +939,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
927939
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
928940
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
929941
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
930-
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
931-
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
942+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
943+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
932944
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
933945
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
934946
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=

src/mod/dynamicproxy/dpcore/dpcore.go

+25-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dpcore
22

33
import (
44
"context"
5-
"crypto/tls"
65
"errors"
76
"io"
87
"log"
@@ -12,8 +11,8 @@ import (
1211
"strings"
1312
"time"
1413

15-
"golang.org/x/net/http2"
1614
"imuslab.com/zoraxy/mod/dynamicproxy/domainsniff"
15+
"imuslab.com/zoraxy/mod/dynamicproxy/modh2c"
1716
"imuslab.com/zoraxy/mod/dynamicproxy/permissionpolicy"
1817
)
1918

@@ -84,9 +83,12 @@ type requestCanceler interface {
8483
}
8584

8685
type DpcoreOptions struct {
87-
IgnoreTLSVerification bool //Disable all TLS verification when request pass through this proxy router
88-
FlushInterval time.Duration //Duration to flush in normal requests. Stream request or keep-alive request will always flush with interval of -1 (immediately)
89-
UseH2CRoundTripper bool //Use H2C RoundTripper for HTTP/2.0 connection
86+
IgnoreTLSVerification bool //Disable all TLS verification when request pass through this proxy router
87+
FlushInterval time.Duration //Duration to flush in normal requests. Stream request or keep-alive request will always flush with interval of -1 (immediately)
88+
MaxConcurrentConnection int //Maxmium concurrent requests to this server
89+
ResponseHeaderTimeout int64 //Timeout for response header, set to 0 for default
90+
IdleConnectionTimeout int64 //Idle connection timeout, set to 0 for default
91+
UseH2CRoundTripper bool //Use H2C RoundTripper for HTTP/2.0 connection
9092
}
9193

9294
func NewDynamicProxyCore(target *url.URL, prepender string, dpcOptions *DpcoreOptions) *ReverseProxy {
@@ -104,30 +106,38 @@ func NewDynamicProxyCore(target *url.URL, prepender string, dpcOptions *DpcoreOp
104106
}
105107

106108
thisTransporter := http.DefaultTransport
107-
if dpcOptions.UseH2CRoundTripper {
108-
thisTransporter = &http2.Transport{
109-
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
110-
return net.Dial(network, addr)
111-
},
112-
AllowHTTP: true,
113-
}
114-
}
115109

116110
//Hack the default transporter to handle more connections
117111
optimalConcurrentConnection := 32
112+
if dpcOptions.MaxConcurrentConnection > 0 {
113+
optimalConcurrentConnection = dpcOptions.MaxConcurrentConnection
114+
}
115+
thisTransporter.(*http.Transport).IdleConnTimeout = 30 * time.Second
118116
thisTransporter.(*http.Transport).MaxIdleConns = optimalConcurrentConnection * 2
119117
thisTransporter.(*http.Transport).MaxIdleConnsPerHost = optimalConcurrentConnection
120-
thisTransporter.(*http.Transport).IdleConnTimeout = 30 * time.Second
121118
thisTransporter.(*http.Transport).MaxConnsPerHost = optimalConcurrentConnection * 2
122119
thisTransporter.(*http.Transport).DisableCompression = true
123120

124-
//TODO: Add user adjustable timeout option here
121+
if dpcOptions.ResponseHeaderTimeout > 0 {
122+
//Set response header timeout
123+
thisTransporter.(*http.Transport).ResponseHeaderTimeout = time.Duration(dpcOptions.ResponseHeaderTimeout) * time.Millisecond
124+
}
125+
126+
if dpcOptions.IdleConnectionTimeout > 0 {
127+
//Set idle connection timeout
128+
thisTransporter.(*http.Transport).IdleConnTimeout = time.Duration(dpcOptions.IdleConnectionTimeout) * time.Millisecond
129+
}
125130

126131
if dpcOptions.IgnoreTLSVerification {
127132
//Ignore TLS certificate validation error
128133
thisTransporter.(*http.Transport).TLSClientConfig.InsecureSkipVerify = true
129134
}
130135

136+
if dpcOptions.UseH2CRoundTripper {
137+
//Use H2C RoundTripper for HTTP/2.0 connection
138+
thisTransporter = modh2c.NewH2CRoundTripper()
139+
}
140+
131141
return &ReverseProxy{
132142
Director: director,
133143
Prepender: prepender,

src/mod/dynamicproxy/loadbalance/loadbalance.go

+47-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package loadbalance
33
import (
44
"strings"
55
"sync"
6+
"time"
67

78
"github.com/google/uuid"
89
"github.com/gorilla/sessions"
@@ -25,11 +26,12 @@ type Options struct {
2526
}
2627

2728
type RouteManager struct {
28-
SessionStore *sessions.CookieStore
29-
LoadBalanceMap sync.Map //Sync map to store the last load balance state of a given node
30-
OnlineStatusMap sync.Map //Sync map to store the online status of a given ip address or domain name
31-
onlineStatusTickerStop chan bool //Stopping channel for the online status pinger
32-
Options Options //Options for the load balancer
29+
SessionStore *sessions.CookieStore
30+
OnlineStatus sync.Map //Store the online status notify by uptime monitor
31+
Options Options //Options for the load balancer
32+
33+
cacheTicker *time.Ticker //Ticker for cache cleanup
34+
cacheTickerStop chan bool //Stop the cache cleanup
3335
}
3436

3537
/* Upstream or Origin Server */
@@ -41,8 +43,12 @@ type Upstream struct {
4143
SkipWebSocketOriginCheck bool //Skip origin check on websocket upgrade connections
4244

4345
//Load balancing configs
44-
Weight int //Random weight for round robin, 0 for fallback only
45-
MaxConn int //TODO: Maxmium connection to this server, 0 for unlimited
46+
Weight int //Random weight for round robin, 0 for fallback only
47+
48+
//HTTP Transport Config
49+
MaxConn int //Maxmium concurrent requests to this upstream dpcore instance
50+
RespTimeout int64 //Response header timeout in milliseconds
51+
IdleTimeout int64 //Idle connection timeout in milliseconds
4652

4753
//currentConnectionCounts atomic.Uint64 //Counter for number of client currently connected
4854
proxy *dpcore.ReverseProxy
@@ -55,14 +61,31 @@ func NewLoadBalancer(options *Options) *RouteManager {
5561
options.SystemUUID = uuid.New().String()
5662
}
5763

64+
//Create a ticker for cache cleanup every 12 hours
65+
cacheTicker := time.NewTicker(12 * time.Hour)
66+
cacheTickerStop := make(chan bool)
67+
go func() {
68+
options.Logger.PrintAndLog("LoadBalancer", "Upstream state cache ticker started", nil)
69+
for {
70+
select {
71+
case <-cacheTickerStop:
72+
return
73+
case <-cacheTicker.C:
74+
//Clean up the cache
75+
options.Logger.PrintAndLog("LoadBalancer", "Cleaning up upstream state cache", nil)
76+
}
77+
}
78+
}()
79+
5880
//Generate a session store for stickySession
5981
store := sessions.NewCookieStore([]byte(options.SystemUUID))
6082
return &RouteManager{
61-
SessionStore: store,
62-
LoadBalanceMap: sync.Map{},
63-
OnlineStatusMap: sync.Map{},
64-
onlineStatusTickerStop: nil,
65-
Options: *options,
83+
SessionStore: store,
84+
OnlineStatus: sync.Map{},
85+
Options: *options,
86+
87+
cacheTicker: cacheTicker,
88+
cacheTickerStop: cacheTickerStop,
6689
}
6790
}
6891

@@ -90,11 +113,20 @@ func GetUpstreamsAsString(upstreams []*Upstream) string {
90113
return strings.Join(targets, ", ")
91114
}
92115

116+
// Reset the current session store and clear all previous sessions
117+
func (m *RouteManager) ResetSessions() {
118+
m.SessionStore = sessions.NewCookieStore([]byte(m.Options.SystemUUID))
119+
}
120+
93121
func (m *RouteManager) Close() {
94-
if m.onlineStatusTickerStop != nil {
95-
m.onlineStatusTickerStop <- true
96-
}
122+
//Close the session store
123+
m.SessionStore.MaxAge(0)
97124

125+
//Stop the cache cleanup
126+
if m.cacheTicker != nil {
127+
m.cacheTicker.Stop()
128+
}
129+
close(m.cacheTickerStop)
98130
}
99131

100132
// Log Println, replace all log.Println or fmt.Println with this

0 commit comments

Comments
 (0)