Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
c9a2a08c
Commit
c9a2a08c
authored
Aug 16, 2018
by
Ge Jun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make methods in AdaptiveMaxConcurrency more reasonable
parent
81e346a2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
40 deletions
+85
-40
adaptive_max_concurrency.cpp
src/brpc/adaptive_max_concurrency.cpp
+60
-23
adaptive_max_concurrency.h
src/brpc/adaptive_max_concurrency.h
+25
-17
No files found.
src/brpc/adaptive_max_concurrency.cpp
View file @
c9a2a08c
...
...
@@ -16,14 +16,31 @@
#include <cstring>
#include <strings.h>
#include "butil/string_printf.h"
#include "butil/logging.h"
#include "butil/strings/string_number_conversions.h"
#include "brpc/adaptive_max_concurrency.h"
namespace
brpc
{
AdaptiveMaxConcurrency
::
AdaptiveMaxConcurrency
()
:
_value
(
UNLIMITED
())
,
_max_concurrency
(
0
)
{
}
AdaptiveMaxConcurrency
::
AdaptiveMaxConcurrency
(
int
max_concurrency
)
:
_max_concurrency
(
0
)
{
if
(
max_concurrency
<=
0
)
{
_value
=
UNLIMITED
();
_max_concurrency
=
0
;
}
else
{
_value
=
butil
::
string_printf
(
"%d"
,
max_concurrency
);
_max_concurrency
=
max_concurrency
;
}
}
inline
bool
CompareStringPieceWithoutCase
(
const
butil
::
StringPiece
&
s1
,
const
char
*
s2
)
{
const
butil
::
StringPiece
&
s1
,
const
char
*
s2
)
{
DCHECK
(
s2
!=
NULL
);
if
(
std
::
strlen
(
s2
)
!=
s1
.
size
())
{
return
false
;
...
...
@@ -31,41 +48,61 @@ inline bool CompareStringPieceWithoutCase(
return
::
strncasecmp
(
s1
.
data
(),
s2
,
s1
.
size
())
==
0
;
}
AdaptiveMaxConcurrency
::
AdaptiveMaxConcurrency
(
const
butil
::
StringPiece
&
name
)
{
if
(
butil
::
StringToInt
(
name
,
&
_max_concurrency
)
&&
_max_concurrency
>=
0
)
{
_name
=
"constant"
;
}
else
if
(
_max_concurrency
<
0
)
{
LOG
(
FATAL
)
<<
"Invalid max_concurrency: "
<<
name
;
AdaptiveMaxConcurrency
::
AdaptiveMaxConcurrency
(
const
butil
::
StringPiece
&
value
)
:
_max_concurrency
(
0
)
{
int
max_concurrency
=
0
;
if
(
butil
::
StringToInt
(
value
,
&
max_concurrency
)
)
{
operator
=
(
max_concurrency
)
;
}
else
{
_name
.
assign
(
name
.
begin
(),
name
.
end
()
);
_max_concurrency
=
0
;
value
.
CopyToString
(
&
_value
);
_max_concurrency
=
-
1
;
}
}
void
AdaptiveMaxConcurrency
::
operator
=
(
const
butil
::
StringPiece
&
nam
e
)
{
void
AdaptiveMaxConcurrency
::
operator
=
(
const
butil
::
StringPiece
&
valu
e
)
{
int
max_concurrency
=
0
;
if
(
butil
::
StringToInt
(
name
,
&
max_concurrency
)
&&
max_concurrency
>=
0
)
{
_name
=
"constant"
;
_max_concurrency
=
max_concurrency
;
}
else
if
(
max_concurrency
<
0
)
{
LOG
(
ERROR
)
<<
"Fail to set max_concurrency, invalid value:"
<<
name
;
}
else
if
(
CompareStringPieceWithoutCase
(
name
,
"constant"
))
{
LOG
(
WARNING
)
<<
"If you want to use a constant maximum concurrency, assign "
<<
"an integer value directly to ServerOptions.max_concurrency "
<<
"like: `server_options.max_concurrency = 1000`"
;
_name
.
assign
(
name
.
begin
(),
name
.
end
());
_max_concurrency
=
0
;
if
(
butil
::
StringToInt
(
value
,
&
max_concurrency
))
{
return
operator
=
(
max_concurrency
);
}
else
{
_name
.
assign
(
name
.
begin
(),
name
.
end
());
value
.
CopyToString
(
&
_value
);
_max_concurrency
=
-
1
;
}
}
void
AdaptiveMaxConcurrency
::
operator
=
(
int
max_concurrency
)
{
if
(
max_concurrency
<=
0
)
{
_value
=
UNLIMITED
();
_max_concurrency
=
0
;
}
else
{
_value
=
butil
::
string_printf
(
"%d"
,
max_concurrency
);
_max_concurrency
=
max_concurrency
;
}
}
const
std
::
string
&
AdaptiveMaxConcurrency
::
type
()
const
{
if
(
_max_concurrency
>
0
)
{
return
CONSTANT
();
}
else
if
(
_max_concurrency
==
0
)
{
return
UNLIMITED
();
}
else
{
return
_value
;
}
}
const
std
::
string
&
AdaptiveMaxConcurrency
::
UNLIMITED
()
{
static
std
::
string
*
s
=
new
std
::
string
(
"unlimited"
);
return
*
s
;
}
const
std
::
string
&
AdaptiveMaxConcurrency
::
CONSTANT
()
{
static
std
::
string
*
s
=
new
std
::
string
(
"constant"
);
return
*
s
;
}
bool
operator
==
(
const
AdaptiveMaxConcurrency
&
adaptive_concurrency
,
const
butil
::
StringPiece
&
concurrency
)
{
return
CompareStringPieceWithoutCase
(
concurrency
,
adaptive_concurrency
.
nam
e
().
c_str
());
adaptive_concurrency
.
valu
e
().
c_str
());
}
}
// namespace brpc
src/brpc/adaptive_max_concurrency.h
View file @
c9a2a08c
...
...
@@ -27,37 +27,45 @@ namespace brpc {
class
AdaptiveMaxConcurrency
{
public
:
AdaptiveMaxConcurrency
()
:
_name
(
"constant"
)
,
_max_concurrency
(
0
)
{}
AdaptiveMaxConcurrency
(
int
max_concurrency
)
:
_name
(
"constant"
)
,
_max_concurrency
(
max_concurrency
)
{}
AdaptiveMaxConcurrency
();
AdaptiveMaxConcurrency
(
int
max_concurrency
);
AdaptiveMaxConcurrency
(
const
butil
::
StringPiece
&
value
);
// Non-trivial destructor to prevent AdaptiveMaxConcurrency from being
// passed to variadic arguments without explicit type conversion.
// eg:
// printf("%d", options.max_concurrency) // compile error
// printf("%
d", static_cast<int>(options.max_concurrency
) // ok
// printf("%
s", options.max_concurrency.value().c_str()
) // ok
~
AdaptiveMaxConcurrency
()
{}
AdaptiveMaxConcurrency
(
const
butil
::
StringPiece
&
name
);
void
operator
=
(
int
max_concurrency
)
{
_name
=
"constant"
;
_max_concurrency
=
max_concurrency
;
}
void
operator
=
(
const
butil
::
StringPiece
&
name
);
void
operator
=
(
int
max_concurrency
);
void
operator
=
(
const
butil
::
StringPiece
&
value
);
// 0 for type="unlimited"
// >0 for type="constant"
// <0 for type="user-defined"
operator
int
()
const
{
return
_max_concurrency
;
}
const
std
::
string
&
name
()
const
{
return
_name
;
}
// "unlimited" for type="unlimited"
// "10" "20" "30" for type="constant"
// "user-defined" for type="user-defined"
const
std
::
string
&
value
()
const
{
return
_value
;
}
// "unlimited", "constant" or "user-defined"
const
std
::
string
&
type
()
const
;
// Get strings filled with "unlimited" and "constant"
static
const
std
::
string
&
UNLIMITED
();
static
const
std
::
string
&
CONSTANT
();
private
:
std
::
string
_
nam
e
;
std
::
string
_
valu
e
;
int
_max_concurrency
;
};
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
AdaptiveMaxConcurrency
&
amc
)
{
return
os
<<
amc
.
value
();
}
bool
operator
==
(
const
AdaptiveMaxConcurrency
&
adaptive_concurrency
,
const
butil
::
StringPiece
&
concurrency
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment