Skip to content

Commit c1b9ff7

Browse files
committed
combined RequestExecutingContext and CommandExecutingContext; changed to use thread local to reuse context objects instead of by pool
1 parent 13627bb commit c1b9ff7

File tree

3 files changed

+161
-56
lines changed

3 files changed

+161
-56
lines changed

SocketBase/AppServerBase.cs

+21-26
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ IBufferManager IAppServer.BufferManager
116116
get { return m_BufferManager; }
117117
}
118118

119-
private IPool<RequestExecutingContext<TAppSession, TPackageInfo>> m_RequestExecutingContextPool;
120-
121119
/// <summary>
122120
/// Gets or sets the receive filter factory.
123121
/// </summary>
@@ -1141,10 +1139,6 @@ public virtual bool Start()
11411139
ServerResourceItem.Create<BufferManagerResource, IBufferManager>(
11421140
(bufferManager) => this.m_BufferManager = bufferManager, Config));
11431141

1144-
transaction.RegisterItem(
1145-
ServerResourceItem.Create<RequestExecutingContextPoolResource<TAppSession, TPackageInfo>, IPool<RequestExecutingContext<TAppSession, TPackageInfo>>>(
1146-
(pool) => this.m_RequestExecutingContextPool = pool, Config));
1147-
11481142
if (!m_SocketServer.Start())
11491143
{
11501144
m_StateCode = ServerStateConst.NotStarted;
@@ -1331,10 +1325,12 @@ public virtual event RequestHandler<TAppSession, TPackageInfo> NewRequestReceive
13311325
/// <summary>
13321326
/// Executes the command.
13331327
/// </summary>
1334-
/// <param name="session">The session.</param>
1335-
/// <param name="requestInfo">The request info.</param>
1336-
protected virtual void ExecuteCommand(TAppSession session, TPackageInfo requestInfo)
1328+
/// <param name="context">The context.</param>
1329+
protected virtual void ExecuteCommand(IRequestExecutingContext<TAppSession, TPackageInfo> context)
13371330
{
1331+
var requestInfo = context.RequestInfo;
1332+
var session = context.Session;
1333+
13381334
if (m_RequestHandler == null)
13391335
{
13401336
var commandProxy = GetCommandByName(requestInfo.Key);
@@ -1354,8 +1350,9 @@ protected virtual void ExecuteCommand(TAppSession session, TPackageInfo requestI
13541350
}
13551351
else
13561352
{
1357-
var commandContext = new CommandExecutingContext();
1358-
commandContext.Initialize(session, requestInfo, command);
1353+
var commandContext = context as RequestExecutingContext<TAppSession, TPackageInfo>;
1354+
1355+
commandContext.CurrentCommand = command;
13591356

13601357
for (var i = 0; i < commandFilters.Length; i++)
13611358
{
@@ -1447,42 +1444,40 @@ protected virtual void ExecuteCommand(TAppSession session, TPackageInfo requestI
14471444
/// <param name="requestInfo">The request info.</param>
14481445
internal void ExecuteCommand(IAppSession session, TPackageInfo requestInfo)
14491446
{
1450-
var context = m_RequestExecutingContextPool.Get();
1447+
//var context = m_RequestExecutingContextPool.Get();
1448+
var context = RequestExecutingContext<TAppSession, TPackageInfo>.GetFromThreadContext();
1449+
14511450
context.Initialize((TAppSession)session, requestInfo);
14521451

14531452
if (m_RequestHandlingTaskScheduler == null)
14541453
{
1455-
try
1456-
{
1457-
ExecuteCommandInTask(context);
1458-
}
1459-
finally
1460-
{
1461-
TryReturnBufferedPackageInfo(context);
1462-
m_RequestExecutingContextPool.Return(context);
1463-
}
1454+
ExecuteCommandDirect(context);
14641455
}
14651456
else
14661457
{
14671458
Task.Factory.StartNew(ExecuteCommandInTask, context, CancellationToken.None, TaskCreationOptions.None, m_RequestHandlingTaskScheduler);
14681459
}
14691460
}
14701461

1471-
void ExecuteCommandInTask(object state)
1462+
void ExecuteCommandDirect(RequestExecutingContext<TAppSession, TPackageInfo> context)
14721463
{
1473-
var context = state as RequestExecutingContext<TAppSession, TPackageInfo>;
1474-
14751464
try
14761465
{
1477-
this.ExecuteCommand(context.Session, context.RequestInfo);
1466+
this.ExecuteCommand(context);
14781467
}
14791468
finally
14801469
{
14811470
TryReturnBufferedPackageInfo(context);
1482-
m_RequestExecutingContextPool.Return(context);
1471+
context.Reset();
14831472
}
14841473
}
14851474

1475+
void ExecuteCommandInTask(object state)
1476+
{
1477+
var context = state as RequestExecutingContext<TAppSession, TPackageInfo>;
1478+
ExecuteCommandDirect(context);
1479+
}
1480+
14861481
bool TryReturnBufferedPackageInfo(RequestExecutingContext<TAppSession, TPackageInfo> context)
14871482
{
14881483
var request = context.RequestInfo as IBufferedPackageInfo;

SocketBase/CommandExecutingContext.cs

+138-28
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,126 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Threading;
56
using SuperSocket.ProtoBase;
67
using SuperSocket.SocketBase.Command;
78
using SuperSocket.SocketBase.Pool;
89
using SuperSocket.SocketBase.Protocol;
910

11+
1012
namespace SuperSocket.SocketBase
1113
{
1214
/// <summary>
13-
/// Command Executing Context
15+
/// The interface for request executing context
1416
/// </summary>
15-
public class CommandExecutingContext
17+
/// <typeparam name="TAppSession">The type of the application session.</typeparam>
18+
/// <typeparam name="TPackageInfo">The type of the package information.</typeparam>
19+
public interface IRequestExecutingContext<TAppSession, TPackageInfo>
20+
where TPackageInfo : IPackageInfo
21+
where TAppSession : IAppSession, IThreadExecutingContext, IAppSession<TAppSession, TPackageInfo>, new()
1622
{
23+
1724
/// <summary>
18-
/// Gets the session.
25+
/// Gets the current session.
1926
/// </summary>
20-
public IAppSession Session { get; private set; }
27+
/// <value>
28+
/// The session.
29+
/// </value>
30+
TAppSession Session { get; }
31+
32+
/// <summary>
33+
/// Gets the current request.
34+
/// </summary>
35+
/// <value>
36+
/// The request information.
37+
/// </value>
38+
TPackageInfo RequestInfo { get; }
39+
}
2140

41+
/// <summary>
42+
/// The interface for command excuting context
43+
/// </summary>
44+
public interface ICommandExecutingContext
45+
{
2246
/// <summary>
23-
/// Gets the request info.
47+
/// Gets the session which is executing the command.
2448
/// </summary>
25-
public IPackageInfo Request { get; private set; }
49+
/// <value>
50+
/// The session.
51+
/// </value>
52+
IAppSession Session { get; }
2653

2754
/// <summary>
28-
/// Gets the current command.
55+
/// Gets the request which is being processed.
2956
/// </summary>
30-
public ICommand CurrentCommand { get; private set; }
57+
/// <value>
58+
/// The request.
59+
/// </value>
60+
IPackageInfo Request { get; }
61+
3162

3263
/// <summary>
33-
/// Gets the exception.
64+
/// Gets the current command which is being executed.
65+
/// </summary>
66+
/// <value>
67+
/// The current command.
68+
/// </value>
69+
ICommand CurrentCommand { get; }
70+
71+
72+
/// <summary>
73+
/// Gets the exception which was thrown in the command execution.
3474
/// </summary>
3575
/// <value>
3676
/// The exception.
3777
/// </value>
38-
public Exception Exception { get; internal set; }
78+
Exception Exception { get; }
3979

4080
/// <summary>
4181
/// Gets a value indicating whether [exception handled].
4282
/// </summary>
4383
/// <value>
4484
/// <c>true</c> if [exception handled]; otherwise, <c>false</c>.
4585
/// </value>
46-
public bool ExceptionHandled { get; internal set; }
86+
bool ExceptionHandled { get; set; }
87+
4788

4889
/// <summary>
49-
/// Gets or sets a value indicating whether this command executing is cancelled.
90+
/// Gets or sets a value indicating whether the command exeuction is cancelled.
5091
/// </summary>
5192
/// <value>
5293
/// <c>true</c> if cancel; otherwise, <c>false</c>.
5394
/// </value>
54-
public bool Cancel { get; set; }
55-
56-
/// <summary>
57-
/// Initializes a new instance of the <see cref="CommandExecutingContext" /> class.
58-
/// </summary>
59-
/// <param name="session">The session.</param>
60-
/// <param name="requestInfo">The request info.</param>
61-
/// <param name="command">The command.</param>
62-
public void Initialize(IAppSession session, IPackageInfo requestInfo, ICommand command)
63-
{
64-
Session = session;
65-
Request = requestInfo;
66-
CurrentCommand = command;
67-
}
95+
bool Cancel { get; set; }
6896
}
6997

70-
interface IThreadExecutingContext
98+
/// <summary>
99+
/// The interface for thread execution context
100+
/// </summary>
101+
public interface IThreadExecutingContext
71102
{
103+
/// <summary>
104+
/// Increments the queued request count.
105+
/// </summary>
106+
/// <param name="value">The value.</param>
72107
void Increment(int value);
73108

109+
/// <summary>
110+
/// Decrements the queued request count.
111+
/// </summary>
112+
/// <param name="value">The value.</param>
74113
void Decrement(int value);
75114

115+
/// <summary>
116+
/// Gets or sets the prefered thread identifier.
117+
/// </summary>
118+
/// <value>
119+
/// The prefered thread identifier.
120+
/// </value>
76121
int PreferedThreadId { get; set; }
77122
}
78123

79-
class RequestExecutingContext<TAppSession, TPackageInfo> : IThreadExecutingContext
124+
class RequestExecutingContext<TAppSession, TPackageInfo> : IRequestExecutingContext<TAppSession, TPackageInfo>, IThreadExecutingContext, ICommandExecutingContext
80125
where TPackageInfo : IPackageInfo
81126
where TAppSession : IAppSession, IThreadExecutingContext, IAppSession<TAppSession, TPackageInfo>, new()
82127
{
@@ -126,6 +171,71 @@ public void Reset()
126171
{
127172
Session = default(TAppSession);
128173
RequestInfo = default(TPackageInfo);
174+
CurrentCommand = null;
175+
Exception = null;
176+
ExceptionHandled = false;
177+
Cancel = false;
178+
}
179+
180+
IAppSession ICommandExecutingContext.Session
181+
{
182+
get { return Session; }
183+
}
184+
185+
IPackageInfo ICommandExecutingContext.Request
186+
{
187+
get { return RequestInfo; }
188+
}
189+
190+
/// <summary>
191+
/// Gets the current command which is being executed.
192+
/// </summary>
193+
/// <value>
194+
/// The current command.
195+
/// </value>
196+
public ICommand CurrentCommand { get; internal set; }
197+
198+
/// <summary>
199+
/// Gets the exception which was thrown in the command execution.
200+
/// </summary>
201+
/// <value>
202+
/// The exception.
203+
/// </value>
204+
public Exception Exception { get; internal set; }
205+
206+
/// <summary>
207+
/// Gets a value indicating whether [exception handled].
208+
/// </summary>
209+
/// <value>
210+
/// <c>true</c> if [exception handled]; otherwise, <c>false</c>.
211+
/// </value>
212+
public bool ExceptionHandled { get; set; }
213+
214+
/// <summary>
215+
/// Gets or sets a value indicating whether the command exeuction is cancelled.
216+
/// </summary>
217+
/// <value>
218+
/// <c>true</c> if cancel; otherwise, <c>false</c>.
219+
/// </value>
220+
public bool Cancel { get; set; }
221+
222+
private const string m_SlotName = "REC"; //RequestExecutingContext
223+
224+
public static RequestExecutingContext<TAppSession, TPackageInfo> GetFromThreadContext()
225+
{
226+
var slot = Thread.GetNamedDataSlot(m_SlotName);
227+
228+
// look for existing context instance from thread local at first
229+
var context = Thread.GetData(slot) as RequestExecutingContext<TAppSession, TPackageInfo>;
230+
231+
if (context != null)
232+
return context;
233+
234+
// create a new context instance and then store it into thread local
235+
context = new RequestExecutingContext<TAppSession, TPackageInfo>();
236+
Thread.SetData(slot, context);
237+
238+
return context;
129239
}
130240
}
131241
}

SocketBase/Metadata/CommandFilterAttribute.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public abstract class CommandFilterAttribute : Attribute
2222
/// Called when [command executing].
2323
/// </summary>
2424
/// <param name="commandContext">The command context.</param>
25-
public abstract void OnCommandExecuting(CommandExecutingContext commandContext);
25+
public abstract void OnCommandExecuting(ICommandExecutingContext commandContext);
2626

2727
/// <summary>
2828
/// Called when [command executed].
2929
/// </summary>
3030
/// <param name="commandContext">The command context.</param>
31-
public abstract void OnCommandExecuted(CommandExecutingContext commandContext);
31+
public abstract void OnCommandExecuted(ICommandExecutingContext commandContext);
3232
}
3333
}
3434

0 commit comments

Comments
 (0)