Skip to content

Commit 14873ec

Browse files
committed
nginx fronts varnish-end-user and varnish-admin
`ServiceImpl` uses the new `GraphStoreClient` and `SPARQLClient` subclasses
1 parent be31ee7 commit 14873ec

7 files changed

Lines changed: 93 additions & 39 deletions

File tree

config/system-varnish.trig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
sd:endpoint <http://fuseki-admin:3030/ds/> ;
3030
a:graphStore <http://fuseki-admin:3030/ds/> ;
3131
a:quadStore <http://fuseki-admin:3030/ds/> ;
32-
lapp:backendProxy <http://varnish-admin/> .
32+
lapp:backendProxy <http://nginx-admin:8080/> .
3333

3434
# root end-user
3535

@@ -48,4 +48,4 @@
4848
sd:endpoint <http://fuseki-end-user:3030/ds/> ;
4949
a:graphStore <http://fuseki-end-user:3030/ds/> ;
5050
a:quadStore <http://fuseki-end-user:3030/ds/> ;
51-
lapp:backendProxy <http://varnish-end-user/> .
51+
lapp:backendProxy <http://nginx-end-user:8080/> .

docker-compose.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ services:
3434
- SSL_VERIFY_CLIENT=optional_no_ca
3535
- MAX_BODY_SIZE=2097152
3636
volumes:
37-
- ./platform/nginx.conf.template:/etc/nginx/nginx.conf.template:ro
37+
- ./platform/nginx-frontend.conf.template:/etc/nginx/nginx.conf.template:ro
3838
- ./ssl/server:/etc/nginx/ssl:ro
3939
linkeddatahub:
4040
user: root # otherwise the ldh user does not have permissions to the mounted folder which is owner by root
@@ -128,6 +128,28 @@ services:
128128
command: [ "-t", "86400" ] # time to live
129129
volumes:
130130
- ./platform/varnish-frontend.vcl.template:/etc/varnish/default.vcl.template:ro
131+
nginx-admin:
132+
image: nginx:1.23.3
133+
depends_on:
134+
- varnish-admin
135+
command: /bin/sh -c "cp /etc/nginx/nginx.conf.template /etc/nginx/nginx.conf && sed -i 's|$${UPSTREAM_SERVER}|'"$$UPSTREAM_SERVER"'|g' /etc/nginx/nginx.conf && sed -i 's|$${UPSTREAM_HTTP_PORT}|'"$$UPSTREAM_HTTP_PORT"'|g' /etc/nginx/nginx.conf && sed -i 's|$${SERVER_HTTP_PORT}|'"$$SERVER_HTTP_PORT"'|g' /etc/nginx/nginx.conf && nginx -g 'daemon off;'"
136+
environment:
137+
- UPSTREAM_SERVER=varnish-admin
138+
- UPSTREAM_HTTP_PORT=80
139+
- SERVER_HTTP_PORT=8080 # because of nginx-unprivileged
140+
volumes:
141+
- ./platform/nginx-backend.conf.template:/etc/nginx/nginx.conf.template:ro
142+
nginx-end-user:
143+
image: nginx:1.23.3
144+
depends_on:
145+
- varnish-end-user
146+
command: /bin/sh -c "cp /etc/nginx/nginx.conf.template /etc/nginx/nginx.conf && sed -i 's|$${UPSTREAM_SERVER}|'"$$UPSTREAM_SERVER"'|g' /etc/nginx/nginx.conf && sed -i 's|$${UPSTREAM_HTTP_PORT}|'"$$UPSTREAM_HTTP_PORT"'|g' /etc/nginx/nginx.conf && sed -i 's|$${SERVER_HTTP_PORT}|'"$$SERVER_HTTP_PORT"'|g' /etc/nginx/nginx.conf && nginx -g 'daemon off;'"
147+
environment:
148+
- UPSTREAM_SERVER=varnish-end-user
149+
- UPSTREAM_HTTP_PORT=80
150+
- SERVER_HTTP_PORT=8080 # because of nginx-unprivileged
151+
volumes:
152+
- ./platform/nginx-backend.conf.template:/etc/nginx/nginx.conf.template:ro
131153
varnish-admin:
132154
image: varnish:7.3.0
133155
user: root # otherwise the varnish user does not have permissions to the mounted folder which is owner by root
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
worker_processes 1;
2+
3+
events {
4+
worker_connections 1024;
5+
}
6+
7+
http {
8+
include mime.types;
9+
default_type application/octet-stream;
10+
11+
# Define a shared memory zone for rate limiting (keyed by client IP)
12+
limit_req_zone $binary_remote_addr zone=api_ratelimit:10m rate=5r/s;
13+
limit_req_status 429;
14+
15+
upstream varnish_backend {
16+
server ${UPSTREAM_SERVER}:${UPSTREAM_HTTP_PORT};
17+
}
18+
19+
server {
20+
listen ${SERVER_HTTP_PORT};
21+
22+
# Optional: allow health checks or pre-flight OPTIONS through without limits
23+
location = /healthz {
24+
return 200 'ok';
25+
add_header Content-Type text/plain;
26+
}
27+
28+
location / {
29+
# Apply rate limiting
30+
limit_req zone=api_ratelimit nodelay;
31+
32+
proxy_pass http://varnish_backend;
33+
proxy_set_header Host $host;
34+
proxy_set_header X-Real-IP $remote_addr;
35+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
36+
}
37+
}
38+
}

src/main/java/com/atomgraph/linkeddatahub/client/GraphStoreClient.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,17 @@ public class GraphStoreClient extends com.atomgraph.core.client.GraphStoreClient
3737

3838
private static final Logger log = LoggerFactory.getLogger(GraphStoreClient.class);
3939

40-
private final long defaultDelayMillis = 5000L;
41-
private final int maxRetryCount = 3;
40+
private final long defaultDelayMillis; // = 5000L;
41+
private final int maxRetryCount; // = 3;
4242

43-
protected GraphStoreClient(MediaTypes mediaTypes, WebTarget endpoint) {
43+
protected GraphStoreClient(MediaTypes mediaTypes, WebTarget endpoint, long defaultDelayMillis, int maxRetryCount) {
4444
super(mediaTypes, endpoint);
45+
this.defaultDelayMillis = defaultDelayMillis;
46+
this.maxRetryCount = maxRetryCount;
4547
}
4648

47-
protected GraphStoreClient(WebTarget endpoint) {
48-
this(new MediaTypes(), endpoint);
49-
}
50-
51-
public static GraphStoreClient create(MediaTypes mediaTypes, WebTarget endpoint) {
52-
return new GraphStoreClient(mediaTypes, endpoint);
53-
}
54-
55-
public static GraphStoreClient create(WebTarget endpoint) {
56-
return new GraphStoreClient(endpoint);
49+
public static GraphStoreClient create(MediaTypes mediaTypes, WebTarget endpoint, long defaultDelayMillis, int maxRetryCount) {
50+
return new GraphStoreClient(mediaTypes, endpoint, defaultDelayMillis, maxRetryCount);
5751
}
5852

5953
@Override

src/main/java/com/atomgraph/linkeddatahub/client/SPARQLClient.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,27 @@ public class SPARQLClient extends com.atomgraph.core.client.SPARQLClient {
3434

3535
private static final Logger log = LoggerFactory.getLogger(SPARQLClient.class);
3636

37-
private final long defaultDelayMillis = 1000L;
38-
private final int maxRetryCount = 3;
37+
private final long defaultDelayMillis; // = 1000L;
38+
private final int maxRetryCount; // = 3;
3939

40-
protected SPARQLClient(MediaTypes mediaTypes, WebTarget endpoint, int maxGetRequestSize) {
40+
protected SPARQLClient(MediaTypes mediaTypes, WebTarget endpoint, int maxGetRequestSize, long defaultDelayMillis, int maxRetryCount) {
4141
super(mediaTypes, endpoint, maxGetRequestSize);
42+
this.defaultDelayMillis = defaultDelayMillis;
43+
this.maxRetryCount = maxRetryCount;
4244
}
4345

44-
protected SPARQLClient(MediaTypes mediaTypes, WebTarget endpoint) {
45-
this(mediaTypes, endpoint, 8192);
46+
protected SPARQLClient(MediaTypes mediaTypes, WebTarget endpoint, long defaultDelayMillis, int maxRetryCount) {
47+
super(mediaTypes, endpoint);
48+
this.defaultDelayMillis = defaultDelayMillis;
49+
this.maxRetryCount = maxRetryCount;
4650
}
4751

48-
protected SPARQLClient(WebTarget endpoint) {
49-
this(new MediaTypes(), endpoint);
52+
public static SPARQLClient create(MediaTypes mediaTypes, WebTarget endpoint, int maxGetRequestSize, long defaultDelayMillis, int maxRetryCount) {
53+
return new SPARQLClient(mediaTypes, endpoint, maxGetRequestSize, defaultDelayMillis, maxRetryCount);
5054
}
5155

52-
public static SPARQLClient create(MediaTypes mediaTypes, WebTarget endpoint, int maxGetRequestSize) {
53-
return new SPARQLClient(mediaTypes, endpoint, maxGetRequestSize);
54-
}
55-
56-
public static SPARQLClient create(MediaTypes mediaTypes, WebTarget endpoint) {
57-
return new SPARQLClient(mediaTypes, endpoint);
58-
}
59-
60-
public static SPARQLClient create(WebTarget endpoint) {
61-
return new SPARQLClient(endpoint);
56+
public static SPARQLClient create(MediaTypes mediaTypes, WebTarget endpoint, long defaultDelayMillis, int maxRetryCount) {
57+
return new SPARQLClient(mediaTypes, endpoint, defaultDelayMillis, maxRetryCount);
6258
}
6359

6460
@Override

src/main/java/com/atomgraph/linkeddatahub/model/impl/ServiceImpl.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.atomgraph.core.MediaTypes;
2020
import com.atomgraph.core.client.QuadStoreClient;
21-
import com.atomgraph.core.client.SPARQLClient;
2221
import com.atomgraph.core.model.DatasetAccessor;
2322
import com.atomgraph.core.model.DatasetQuadAccessor;
2423
import com.atomgraph.core.model.EndpointAccessor;
@@ -28,6 +27,7 @@
2827
import com.atomgraph.core.vocabulary.A;
2928
import com.atomgraph.core.vocabulary.SD;
3029
import com.atomgraph.linkeddatahub.client.GraphStoreClient;
30+
import com.atomgraph.linkeddatahub.client.SPARQLClient;
3131
import com.atomgraph.linkeddatahub.model.Service;
3232
import com.atomgraph.linkeddatahub.vocabulary.LAPP;
3333
import java.net.URI;
@@ -131,11 +131,13 @@ public SPARQLClient getSPARQLClient()
131131
public SPARQLClient getSPARQLClient(WebTarget webTarget)
132132
{
133133
SPARQLClient sparqlClient;
134-
134+
final long defaultDelayMillis = 1000L;
135+
final int maxRetryCount = 3;
136+
135137
if (getMaxGetRequestSize() != null)
136-
sparqlClient = SPARQLClient.create(getMediaTypes(), webTarget, getMaxGetRequestSize());
138+
sparqlClient = SPARQLClient.create(getMediaTypes(), webTarget, getMaxGetRequestSize(), defaultDelayMillis, maxRetryCount);
137139
else
138-
sparqlClient = SPARQLClient.create(getMediaTypes(), webTarget);
140+
sparqlClient = SPARQLClient.create(getMediaTypes(), webTarget, defaultDelayMillis, maxRetryCount);
139141

140142
if (getAuthUser() != null && getAuthPwd() != null)
141143
{
@@ -169,8 +171,10 @@ public com.atomgraph.core.client.GraphStoreClient getGraphStoreClient()
169171
*/
170172
public GraphStoreClient getGraphStoreClient(WebTarget webTarget)
171173
{
172-
GraphStoreClient graphStoreClient = GraphStoreClient.create(webTarget);
173-
174+
final long defaultDelayMillis = 1000L;
175+
final int maxRetryCount = 3;
176+
GraphStoreClient graphStoreClient = GraphStoreClient.create(getMediaTypes(), webTarget, defaultDelayMillis, maxRetryCount);
177+
174178
if (getAuthUser() != null && getAuthPwd() != null)
175179
{
176180
HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basicBuilder().

0 commit comments

Comments
 (0)