<commit_before>%% @author Couchbase <<EMAIL>>
%% @copyright 2013 Couchbase, Inc
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc pool of sockets that are used for long blocking memcached calls
%%
-module(ns_memcached_sockets_pool).

-include("ns_common.hrl").

-export([start_link/0]).

-export([take_socket/1, put_socket/1]).

start_link() ->
    Options = [{name, ?MODULE},
               {connection_timeout, 30000},
               {pool_size, 10000}],
    ns_connection_pool:start_link(Options).


take_socket(Bucket) ->
    Result =
        case ns_connection_pool:maybe_take_socket(?MODULE, ns_memcached) of
            {ok, Sock} ->
                {ok, Sock};
            no_socket ->
                ns_memcached:connect()
        end,

    case Result of
        {ok, Socket} ->
            case mc_client_binary:select_bucket(Socket, Bucket) of
                ok ->
                    {ok, Socket};
                Err ->
                    {error, {select_bucket_failed, Err}}
            end;
        Error ->
            Error
    end.

put_socket(Socket) ->
    ns_connection_pool:put_socket(?MODULE, ns_memcached, Socket).
<commit_msg>allow not to set bucket when taking socket from memcached pool<commit_after>%% @author Couchbase <<EMAIL>>
%% @copyright 2013 Couchbase, Inc
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc pool of sockets that are used for long blocking memcached calls
%%
-module(ns_memcached_sockets_pool).

-include("ns_common.hrl").

-export([start_link/0]).

-export([take_socket/0, take_socket/1, put_socket/1, executing_on_socket/1]).

start_link() ->
    Options = [{name, ?MODULE},
               {connection_timeout, 30000},
               {pool_size, 10000}],
    ns_connection_pool:start_link(Options).

take_socket() ->
    case ns_connection_pool:maybe_take_socket(?MODULE, ns_memcached) of
        {ok, Sock} ->
            {ok, Sock};
        no_socket ->
            ns_memcached:connect()
    end.

take_socket(Bucket) ->
    case take_socket() of
        {ok, Socket} ->
            case mc_client_binary:select_bucket(Socket, Bucket) of
                ok ->
                    {ok, Socket};
                Err ->
                    {error, {select_bucket_failed, Err}}
            end;
        Error ->
            Error
    end.

put_socket(Socket) ->
    ns_connection_pool:put_socket(?MODULE, ns_memcached, Socket).

executing_on_socket(Fun) ->
    misc:executing_on_new_process(
      fun () ->
              case ns_memcached_sockets_pool:take_socket() of
                  {ok, Sock} ->
                      Result = Fun(Sock),
                      ns_memcached_sockets_pool:put_socket(Sock),
                      Result;
                  Error ->
                      Error
              end
      end).

{'fraction_non_alphanumeric': 0.10628272251308901, 'fraction_numerical': 0.011518324607329843, 'mean_word_length': 2.369488536155203, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4605857', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1066, 'n_words': 326}