1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 * Portions Copyright (C) 2013 CohortFS, LLC
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
16 #ifndef SIMPLE_POLICY_MESSENGER_H
17 #define SIMPLE_POLICY_MESSENGER_H
19 #include "Messenger.h"
21 class SimplePolicyMessenger : public Messenger
24 /// lock protecting policy
26 /// the default Policy we use for Pipes
27 Policy default_policy;
28 /// map specifying different Policies for specific peer types
29 map<int, Policy> policy_map; // entity_name_t::type -> Policy
33 SimplePolicyMessenger(CephContext *cct, entity_name_t name,
34 string mname, uint64_t _nonce)
35 : Messenger(cct, name),
36 policy_lock("SimplePolicyMessenger::policy_lock")
41 * Get the Policy associated with a type of peer.
42 * @param t The peer type to get the default policy for.
44 * @return A const Policy reference.
46 Policy get_policy(int t) override {
47 Mutex::Locker l(policy_lock);
48 map<int, Policy>::iterator iter =
50 if (iter != policy_map.end())
53 return default_policy;
56 Policy get_default_policy() override {
57 Mutex::Locker l(policy_lock);
58 return default_policy;
62 * Set a policy which is applied to all peers who do not have a type-specific
64 * This is an init-time function and cannot be called after calling
67 * @param p The Policy to apply.
69 void set_default_policy(Policy p) override {
70 Mutex::Locker l(policy_lock);
74 * Set a policy which is applied to all peers of the given type.
75 * This is an init-time function and cannot be called after calling
78 * @param type The peer type this policy applies to.
79 * @param p The policy to apply.
81 void set_policy(int type, Policy p) override {
82 Mutex::Locker l(policy_lock);
87 * Set a Throttler which is applied to all Messages from the given
89 * This is an init-time function and cannot be called after calling
92 * @param type The peer type this Throttler will apply to.
93 * @param t The Throttler to apply. SimpleMessenger does not take
94 * ownership of this pointer, but you must not destroy it before
95 * you destroy SimpleMessenger.
97 void set_policy_throttlers(int type,
98 Throttle *byte_throttle,
99 Throttle *msg_throttle) override {
100 Mutex::Locker l(policy_lock);
101 map<int, Policy>::iterator iter =
102 policy_map.find(type);
103 if (iter != policy_map.end()) {
104 iter->second.throttler_bytes = byte_throttle;
105 iter->second.throttler_messages = msg_throttle;
107 default_policy.throttler_bytes = byte_throttle;
108 default_policy.throttler_messages = msg_throttle;
112 }; /* SimplePolicyMessenger */
114 #endif /* SIMPLE_POLICY_MESSENGER_H */