xref: /trafficserver/mgmt/ProxyConfig.h (revision 3ea91a0b)
1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include <atomic>
27 
28 #include "ProcessManager.h"
29 #include "I_Tasks.h"
30 
31 class ProxyMutex;
32 
33 void *config_int_cb(void *data, void *value);
34 void *config_long_long_cb(void *data, void *value);
35 void *config_float_cb(void *data, void *value);
36 void *config_string511_cb(void *data, void *value);
37 void *config_string_alloc_cb(void *data, void *value);
38 
39 //
40 // Macros that spin waiting for the data to be bound
41 //
42 #define SignalManager(_n, _d) pmgmt->signalManager(_n, (char *)_d)
43 #define SignalWarning(_n, _s) \
44   {                           \
45     Warning("%s", _s);        \
46     SignalManager(_n, _s);    \
47   }
48 
49 #define RegisterMgmtCallback(_signal, _fn, _data) pmgmt->registerMgmtCallback(_signal, _fn, _data)
50 
51 #define MAX_CONFIGS 100
52 
53 typedef RefCountObj ConfigInfo;
54 
55 class ConfigProcessor
56 {
57 public:
58   ConfigProcessor() = default;
59 
60   enum {
61     // The number of seconds to wait before garbage collecting stale ConfigInfo objects. There's
62     // no good reason to tune this, outside of regression tests, so don't.
63     CONFIG_PROCESSOR_RELEASE_SECS = 60
64   };
65 
66   template <typename ClassType, typename ConfigType> struct scoped_config {
scoped_configConfigProcessor::scoped_config67     scoped_config() : ptr(ClassType::acquire()) {}
~scoped_configConfigProcessor::scoped_config68     ~scoped_config() { ClassType::release(ptr); }
69     operator bool() const { return ptr != nullptr; }
70     operator const ConfigType *() const { return ptr; }
operator ->ConfigProcessor::scoped_config71     const ConfigType *operator->() const { return ptr; }
72 
73   private:
74     ConfigType *ptr;
75   };
76 
77   unsigned int set(unsigned int id, ConfigInfo *info, unsigned timeout_secs = CONFIG_PROCESSOR_RELEASE_SECS);
78   ConfigInfo *get(unsigned int id);
79   void release(unsigned int id, ConfigInfo *data);
80 
81 public:
82   std::atomic<ConfigInfo *> infos[MAX_CONFIGS] = {nullptr};
83   std::atomic<int> ninfos{0};
84 };
85 
86 // A Continuation wrapper that calls the static reconfigure() method of the given class.
87 template <typename UpdateClass> struct ConfigUpdateContinuation : public Continuation {
88   int
updateConfigUpdateContinuation89   update(int /* etype */, void * /* data */)
90   {
91     UpdateClass::reconfigure();
92     delete this;
93     return EVENT_DONE;
94   }
95 
ConfigUpdateContinuationConfigUpdateContinuation96   ConfigUpdateContinuation(Ptr<ProxyMutex> &m) : Continuation(m.get()) { SET_HANDLER(&ConfigUpdateContinuation::update); }
97 };
98 
99 template <typename UpdateClass>
100 int
ConfigScheduleUpdate(Ptr<ProxyMutex> & mutex)101 ConfigScheduleUpdate(Ptr<ProxyMutex> &mutex)
102 {
103   eventProcessor.schedule_imm(new ConfigUpdateContinuation<UpdateClass>(mutex), ET_TASK);
104   return 0;
105 }
106 
107 template <typename UpdateClass> struct ConfigUpdateHandler {
ConfigUpdateHandlerConfigUpdateHandler108   ConfigUpdateHandler() : mutex(new_ProxyMutex()) {}
109   // The mutex member is ref-counted so should not explicitly free it
~ConfigUpdateHandlerConfigUpdateHandler110   ~ConfigUpdateHandler() {}
111   int
attachConfigUpdateHandler112   attach(const char *name)
113   {
114     return REC_RegisterConfigUpdateFunc(name, ConfigUpdateHandler::update, this);
115   }
116 
117 private:
118   static int
updateConfigUpdateHandler119   update(const char *name, RecDataT /* data_type ATS_UNUSED */, RecData /* data ATS_UNUSED */, void *cookie)
120   {
121     ConfigUpdateHandler *self = static_cast<ConfigUpdateHandler *>(cookie);
122 
123     Debug("config", "%s(%s)", __PRETTY_FUNCTION__, name);
124     return ConfigScheduleUpdate<UpdateClass>(self->mutex);
125   }
126 
127   Ptr<ProxyMutex> mutex;
128 };
129 
130 extern ConfigProcessor configProcessor;
131