<commit_before>(ns puppetlabs.pcp.broker.service
  (:require [puppetlabs.pcp.broker.core :as core]
            [puppetlabs.structured-logging.core :as sl]
            [clj-time.core :as t]
            [clojure.string :as str]
            [puppetlabs.kitchensink.core :as ks]
            [puppetlabs.trapperkeeper.core :as trapperkeeper]
            [puppetlabs.trapperkeeper.services :refer [service-context get-service]]
            [puppetlabs.trapperkeeper.services.status.status-core :as status-core]
            [puppetlabs.trapperkeeper.services.webserver.jetty9-core :as jetty9-core]
            [puppetlabs.i18n.core :as i18n]))

(defprotocol BrokerService)

(trapperkeeper/defservice broker-service
  BrokerService
  [[:AuthorizationService authorization-check]
   [:ConfigService get-in-config]
   [:WebroutingService add-websocket-handler get-server get-route]
   [:MetricsService get-metrics-registry]
   [:StatusService register-status]]
  (init [this context]
        (sl/maplog :info {:type :broker-init} (fn [_] (i18n/trs "Initializing broker service.")))
        (let [max-connections    (get-in-config [:pcp-broker :max-connections] 0)
              max-message-size   (get-in-config [:pcp-broker :max-message-size] (* 64 1024 1024))
              idle-timeout       (get-in-config [:pcp-broker :idle-timeout] (* 1000 60 15))
              broker             (core/init {:add-websocket-handler (partial add-websocket-handler this)
                                             :authorization-check   authorization-check
                                             :get-metrics-registry  get-metrics-registry
                                             :max-connections       max-connections
                                             :max-message-size      max-message-size
                                             :idle-timeout          idle-timeout
                                             :get-route             (partial get-route this)})]
          (register-status "broker-service"
                           (status-core/get-artifact-version "puppetlabs" "pcp-broker")
                           1
                           (partial core/status broker))
          (assoc context :broker broker)))
  (start [this context]
         (sl/maplog :info {:type :broker-start} (fn [_] (i18n/trs "Starting broker service.")))
         (let [controller-uris (get-in-config [:pcp-broker :controller-uris] [])
               controller-disconnection-ms (-> (get-in-config [:pcp-broker
                                                               :controller-disconnection-graceperiod]
                                                              "90s")
                                               ks/parse-interval
                                               t/in-millis)
               controller-whitelist (set (get-in-config [:pcp-broker :controller-whitelist]
                                                        ["http://puppetlabs.com/inventory_request"]))
               broker (:broker context)
               server-context (some-> (get-service this :WebserverService)
                                      service-context
                                      (jetty9-core/get-server-context (keyword (get-server this :v1))))
               broker-name (or (:broker-name broker)
                               (core/get-webserver-cn server-context)<commit_msg>(maint) Lower default idle-timeout from 15 minutes to 6<commit_after>(ns puppetlabs.pcp.broker.service
  (:require [puppetlabs.pcp.broker.core :as core]
            [puppetlabs.structured-logging.core :as sl]
            [clj-time.core :as t]
            [clojure.string :as str]
            [puppetlabs.kitchensink.core :as ks]
            [puppetlabs.trapperkeeper.core :as trapperkeeper]
            [puppetlabs.trapperkeeper.services :refer [service-context get-service]]
            [puppetlabs.trapperkeeper.services.status.status-core :as status-core]
            [puppetlabs.trapperkeeper.services.webserver.jetty9-core :as jetty9-core]
            [puppetlabs.i18n.core :as i18n]))

(defprotocol BrokerService)

(trapperkeeper/defservice broker-service
  BrokerService
  [[:AuthorizationService authorization-check]
   [:ConfigService get-in-config]
   [:WebroutingService add-websocket-handler get-server get-route]
   [:MetricsService get-metrics-registry]
   [:StatusService register-status]]
  (init [this context]
        (sl/maplog :info {:type :broker-init} (fn [_] (i18n/trs "Initializing broker service.")))
        (let [max-connections    (get-in-config [:pcp-broker :max-connections] 0)
              max-message-size   (get-in-config [:pcp-broker :max-message-size] (* 64 1024 1024))
              idle-timeout       (get-in-config [:pcp-broker :idle-timeout] (* 1000 60 6))
              broker             (core/init {:add-websocket-handler (partial add-websocket-handler this)
                                             :authorization-check   authorization-check
                                             :get-metrics-registry  get-metrics-registry
                                             :max-connections       max-connections
                                             :max-message-size      max-message-size
                                             :idle-timeout          idle-timeout
                                             :get-route             (partial get-route this)})]
          (register-status "broker-service"
                           (status-core/get-artifact-version "puppetlabs" "pcp-broker")
                           1
                           (partial core/status broker))
          (assoc context :broker broker)))
  (start [this context]
         (sl/maplog :info {:type :broker-start} (fn [_] (i18n/trs "Starting broker service.")))
         (let [controller-uris (get-in-config [:pcp-broker :controller-uris] [])
               controller-disconnection-ms (-> (get-in-config [:pcp-broker
                                                               :controller-disconnection-graceperiod]
                                                              "90s")
                                               ks/parse-interval
                                               t/in-millis)
               controller-whitelist (set (get-in-config [:pcp-broker :controller-whitelist]
                                                        ["http://puppetlabs.com/inventory_request"]))
               broker (:broker context)
               server-context (some-> (get-service this :WebserverService)
                                      service-context
                                      (jetty9-core/get-server-context (keyword (get-server this :v1))))
               broker-name (or (:broker-name broker)
                               (core/get-webserver-cn server-context)
{'fraction_non_alphanumeric': 0.10316649642492338, 'fraction_numerical': 0.010214504596527068, 'mean_word_length': 1.4287739192062368, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4025902', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1764, 'n_words': 339}