Skip to content

Commit 6e023c8

Browse files
committed
slight refactor of api to get name from strategy pointer
1 parent 567e54a commit 6e023c8

File tree

7 files changed

+55
-103
lines changed

7 files changed

+55
-103
lines changed

include/ts/ts.h

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,20 +1611,7 @@ void *TSHttpTxnNextHopStrategyGet(TSHttpTxn txnp);
16111611
@param txnp HTTP transaction whose next hop strategy to get.
16121612
16131613
*/
1614-
char const *TSHttpTxnNextHopStrategyNameGet(TSHttpTxn txnp);
1615-
1616-
/**
1617-
Retrieves a pointer to the named strategy in the strategy table.
1618-
This will return nullptr if no strategy is set.
1619-
1620-
This uses the current transaction's state machine to get
1621-
access to UrlRewrite's NextHopStrategyFactory.
1622-
1623-
@param txnp HTTP transaction which holds the strategy table.
1624-
@param name of the strategy to look up.
1625-
1626-
*/
1627-
TSReturnCode TSHttpTxnNextHopNamedStrategySet(TSHttpTxn txnp, const char *name);
1614+
char const *TSHttpNextHopStrategyNameGet(void *strategy);
16281615

16291616
/**
16301617
Retrieves a pointer to the named strategy in the strategy table.
@@ -1639,16 +1626,6 @@ TSReturnCode TSHttpTxnNextHopNamedStrategySet(TSHttpTxn txnp, const char *name);
16391626
*/
16401627
void *TSHttpTxnNextHopNamedStrategyGet(TSHttpTxn txnp, const char *name);
16411628

1642-
/**
1643-
Retrieves a pointer to the named strategy in the loaded strategy table.
1644-
This will return nullptr if the named strategy is not found.
1645-
1646-
@param txnp HTTP transaction whose parent proxy to get.
1647-
@param pointer to the current selection strategy.
1648-
1649-
*/
1650-
// void* TSRemapNamedNextHopStrategyGet(char const *name);
1651-
16521629
/**
16531630
Sets the parent proxy name and port. The string hostname is copied
16541631
into the TSHttpTxn; you can modify or delete the string after

plugins/header_rewrite/conditions.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ ConditionNextHop::append_value(std::string &s, const Resources &res)
15391539
s.append(std::to_string(port));
15401540
} break;
15411541
case NEXT_HOP_STRATEGY: {
1542-
char const *const name = TSHttpTxnNextHopStrategyNameGet(res.state.txnp);
1542+
char const *const name = TSHttpNextHopStrategyNameGet(res.state.txnp);
15431543
if (nullptr != name) {
15441544
Dbg(pi_dbg_ctl, "Appending '%s' to evaluation value", name);
15451545
s.append(name);

plugins/header_rewrite/operators.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,11 +1621,13 @@ OperatorSetNextHopStrategy::exec(const Resources &res) const
16211621
return true;
16221622
}
16231623

1624-
if (TS_SUCCESS == TSHttpTxnNextHopNamedStrategySet(txnp, value.c_str())) {
1625-
Dbg(pi_dbg_ctl, " Setting strategy '%s'", value.c_str());
1626-
} else {
1627-
TSWarning("[%s] Failed to set strategy '%s'", PLUGIN_NAME, value.c_str());
1624+
void *const stratptr = TSHttpTxnNextHopNamedStrategyGet(txnp, value.c_str());
1625+
if (nullptr == stratptr) {
1626+
TSWarning("[%s] Failed to get strategy '%s'", PLUGIN_NAME, value.c_str());
16281627
return false;
1628+
} else {
1629+
Dbg(pi_dbg_ctl, " Setting strategy '%s'", value.c_str());
1630+
TSHttpTxnNextHopStrategySet(txnp, stratptr);
16291631
}
16301632

16311633
return true;

plugins/lua/ts_lua_http.cc

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ static int ts_lua_http_redo_cache_lookup(lua_State *L);
7878

7979
static int ts_lua_http_get_next_hop_strategy(lua_State *L);
8080
static int ts_lua_http_set_next_hop_strategy(lua_State *L);
81-
static int ts_lua_http_clear_next_hop_strategy(lua_State *L);
8281

8382
static int ts_lua_http_get_parent_proxy(lua_State *L);
8483
static int ts_lua_http_set_parent_proxy(lua_State *L);
@@ -191,9 +190,6 @@ ts_lua_inject_http_cache_api(lua_State *L)
191190
lua_pushcfunction(L, ts_lua_http_set_next_hop_strategy);
192191
lua_setfield(L, -2, "set_next_hop_strategy");
193192

194-
lua_pushcfunction(L, ts_lua_http_clear_next_hop_strategy);
195-
lua_setfield(L, -2, "clear_next_hop_strategy");
196-
197193
lua_pushcfunction(L, ts_lua_http_get_parent_proxy);
198194
lua_setfield(L, -2, "get_parent_proxy");
199195

@@ -539,7 +535,10 @@ ts_lua_http_get_next_hop_strategy(lua_State *L)
539535

540536
GET_HTTP_CONTEXT(http_ctx, L);
541537

542-
name = TSHttpTxnNextHopStrategyNameGet(http_ctx->txnp);
538+
void *const stratptr = TSHttpTxnNextHopStrategyGet(http_ctx->txnp);
539+
if (nullptr != stratptr) {
540+
name = TSHttpNextHopStrategyNameGet(stratptr);
541+
}
543542

544543
if (name == nullptr) {
545544
lua_pushnil(L);
@@ -566,8 +565,16 @@ ts_lua_http_set_next_hop_strategy(lua_State *L)
566565
size_t name_len;
567566

568567
name = luaL_checklstring(L, 1, &name_len);
569-
if (TS_SUCCESS != TSHttpTxnNextHopNamedStrategySet(http_ctx->txnp, name)) {
570-
TSError("[ts_lua][%s] Failed to set next hop strategy by name", __FUNCTION__);
568+
if (0 == name_len) {
569+
Dbg(dbg_ctl, "Clearning strategy (use parent.config)");
570+
TSHttpTxnNextHopStrategySet(http_ctx->txnp, nullptr);
571+
} else {
572+
void *const stratptr = TSHttpTxnNextHopNamedStrategyGet(http_ctx->txnp, name);
573+
if (nullptr == stratptr) {
574+
TSError("[ts_lua][%s] Failed get next hop strategy name '%s'", __FUNCTION__, name);
575+
} else {
576+
TSHttpTxnNextHopStrategySet(http_ctx->txnp, stratptr);
577+
}
571578
}
572579
} else {
573580
return luaL_error(L, "incorrect # of arguments for set_parent_proxy, receiving %d instead of 1", n);
@@ -576,18 +583,6 @@ ts_lua_http_set_next_hop_strategy(lua_State *L)
576583
return 0;
577584
}
578585

579-
static int
580-
ts_lua_http_clear_next_hop_strategy(lua_State *L)
581-
{
582-
ts_lua_http_ctx *http_ctx;
583-
584-
GET_HTTP_CONTEXT(http_ctx, L);
585-
586-
TSHttpTxnNextHopStrategySet(http_ctx->txnp, nullptr);
587-
588-
return 0;
589-
}
590-
591586
static int
592587
ts_lua_http_get_parent_proxy(lua_State *L)
593588
{

plugins/regex_remap/regex_remap.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ RemapRegex::initialize(const std::string &reg, const std::string &sub, const std
316316
_options |= PCRE_CASELESS;
317317
} else if (opt.compare(start, 23, "lowercase_substitutions") == 0) {
318318
_lowercase_substitutions = true;
319+
} else if (opt.compare(start, 8, "strategy") == 0) {
320+
_strategy = opt_val;
319321
} else if (opt_val.size() <= 0) {
320322
// All other options have a required value
321323
TSError("[%s] Malformed options: %s", PLUGIN_NAME, opt.c_str());
@@ -330,8 +332,6 @@ RemapRegex::initialize(const std::string &reg, const std::string &sub, const std
330332
_connect_timeout = strtol(opt_val.c_str(), nullptr, 10);
331333
} else if (opt.compare(start, 11, "dns_timeout") == 0) {
332334
_dns_timeout = strtol(opt_val.c_str(), nullptr, 10);
333-
} else if (opt.compare(start, 8, "strategy") == 0) {
334-
_strategy = opt_val;
335335
} else {
336336
TSOverridableConfigKey key;
337337
TSRecordDataType type;
@@ -981,12 +981,16 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
981981
TSHttpTxnDNSTimeoutSet(txnp, re->dns_timeout_option());
982982
}
983983
auto const &strat = re->strategy();
984-
if (!strat.empty()) {
985-
Dbg(dbg_ctl, "Setting strategy to %s", strat.c_str());
986-
if ("null" == strat) {
987-
TSHttpTxnNextHopStrategySet(txnp, nullptr);
988-
} else if (TS_ERROR == TSHttpTxnNextHopNamedStrategySet(txnp, strat.c_str())) {
989-
Dbg(dbg_ctl, "Error setting strategy to %s", strat.c_str());
984+
if (strat.empty()) {
985+
Dbg(dbg_ctl, "Clearing strategy (use parent.config)");
986+
TSHttpTxnNextHopStrategySet(txnp, nullptr);
987+
} else {
988+
void *const stratptr = TSHttpTxnNextHopNamedStrategyGet(txnp, strat.c_str());
989+
if (nullptr == stratptr) {
990+
Dbg(dbg_ctl, "No strategy found with name '%s'", strat.c_str());
991+
} else {
992+
Dbg(dbg_ctl, "Setting strategy to %s", strat.c_str());
993+
TSHttpTxnNextHopStrategySet(txnp, stratptr);
990994
}
991995
}
992996
bool lowercase_substitutions = false;

src/api/InkAPI.cc

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5003,15 +5003,12 @@ TSHttpTxnNextHopStrategySet(TSHttpTxn txnp, void *strategy)
50035003
}
50045004

50055005
char const *
5006-
TSHttpTxnNextHopStrategyNameGet(TSHttpTxn txnp)
5006+
TSHttpNextHopStrategyNameGet(void *stratptr)
50075007
{
5008-
sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
5009-
5010-
auto sm = reinterpret_cast<HttpSM const *>(txnp);
5011-
50125008
char const *name = nullptr;
5013-
if (nullptr != sm->t_state.next_hop_strategy) {
5014-
name = sm->t_state.next_hop_strategy->strategy_name.c_str();
5009+
if (nullptr != stratptr) {
5010+
auto strategy = reinterpret_cast<NextHopSelectionStrategy const *>(stratptr);
5011+
name = strategy->strategy_name.c_str();
50155012
}
50165013

50175014
return name;
@@ -5035,29 +5032,6 @@ TSHttpTxnNextHopNamedStrategyGet(TSHttpTxn txnp, const char *name)
50355032
return static_cast<void *>(strat);
50365033
}
50375034

5038-
TSReturnCode
5039-
TSHttpTxnNextHopNamedStrategySet(TSHttpTxn txnp, const char *name)
5040-
{
5041-
sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
5042-
sdk_assert(sdk_sanity_check_null_ptr((void *)name) == TS_SUCCESS);
5043-
5044-
auto sm = reinterpret_cast<HttpSM *>(txnp);
5045-
5046-
sdk_assert(sdk_sanity_check_null_ptr((void *)sm->m_remap) == TS_SUCCESS);
5047-
sdk_assert(sdk_sanity_check_null_ptr((void *)sm->m_remap->strategyFactory) == TS_SUCCESS);
5048-
5049-
// HttpSM has a reference count handle to UrlRewrite which has a
5050-
// pointer to NextHopStrategyFactory
5051-
NextHopSelectionStrategy *const strat = sm->m_remap->strategyFactory->strategyInstance(name);
5052-
5053-
if (nullptr == strat) {
5054-
return TS_ERROR;
5055-
}
5056-
5057-
sm->t_state.next_hop_strategy = strat;
5058-
return TS_SUCCESS;
5059-
}
5060-
50615035
TSReturnCode
50625036
TSHttpTxnParentProxyGet(TSHttpTxn txnp, const char **hostname, int *port)
50635037
{

tests/gold_tests/pluginTest/strategies/strategies_plugins.test.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@
7373
"body": name,
7474
}
7575
origin.addResponse("sessionfile.log", request_header, response_header)
76-
origin.ReturnCode = Any(0, -2)
76+
origin.ReturnCode = 0
7777
origins.append(origin)
7878
dns.addRecords(records={name: ["127.0.0.1"]})
7979

8080
# Define ATS and configure
8181
ts = Test.MakeATSProcess("ts", enable_cache=False)
82-
ts.ReturnCode = Any(0, -2)
82+
ts.ReturnCode = 0
8383
ts.Disk.records_config.update(
8484
{
8585
'proxy.config.dns.nameservers': f"127.0.0.1:{dns.Variables.Port}",
@@ -103,7 +103,7 @@
103103
ts.Disk.MakeConfigFile("regex_remap.config").AddLines(
104104
[
105105
"/nh0 http://origin/path @strategy=nh1",
106-
"/nh1 http://origin/path @strategy=null",
106+
'/nh1 http://origin/path @strategy=',
107107
"/nh2 http://origin/path @strategy=nh0",
108108
])
109109
ts.Disk.MakeConfigFile("strat.lua").AddLines(
@@ -113,7 +113,7 @@
113113
' if uri:find("nh0") then',
114114
' ts.http.set_next_hop_strategy("nh1")',
115115
' elseif uri:find("nh1") then',
116-
' ts.http.clear_next_hop_strategy("nh1")',
116+
' ts.http.set_next_hop_strategy("")',
117117
' elseif uri:find("nh2") then',
118118
' ts.http.set_next_hop_strategy("nh0")',
119119
' end',
@@ -190,7 +190,7 @@
190190
ps.StartBefore(dns)
191191
ps.StartBefore(Test.Processes.ts)
192192
tr.MakeCurlCommand(curl_and_args + " http://nh0_hr/path", ts=ts)
193-
ps.ReturnCode = Any(0, -2)
193+
ps.ReturnCode = 0
194194
ps.Streams.stdout.Content = Testers.ContainsExpression("nh0", "expected nh0")
195195
tr.StillRunningAfter = ts
196196
tr.StillRunnerAfter = dns
@@ -199,23 +199,23 @@
199199
tr = Test.AddTestRun("nh1_hr straight through request")
200200
ps = tr.Processes.Default
201201
tr.MakeCurlCommand(curl_and_args + " http://nh1_hr/path", ts=ts)
202-
ps.ReturnCode = Any(0, -2)
202+
ps.ReturnCode = 0
203203
ps.Streams.stdout.Content = Testers.ContainsExpression("nh1", "expected nh1")
204204
tr.StillRunningAfter = ts
205205

206206
# 2 - nh2_hr default request
207207
tr = Test.AddTestRun("nh2_hr straight through request")
208208
ps = tr.Processes.Default
209209
tr.MakeCurlCommand(curl_and_args + " http://nh2_hr/path", ts=ts)
210-
ps.ReturnCode = Any(0, -2)
210+
ps.ReturnCode = 0
211211
ps.Streams.stdout.Content = Testers.ContainsExpression("nh2", "expected nh2")
212212
tr.StillRunningAfter = ts
213213

214214
# 3 switch strategies
215215
tr = Test.AddTestRun("nh0_hr switch to nh1")
216216
ps = tr.Processes.Default
217217
tr.MakeCurlCommand(curl_and_args + ' http://nh0_hr/path -H "Strategy: nh1"', ts=ts)
218-
ps.ReturnCode = Any(0, -2)
218+
ps.ReturnCode = 0
219219
ps.Streams.stdout.Content = Testers.ContainsExpression("nh1", "expected nh1")
220220
tr.StillRunningAfter = ts
221221
tr.StillRunnerAfter = dns
@@ -224,7 +224,7 @@
224224
tr = Test.AddTestRun("nh1_hr switch to parent.config")
225225
ps = tr.Processes.Default
226226
tr.MakeCurlCommand(curl_and_args + ' http://nh1_hr/path -H "Strategy: null"', ts=ts)
227-
ps.ReturnCode = Any(0, -2)
227+
ps.ReturnCode = 0
228228
ps.Streams.stdout.Content = Testers.ContainsExpression("nh2", "expected nh2")
229229
tr.StillRunningAfter = ts
230230
tr.StillRunnerAfter = dns
@@ -233,7 +233,7 @@
233233
tr = Test.AddTestRun("nh2_hr switch to nh0")
234234
ps = tr.Processes.Default
235235
tr.MakeCurlCommand(curl_and_args + ' http://nh2_hr/path -H "Strategy: nh0"', ts=ts)
236-
ps.ReturnCode = Any(0, -2)
236+
ps.ReturnCode = 0
237237
ps.Streams.stdout.Content = Testers.ContainsExpression("nh0", "expected nh0")
238238
tr.StillRunningAfter = ts
239239
tr.StillRunnerAfter = dns
@@ -244,16 +244,16 @@
244244
tr = Test.AddTestRun("nh0_rr switch to nh1")
245245
ps = tr.Processes.Default
246246
tr.MakeCurlCommand(curl_and_args + ' http://nh0_rr/nh0', ts=ts)
247-
ps.ReturnCode = Any(0, -2)
247+
ps.ReturnCode = 0
248248
ps.Streams.stdout.Content = Testers.ContainsExpression("nh1", "expected nh1")
249249
tr.StillRunningAfter = ts
250250
tr.StillRunnerAfter = dns
251251

252-
# 7 strategy to parent.config
252+
# ' strategy to parent.config
253253
tr = Test.AddTestRun("nh1_rr switch to parent.config")
254254
ps = tr.Processes.Default
255255
tr.MakeCurlCommand(curl_and_args + ' http://nh1_rr/nh1', ts=ts)
256-
ps.ReturnCode = Any(0, -2)
256+
ps.ReturnCode = 0
257257
ps.Streams.stdout.Content = Testers.ContainsExpression("nh2", "expected nh2")
258258
tr.StillRunningAfter = ts
259259
tr.StillRunnerAfter = dns
@@ -262,7 +262,7 @@
262262
tr = Test.AddTestRun("nh2_rr switch to nh0")
263263
ps = tr.Processes.Default
264264
tr.MakeCurlCommand(curl_and_args + ' http://nh2_rr/nh2', ts=ts)
265-
ps.ReturnCode = Any(0, -2)
265+
ps.ReturnCode = 0
266266
ps.Streams.stdout.Content = Testers.ContainsExpression("nh0", "expected nh0")
267267
tr.StillRunningAfter = ts
268268
tr.StillRunnerAfter = dns
@@ -273,7 +273,7 @@
273273
tr = Test.AddTestRun("nh0_lua switch to nh1")
274274
ps = tr.Processes.Default
275275
tr.MakeCurlCommand(curl_and_args + ' http://nh0_lua/nh0', ts=ts)
276-
ps.ReturnCode = Any(0, -2)
276+
ps.ReturnCode = 0
277277
ps.Streams.stdout.Content = Testers.ContainsExpression("nh1", "expected nh1")
278278
tr.StillRunningAfter = ts
279279
tr.StillRunnerAfter = dns
@@ -282,7 +282,7 @@
282282
tr = Test.AddTestRun("nh1_lua switch to parent.config")
283283
ps = tr.Processes.Default
284284
tr.MakeCurlCommand(curl_and_args + ' http://nh1_lua/nh1', ts=ts)
285-
ps.ReturnCode = Any(0, -2)
285+
ps.ReturnCode = 0
286286
ps.Streams.stdout.Content = Testers.ContainsExpression("nh2", "expected nh2")
287287
tr.StillRunningAfter = ts
288288
tr.StillRunnerAfter = dns
@@ -291,7 +291,7 @@
291291
tr = Test.AddTestRun("nh2_lua switch to nh0")
292292
ps = tr.Processes.Default
293293
tr.MakeCurlCommand(curl_and_args + ' http://nh2_lua/nh2', ts=ts)
294-
ps.ReturnCode = Any(0, -2)
294+
ps.ReturnCode = 0
295295
ps.Streams.stdout.Content = Testers.ContainsExpression("nh0", "expected nh0")
296296
tr.StillRunningAfter = ts
297297
tr.StillRunnerAfter = dns

0 commit comments

Comments
 (0)