Skip to content

Commit 61e5afe

Browse files
committed
add strategies api for getting a strategy during remap plugin init
1 parent 8e0845c commit 61e5afe

File tree

15 files changed

+348
-90
lines changed

15 files changed

+348
-90
lines changed

doc/developer-guide/api/functions/TSHttpTxnNextHopNamedStrategyGet.en.rst renamed to doc/developer-guide/api/functions/TSHttpTxnNextHopStrategyFind.en.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
.. default-domain:: cpp
2020

21-
TSHttpTxnNextHopNamedStrategyGet
22-
********************************
21+
TSHttpTxnNextHopStrategyFind
22+
****************************
2323

2424
Synopsis
2525
========
@@ -28,7 +28,7 @@ Synopsis
2828
2929
#include <ts/ts.h>
3030
31-
.. function:: void const* TSHttpTxnNextHopNamedStrategyGet(TSHttpTxn txnp, const char *name)
31+
.. function:: void const* TSHttpTxnNextHopStrategyFind(TSHttpTxn txnp, const char *name)
3232

3333
Description
3434
===========

doc/developer-guide/api/functions/TSHttpTxnNextHopStrategySet.en.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ nullptr to indicate that parent.config will be used instead.
3939

4040
Plugins can get a strategy by name by calling
4141
:func:`TSHttpTxnNextHopStrategyGet` to get the current transaction's
42-
active strategy or :func:`TSHttpTxnNextHopNamedStrategyGet` to
42+
active strategy or :func:`TSHttpTxnNextHopStrategyFind` to
4343
look up a strategy by name using the transaction's pointer to the
4444
NextHopStrategyFactory strategy database.
4545

@@ -53,4 +53,4 @@ NextHopStrategyFactory strategy database.
5353
See Also
5454
========
5555

56-
:func:`TSHttpTxnNextHopStrategyGet`, :func:`TSHttpTxnNextHopNamedStrategyGet`.
56+
:func:`TSHttpTxnNextHopStrategyGet`, :func:`TSHttpTxnNextHopStrategyFind`.

doc/developer-guide/api/functions/TSHttpNextHopStrategyNameGet.en.rst renamed to doc/developer-guide/api/functions/TSNextHopStrategyNameGet.en.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
.. default-domain:: cpp
2020

21-
TSHttpNextHopStrategyNameGet
22-
****************************
21+
TSNextHopStrategyNameGet
22+
************************
2323

2424
Synopsis
2525
========
@@ -28,13 +28,13 @@ Synopsis
2828
2929
#include <ts/ts.h>
3030
31-
.. function:: char const* TSHttpNextHopStrategyNameGet(void const* strategy)
31+
.. function:: char const* TSNextHopStrategyNameGet(void const* strategy)
3232

3333
Description
3434
===========
3535

36-
Gets the name associated with the provided strategy.
37-
This may be nullptr indicating that parent.config is in use.
36+
Gets the name associated with the provided strategy pointer.
37+
This may be nullptr which indicates that parent.config is in use.
3838

3939
.. note::
4040

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
contributor license agreements. See the NOTICE file distributed
3+
with this work for additional information regarding copyright
4+
ownership. The ASF licenses this file to you under the Apache
5+
License, Version 2.0 (the "License"); you may not use this file
6+
except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
implied. See the License for the specific language governing
15+
permissions and limitations under the License.
16+
17+
.. include:: ../../../common.defs
18+
19+
.. default-domain:: cpp
20+
21+
TSRemapNextHopStrategyFind
22+
**************************
23+
24+
Synopsis
25+
========
26+
27+
.. code-block:: cpp
28+
29+
#include <ts/ts.h>
30+
31+
.. function:: void const* TSRemapNextHopStrategyFind(const char *name)
32+
33+
Description
34+
===========
35+
36+
Gets a pointer to the specified :arg:`name` NextHopSelectionStrategy.
37+
This may be nullptr indicating that no strategy exists with the given name.
38+
39+
This function may ONLY be called during TSRemapNewInstance.
40+
The resulting strategy pointer is valid for all subsequent transactions.
41+
42+
.. note::
43+
44+
This returned pointer must not be freed and the contents must not
45+
be changed.
46+
Strategy pointers held by plugins will become invalid when ATS
47+
configs are reloaded and should be reset with :func:`TSRemapNewInstance`
48+
49+
See Also
50+
========
51+
52+
:func:`TSRemapNextHopStrategySet`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
contributor license agreements. See the NOTICE file distributed
3+
with this work for additional information regarding copyright
4+
ownership. The ASF licenses this file to you under the Apache
5+
License, Version 2.0 (the "License"); you may not use this file
6+
except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
implied. See the License for the specific language governing
15+
permissions and limitations under the License.
16+
17+
.. include:: ../../../common.defs
18+
19+
.. default-domain:: cpp
20+
21+
TSRemapNextHopStrategyGet
22+
*************************
23+
24+
Synopsis
25+
========
26+
27+
.. code-block:: cpp
28+
29+
#include <ts/ts.h>
30+
31+
.. function:: void const* TSRemapNextHopStrategyGet()
32+
33+
Description
34+
===========
35+
36+
Gets a pointer to the current remap rule being loaded.
37+
This may be nullptr indicating that parent.config is in use.
38+
39+
This function may ONLY be called during TSRemapNewInstance.
40+
The resulting strategy pointer is valid for all subsequent transactions.
41+
42+
.. note::
43+
44+
This strategy pointer must not be freed and the contents must not
45+
be changed.
46+
Strategy pointers held by plugins will become invalid when ATS
47+
configs are reloaded and should be reset with :func:`TSRemapNewInstance`
48+
49+
See Also
50+
========
51+
52+
:func:`TSRemapNextHopStrategySet`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
contributor license agreements. See the NOTICE file distributed
3+
with this work for additional information regarding copyright
4+
ownership. The ASF licenses this file to you under the Apache
5+
License, Version 2.0 (the "License"); you may not use this file
6+
except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
implied. See the License for the specific language governing
15+
permissions and limitations under the License.
16+
17+
.. include:: ../../../common.defs
18+
19+
.. default-domain:: cpp
20+
21+
TSRemapNextHopNameGet
22+
***********************
23+
24+
Synopsis
25+
========
26+
27+
.. code-block:: cpp
28+
29+
#include <ts/ts.h>
30+
31+
.. function:: void TSRemapNextHopStrategySet(void const* strategy)
32+
33+
Description
34+
===========
35+
36+
Sets the next hop strategy for the currently loading remap rule.
37+
This :arg:`strategy` pointer must be a valid strategy and can be
38+
nullptr to indicate that parent.config will be used instead.
39+
40+
Plugins can get a strategy by name by calling
41+
:func:`TSRemapNextHopStrategyGet` to get the current transaction's active
42+
strategy or :func:`TSRemapNextHopStrategyFind` to look up a strategy by
43+
name using the loading remap rule's pointer to the NextHopStrategyFactory
44+
strategy database.
45+
46+
.. note::
47+
48+
This strategy pointer must not be freed and the contents must not
49+
be changed.
50+
Strategy pointers held by plugins will become invalid when ATS
51+
configs are reloaded and should be reset with :func:`TSRemapNewInstance`
52+
53+
See Also
54+
========
55+
56+
:func:`TSRemapNextHopStrategyGet`, :func:`TSRemapNextHopStrategyFind`.

include/proxy/http/remap/UrlMapping.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "tscore/List.h"
3737

3838
class NextHopSelectionStrategy;
39+
class NextHopStrategyFactory;
3940

4041
/**
4142
* Used to store http referrer strings (and/or regexp)
@@ -112,9 +113,15 @@ class url_mapping
112113
bool ip_allow_check_enabled_p = false;
113114
acl_filter_rule *filter = nullptr; // acl filtering (linked list of rules)
114115
LINK(url_mapping, link); // For use with the main Queue linked list holding all the mapping
115-
NextHopSelectionStrategy *strategy = nullptr;
116-
std::string remapKey;
117-
std::atomic<uint64_t> _hitCount = 0; // counter can overflow
116+
117+
NextHopSelectionStrategy *strategy = nullptr;
118+
NextHopStrategyFactory *strategyFactory = nullptr;
119+
120+
std::string remapKey;
121+
std::atomic<uint64_t> _hitCount = 0; // counter can overflow
122+
123+
// For use with the strategies API to be called during TSRemapNewInstance.
124+
static inline url_mapping *instance = nullptr;
118125

119126
int
120127
getRank() const

include/ts/ts.h

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,10 +1579,22 @@ void TSHttpTxnErrorBodySet(TSHttpTxn txnp, char *buf, size_t buflength, char *mi
15791579
*/
15801580
char *TSHttpTxnErrorBodyGet(TSHttpTxn txnp, size_t *buflength, char **mimetype);
15811581

1582+
/**
1583+
Retrieves a handle to the named strategy in the strategy table.
1584+
Returns nullptr if no strategy is found.
1585+
This uses the current transaction's state machine to get
1586+
access to UrlRewrite's NextHopStrategyFactory.
1587+
It's preferable to retrieve strategies during TSRemapNewInstance.
1588+
1589+
@param txnp HTTP transaction which holds the strategy table.
1590+
@param name of the strategy to look up.
1591+
1592+
*/
1593+
void const *TSHttpTxnNextHopStrategyFind(TSHttpTxn txnp, const char *name);
1594+
15821595
/**
15831596
Sets the Transaction's Next Hop Parent Strategy.
1584-
Calling this after TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK will
1585-
result in bad behavior.
1597+
Must be called before parent selection logic is required.
15861598
15871599
You can get this strategy pointer by calling TSHttpTxnParentStrategyGet().
15881600
@@ -1605,28 +1617,51 @@ void const *TSHttpTxnNextHopStrategyGet(TSHttpTxn txnp);
16051617

16061618
/**
16071619
Returns either null pointer or null terminated pointer to name.
1608-
DO NOT FREE.
1620+
DO NOT FREE.
16091621
16101622
This value may be a nullptr due to:
16111623
- parent proxying not enabled
16121624
- no parent selection strategy (using parent.config)
16131625
1614-
@param txnp HTTP transaction whose next hop strategy to get.
1626+
@param pointer to the NextHopStrategy.
16151627
16161628
*/
1617-
char const *TSHttpNextHopStrategyNameGet(void const *strategy);
1629+
char const *TSNextHopStrategyNameGet(void const *strategy);
16181630

16191631
/**
16201632
Retrieves a pointer to the named strategy in the strategy table.
1621-
Returns nullptr if no strategy is set.
1622-
This uses the current transaction's state machine to get
1623-
access to UrlRewrite's NextHopStrategyFactory.
1633+
This can only be called during TSRemapNewInstance.
1634+
DO NOT FREE.
1635+
1636+
Returns nullptr if no strategy found.
1637+
This uses the currently being loaded RemapConfig NextHopStrategyFactory.
16241638
1625-
@param txnp HTTP transaction which holds the strategy table.
16261639
@param name of the strategy to look up.
16271640
16281641
*/
1629-
void const *TSHttpTxnNextHopNamedStrategyGet(TSHttpTxn txnp, const char *name);
1642+
void const *TSRemapNextHopStrategyFind(const char *name);
1643+
1644+
/**
1645+
Retrieves a pointer to remap rule strategy pointer.
1646+
This can only be called during TSRemapNewInstance.
1647+
DO NOT FREE.
1648+
1649+
Returns nullptr if no strategy assigned.
1650+
1651+
*/
1652+
void const *TSRemapNextHopStrategyGet();
1653+
1654+
/**
1655+
Sets the remap rule's next hop strategy.
1656+
This can only be called during TSRemapNewInstance.
1657+
DO NOT FREE.
1658+
1659+
This strategy must be present in the loading NextHopStrategyFactory.
1660+
1661+
@param handle to the strategy to set.
1662+
1663+
*/
1664+
void TSRemapNextHopStrategySet(void const *strategy);
16301665

16311666
/**
16321667
Sets the parent proxy name and port. The string hostname is copied

plugins/header_rewrite/conditions.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,12 +1539,13 @@ 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 = TSHttpNextHopStrategyNameGet(res.state.txnp);
1543-
if (nullptr != name) {
1542+
void const *const strategy = TSHttpTxnNextHopStrategyGet(res.state.txnp);
1543+
if (nullptr != strategy) {
1544+
char const *const name = TSNextHopStrategyNameGet(strategy);
15441545
Dbg(pi_dbg_ctl, "Appending '%s' to evaluation value", name);
15451546
s.append(name);
15461547
} else {
1547-
Dbg(pi_dbg_ctl, "NextHopStrategyName is empty");
1548+
Dbg(pi_dbg_ctl, "NextHopStrategy is empty");
15481549
}
15491550
} break;
15501551
default:

0 commit comments

Comments
 (0)