repo stringlengths 5 67 | path stringlengths 4 218 | func_name stringlengths 0 151 | original_string stringlengths 52 373k | language stringclasses 6 values | code stringlengths 52 373k | code_tokens listlengths 10 512 | docstring stringlengths 3 47.2k | docstring_tokens listlengths 3 234 | sha stringlengths 40 40 | url stringlengths 85 339 | partition stringclasses 3 values |
|---|---|---|---|---|---|---|---|---|---|---|---|
topfreegames/pitaya | serialize/mocks/serializer.go | GetName | func (mr *MockSerializerMockRecorder) GetName() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetName", reflect.TypeOf((*MockSerializer)(nil).GetName))
} | go | func (mr *MockSerializerMockRecorder) GetName() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetName", reflect.TypeOf((*MockSerializer)(nil).GetName))
} | [
"func",
"(",
"mr",
"*",
"MockSerializerMockRecorder",
")",
"GetName",
"(",
")",
"*",
"gomock",
".",
"Call",
"{",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"GetName\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockSerializer",
")",
"(",
"nil",
")",
".",
"GetName",
")",
")",
"\n",
"}"
] | // GetName indicates an expected call of GetName | [
"GetName",
"indicates",
"an",
"expected",
"call",
"of",
"GetName"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/serialize/mocks/serializer.go#L43-L45 | train |
topfreegames/pitaya | serialize/mocks/serializer.go | Marshal | func (m *MockSerializer) Marshal(arg0 interface{}) ([]byte, error) {
ret := m.ctrl.Call(m, "Marshal", arg0)
ret0, _ := ret[0].([]byte)
ret1, _ := ret[1].(error)
return ret0, ret1
} | go | func (m *MockSerializer) Marshal(arg0 interface{}) ([]byte, error) {
ret := m.ctrl.Call(m, "Marshal", arg0)
ret0, _ := ret[0].([]byte)
ret1, _ := ret[1].(error)
return ret0, ret1
} | [
"func",
"(",
"m",
"*",
"MockSerializer",
")",
"Marshal",
"(",
"arg0",
"interface",
"{",
"}",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"Marshal\"",
",",
"arg0",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"[",
"]",
"byte",
")",
"\n",
"ret1",
",",
"_",
":=",
"ret",
"[",
"1",
"]",
".",
"(",
"error",
")",
"\n",
"return",
"ret0",
",",
"ret1",
"\n",
"}"
] | // Marshal mocks base method | [
"Marshal",
"mocks",
"base",
"method"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/serialize/mocks/serializer.go#L48-L53 | train |
topfreegames/pitaya | serialize/mocks/serializer.go | Unmarshal | func (m *MockSerializer) Unmarshal(arg0 []byte, arg1 interface{}) error {
ret := m.ctrl.Call(m, "Unmarshal", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
} | go | func (m *MockSerializer) Unmarshal(arg0 []byte, arg1 interface{}) error {
ret := m.ctrl.Call(m, "Unmarshal", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
} | [
"func",
"(",
"m",
"*",
"MockSerializer",
")",
"Unmarshal",
"(",
"arg0",
"[",
"]",
"byte",
",",
"arg1",
"interface",
"{",
"}",
")",
"error",
"{",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"Unmarshal\"",
",",
"arg0",
",",
"arg1",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"error",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] | // Unmarshal mocks base method | [
"Unmarshal",
"mocks",
"base",
"method"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/serialize/mocks/serializer.go#L61-L65 | train |
topfreegames/pitaya | serialize/mocks/serializer.go | Unmarshal | func (mr *MockSerializerMockRecorder) Unmarshal(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Unmarshal", reflect.TypeOf((*MockSerializer)(nil).Unmarshal), arg0, arg1)
} | go | func (mr *MockSerializerMockRecorder) Unmarshal(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Unmarshal", reflect.TypeOf((*MockSerializer)(nil).Unmarshal), arg0, arg1)
} | [
"func",
"(",
"mr",
"*",
"MockSerializerMockRecorder",
")",
"Unmarshal",
"(",
"arg0",
",",
"arg1",
"interface",
"{",
"}",
")",
"*",
"gomock",
".",
"Call",
"{",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"Unmarshal\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockSerializer",
")",
"(",
"nil",
")",
".",
"Unmarshal",
")",
",",
"arg0",
",",
"arg1",
")",
"\n",
"}"
] | // Unmarshal indicates an expected call of Unmarshal | [
"Unmarshal",
"indicates",
"an",
"expected",
"call",
"of",
"Unmarshal"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/serialize/mocks/serializer.go#L68-L70 | train |
topfreegames/pitaya | docgenerator/generator.go | HandlersDocs | func HandlersDocs(serverType string, services map[string]*component.Service, getPtrNames bool) (map[string]interface{}, error) {
docs := &docs{
Handlers: map[string]*doc{},
}
for serviceName, service := range services {
for name, handler := range service.Handlers {
routeName := route.NewRoute(serverType, serviceName, name)
docs.Handlers[routeName.String()] = docForMethod(handler.Method, getPtrNames)
}
}
return docs.Handlers.toMap()
} | go | func HandlersDocs(serverType string, services map[string]*component.Service, getPtrNames bool) (map[string]interface{}, error) {
docs := &docs{
Handlers: map[string]*doc{},
}
for serviceName, service := range services {
for name, handler := range service.Handlers {
routeName := route.NewRoute(serverType, serviceName, name)
docs.Handlers[routeName.String()] = docForMethod(handler.Method, getPtrNames)
}
}
return docs.Handlers.toMap()
} | [
"func",
"HandlersDocs",
"(",
"serverType",
"string",
",",
"services",
"map",
"[",
"string",
"]",
"*",
"component",
".",
"Service",
",",
"getPtrNames",
"bool",
")",
"(",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"docs",
":=",
"&",
"docs",
"{",
"Handlers",
":",
"map",
"[",
"string",
"]",
"*",
"doc",
"{",
"}",
",",
"}",
"\n",
"for",
"serviceName",
",",
"service",
":=",
"range",
"services",
"{",
"for",
"name",
",",
"handler",
":=",
"range",
"service",
".",
"Handlers",
"{",
"routeName",
":=",
"route",
".",
"NewRoute",
"(",
"serverType",
",",
"serviceName",
",",
"name",
")",
"\n",
"docs",
".",
"Handlers",
"[",
"routeName",
".",
"String",
"(",
")",
"]",
"=",
"docForMethod",
"(",
"handler",
".",
"Method",
",",
"getPtrNames",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"docs",
".",
"Handlers",
".",
"toMap",
"(",
")",
"\n",
"}"
] | // HandlersDocs returns a map from route to input and output | [
"HandlersDocs",
"returns",
"a",
"map",
"from",
"route",
"to",
"input",
"and",
"output"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/docgenerator/generator.go#L46-L59 | train |
topfreegames/pitaya | docgenerator/generator.go | RemotesDocs | func RemotesDocs(serverType string, services map[string]*component.Service, getPtrNames bool) (map[string]interface{}, error) {
docs := &docs{
Remotes: map[string]*doc{},
}
for serviceName, service := range services {
for name, remote := range service.Remotes {
routeName := route.NewRoute(serverType, serviceName, name)
docs.Remotes[routeName.String()] = docForMethod(remote.Method, getPtrNames)
}
}
return docs.Remotes.toMap()
} | go | func RemotesDocs(serverType string, services map[string]*component.Service, getPtrNames bool) (map[string]interface{}, error) {
docs := &docs{
Remotes: map[string]*doc{},
}
for serviceName, service := range services {
for name, remote := range service.Remotes {
routeName := route.NewRoute(serverType, serviceName, name)
docs.Remotes[routeName.String()] = docForMethod(remote.Method, getPtrNames)
}
}
return docs.Remotes.toMap()
} | [
"func",
"RemotesDocs",
"(",
"serverType",
"string",
",",
"services",
"map",
"[",
"string",
"]",
"*",
"component",
".",
"Service",
",",
"getPtrNames",
"bool",
")",
"(",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"docs",
":=",
"&",
"docs",
"{",
"Remotes",
":",
"map",
"[",
"string",
"]",
"*",
"doc",
"{",
"}",
",",
"}",
"\n",
"for",
"serviceName",
",",
"service",
":=",
"range",
"services",
"{",
"for",
"name",
",",
"remote",
":=",
"range",
"service",
".",
"Remotes",
"{",
"routeName",
":=",
"route",
".",
"NewRoute",
"(",
"serverType",
",",
"serviceName",
",",
"name",
")",
"\n",
"docs",
".",
"Remotes",
"[",
"routeName",
".",
"String",
"(",
")",
"]",
"=",
"docForMethod",
"(",
"remote",
".",
"Method",
",",
"getPtrNames",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"docs",
".",
"Remotes",
".",
"toMap",
"(",
")",
"\n",
"}"
] | // RemotesDocs returns a map from route to input and output | [
"RemotesDocs",
"returns",
"a",
"map",
"from",
"route",
"to",
"input",
"and",
"output"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/docgenerator/generator.go#L62-L75 | train |
topfreegames/pitaya | conn/message/message.go | New | func New(err ...bool) *Message {
m := &Message{}
if len(err) > 0 {
m.Err = err[0]
}
return m
} | go | func New(err ...bool) *Message {
m := &Message{}
if len(err) > 0 {
m.Err = err[0]
}
return m
} | [
"func",
"New",
"(",
"err",
"...",
"bool",
")",
"*",
"Message",
"{",
"m",
":=",
"&",
"Message",
"{",
"}",
"\n",
"if",
"len",
"(",
"err",
")",
">",
"0",
"{",
"m",
".",
"Err",
"=",
"err",
"[",
"0",
"]",
"\n",
"}",
"\n",
"return",
"m",
"\n",
"}"
] | // New returns a new message instance | [
"New",
"returns",
"a",
"new",
"message",
"instance"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/conn/message/message.go#L79-L85 | train |
topfreegames/pitaya | conn/message/message.go | String | func (m *Message) String() string {
return fmt.Sprintf("Type: %s, ID: %d, Route: %s, Compressed: %t, Error: %t, Data: %v, BodyLength: %d",
types[m.Type],
m.ID,
m.Route,
m.compressed,
m.Err,
m.Data,
len(m.Data))
} | go | func (m *Message) String() string {
return fmt.Sprintf("Type: %s, ID: %d, Route: %s, Compressed: %t, Error: %t, Data: %v, BodyLength: %d",
types[m.Type],
m.ID,
m.Route,
m.compressed,
m.Err,
m.Data,
len(m.Data))
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"String",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"Type: %s, ID: %d, Route: %s, Compressed: %t, Error: %t, Data: %v, BodyLength: %d\"",
",",
"types",
"[",
"m",
".",
"Type",
"]",
",",
"m",
".",
"ID",
",",
"m",
".",
"Route",
",",
"m",
".",
"compressed",
",",
"m",
".",
"Err",
",",
"m",
".",
"Data",
",",
"len",
"(",
"m",
".",
"Data",
")",
")",
"\n",
"}"
] | // String, implementation of fmt.Stringer interface | [
"String",
"implementation",
"of",
"fmt",
".",
"Stringer",
"interface"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/conn/message/message.go#L88-L97 | train |
topfreegames/pitaya | conn/message/message.go | SetDictionary | func SetDictionary(dict map[string]uint16) error {
if dict == nil {
return nil
}
for route, code := range dict {
r := strings.TrimSpace(route)
// duplication check
if _, ok := routes[r]; ok {
return fmt.Errorf("duplicated route(route: %s, code: %d)", r, code)
}
if _, ok := codes[code]; ok {
return fmt.Errorf("duplicated route(route: %s, code: %d)", r, code)
}
// update map, using last value when key duplicated
routes[r] = code
codes[code] = r
}
return nil
} | go | func SetDictionary(dict map[string]uint16) error {
if dict == nil {
return nil
}
for route, code := range dict {
r := strings.TrimSpace(route)
// duplication check
if _, ok := routes[r]; ok {
return fmt.Errorf("duplicated route(route: %s, code: %d)", r, code)
}
if _, ok := codes[code]; ok {
return fmt.Errorf("duplicated route(route: %s, code: %d)", r, code)
}
// update map, using last value when key duplicated
routes[r] = code
codes[code] = r
}
return nil
} | [
"func",
"SetDictionary",
"(",
"dict",
"map",
"[",
"string",
"]",
"uint16",
")",
"error",
"{",
"if",
"dict",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"for",
"route",
",",
"code",
":=",
"range",
"dict",
"{",
"r",
":=",
"strings",
".",
"TrimSpace",
"(",
"route",
")",
"\n",
"if",
"_",
",",
"ok",
":=",
"routes",
"[",
"r",
"]",
";",
"ok",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"duplicated route(route: %s, code: %d)\"",
",",
"r",
",",
"code",
")",
"\n",
"}",
"\n",
"if",
"_",
",",
"ok",
":=",
"codes",
"[",
"code",
"]",
";",
"ok",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"duplicated route(route: %s, code: %d)\"",
",",
"r",
",",
"code",
")",
"\n",
"}",
"\n",
"routes",
"[",
"r",
"]",
"=",
"code",
"\n",
"codes",
"[",
"code",
"]",
"=",
"r",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // SetDictionary set routes map which be used to compress route. | [
"SetDictionary",
"set",
"routes",
"map",
"which",
"be",
"used",
"to",
"compress",
"route",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/conn/message/message.go#L109-L132 | train |
topfreegames/pitaya | conn/packet/packet.go | String | func (p *Packet) String() string {
return fmt.Sprintf("Type: %d, Length: %d, Data: %s", p.Type, p.Length, string(p.Data))
} | go | func (p *Packet) String() string {
return fmt.Sprintf("Type: %d, Length: %d, Data: %s", p.Type, p.Length, string(p.Data))
} | [
"func",
"(",
"p",
"*",
"Packet",
")",
"String",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"Type: %d, Length: %d, Data: %s\"",
",",
"p",
".",
"Type",
",",
"p",
".",
"Length",
",",
"string",
"(",
"p",
".",
"Data",
")",
")",
"\n",
"}"
] | //String represents the Packet's in text mode. | [
"String",
"represents",
"the",
"Packet",
"s",
"in",
"text",
"mode",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/conn/packet/packet.go#L40-L42 | train |
topfreegames/pitaya | metrics/mocks/reporter.go | NewMockReporter | func NewMockReporter(ctrl *gomock.Controller) *MockReporter {
mock := &MockReporter{ctrl: ctrl}
mock.recorder = &MockReporterMockRecorder{mock}
return mock
} | go | func NewMockReporter(ctrl *gomock.Controller) *MockReporter {
mock := &MockReporter{ctrl: ctrl}
mock.recorder = &MockReporterMockRecorder{mock}
return mock
} | [
"func",
"NewMockReporter",
"(",
"ctrl",
"*",
"gomock",
".",
"Controller",
")",
"*",
"MockReporter",
"{",
"mock",
":=",
"&",
"MockReporter",
"{",
"ctrl",
":",
"ctrl",
"}",
"\n",
"mock",
".",
"recorder",
"=",
"&",
"MockReporterMockRecorder",
"{",
"mock",
"}",
"\n",
"return",
"mock",
"\n",
"}"
] | // NewMockReporter creates a new mock instance | [
"NewMockReporter",
"creates",
"a",
"new",
"mock",
"instance"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/metrics/mocks/reporter.go#L25-L29 | train |
topfreegames/pitaya | metrics/mocks/reporter.go | ReportCount | func (m *MockReporter) ReportCount(metric string, tags map[string]string, count float64) error {
ret := m.ctrl.Call(m, "ReportCount", metric, tags, count)
ret0, _ := ret[0].(error)
return ret0
} | go | func (m *MockReporter) ReportCount(metric string, tags map[string]string, count float64) error {
ret := m.ctrl.Call(m, "ReportCount", metric, tags, count)
ret0, _ := ret[0].(error)
return ret0
} | [
"func",
"(",
"m",
"*",
"MockReporter",
")",
"ReportCount",
"(",
"metric",
"string",
",",
"tags",
"map",
"[",
"string",
"]",
"string",
",",
"count",
"float64",
")",
"error",
"{",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"ReportCount\"",
",",
"metric",
",",
"tags",
",",
"count",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"error",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] | // ReportCount mocks base method | [
"ReportCount",
"mocks",
"base",
"method"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/metrics/mocks/reporter.go#L37-L41 | train |
topfreegames/pitaya | metrics/mocks/reporter.go | ReportCount | func (mr *MockReporterMockRecorder) ReportCount(metric, tags, count interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportCount", reflect.TypeOf((*MockReporter)(nil).ReportCount), metric, tags, count)
} | go | func (mr *MockReporterMockRecorder) ReportCount(metric, tags, count interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportCount", reflect.TypeOf((*MockReporter)(nil).ReportCount), metric, tags, count)
} | [
"func",
"(",
"mr",
"*",
"MockReporterMockRecorder",
")",
"ReportCount",
"(",
"metric",
",",
"tags",
",",
"count",
"interface",
"{",
"}",
")",
"*",
"gomock",
".",
"Call",
"{",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"ReportCount\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockReporter",
")",
"(",
"nil",
")",
".",
"ReportCount",
")",
",",
"metric",
",",
"tags",
",",
"count",
")",
"\n",
"}"
] | // ReportCount indicates an expected call of ReportCount | [
"ReportCount",
"indicates",
"an",
"expected",
"call",
"of",
"ReportCount"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/metrics/mocks/reporter.go#L44-L46 | train |
topfreegames/pitaya | metrics/mocks/reporter.go | ReportSummary | func (m *MockReporter) ReportSummary(metric string, tags map[string]string, value float64) error {
ret := m.ctrl.Call(m, "ReportSummary", metric, tags, value)
ret0, _ := ret[0].(error)
return ret0
} | go | func (m *MockReporter) ReportSummary(metric string, tags map[string]string, value float64) error {
ret := m.ctrl.Call(m, "ReportSummary", metric, tags, value)
ret0, _ := ret[0].(error)
return ret0
} | [
"func",
"(",
"m",
"*",
"MockReporter",
")",
"ReportSummary",
"(",
"metric",
"string",
",",
"tags",
"map",
"[",
"string",
"]",
"string",
",",
"value",
"float64",
")",
"error",
"{",
"ret",
":=",
"m",
".",
"ctrl",
".",
"Call",
"(",
"m",
",",
"\"ReportSummary\"",
",",
"metric",
",",
"tags",
",",
"value",
")",
"\n",
"ret0",
",",
"_",
":=",
"ret",
"[",
"0",
"]",
".",
"(",
"error",
")",
"\n",
"return",
"ret0",
"\n",
"}"
] | // ReportSummary mocks base method | [
"ReportSummary",
"mocks",
"base",
"method"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/metrics/mocks/reporter.go#L49-L53 | train |
topfreegames/pitaya | metrics/mocks/reporter.go | ReportSummary | func (mr *MockReporterMockRecorder) ReportSummary(metric, tags, value interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportSummary", reflect.TypeOf((*MockReporter)(nil).ReportSummary), metric, tags, value)
} | go | func (mr *MockReporterMockRecorder) ReportSummary(metric, tags, value interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportSummary", reflect.TypeOf((*MockReporter)(nil).ReportSummary), metric, tags, value)
} | [
"func",
"(",
"mr",
"*",
"MockReporterMockRecorder",
")",
"ReportSummary",
"(",
"metric",
",",
"tags",
",",
"value",
"interface",
"{",
"}",
")",
"*",
"gomock",
".",
"Call",
"{",
"return",
"mr",
".",
"mock",
".",
"ctrl",
".",
"RecordCallWithMethodType",
"(",
"mr",
".",
"mock",
",",
"\"ReportSummary\"",
",",
"reflect",
".",
"TypeOf",
"(",
"(",
"*",
"MockReporter",
")",
"(",
"nil",
")",
".",
"ReportSummary",
")",
",",
"metric",
",",
"tags",
",",
"value",
")",
"\n",
"}"
] | // ReportSummary indicates an expected call of ReportSummary | [
"ReportSummary",
"indicates",
"an",
"expected",
"call",
"of",
"ReportSummary"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/metrics/mocks/reporter.go#L56-L58 | train |
topfreegames/pitaya | tracing/span.go | InjectSpan | func InjectSpan(ctx context.Context) (context.Context, error) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return ctx, nil
}
spanData := opentracing.TextMapCarrier{}
tracer := opentracing.GlobalTracer()
err := tracer.Inject(span.Context(), opentracing.TextMap, spanData)
if err != nil {
return nil, err
}
return pcontext.AddToPropagateCtx(ctx, constants.SpanPropagateCtxKey, spanData), nil
} | go | func InjectSpan(ctx context.Context) (context.Context, error) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return ctx, nil
}
spanData := opentracing.TextMapCarrier{}
tracer := opentracing.GlobalTracer()
err := tracer.Inject(span.Context(), opentracing.TextMap, spanData)
if err != nil {
return nil, err
}
return pcontext.AddToPropagateCtx(ctx, constants.SpanPropagateCtxKey, spanData), nil
} | [
"func",
"InjectSpan",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"context",
".",
"Context",
",",
"error",
")",
"{",
"span",
":=",
"opentracing",
".",
"SpanFromContext",
"(",
"ctx",
")",
"\n",
"if",
"span",
"==",
"nil",
"{",
"return",
"ctx",
",",
"nil",
"\n",
"}",
"\n",
"spanData",
":=",
"opentracing",
".",
"TextMapCarrier",
"{",
"}",
"\n",
"tracer",
":=",
"opentracing",
".",
"GlobalTracer",
"(",
")",
"\n",
"err",
":=",
"tracer",
".",
"Inject",
"(",
"span",
".",
"Context",
"(",
")",
",",
"opentracing",
".",
"TextMap",
",",
"spanData",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"pcontext",
".",
"AddToPropagateCtx",
"(",
"ctx",
",",
"constants",
".",
"SpanPropagateCtxKey",
",",
"spanData",
")",
",",
"nil",
"\n",
"}"
] | // InjectSpan retrieves an opentrancing span from the current context and creates a new context
// with it encoded in binary format inside the propagatable context content | [
"InjectSpan",
"retrieves",
"an",
"opentrancing",
"span",
"from",
"the",
"current",
"context",
"and",
"creates",
"a",
"new",
"context",
"with",
"it",
"encoded",
"in",
"binary",
"format",
"inside",
"the",
"propagatable",
"context",
"content"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/tracing/span.go#L79-L91 | train |
topfreegames/pitaya | tracing/span.go | StartSpan | func StartSpan(
parentCtx context.Context,
opName string,
tags opentracing.Tags,
reference ...opentracing.SpanContext,
) context.Context {
var ref opentracing.SpanContext
if len(reference) > 0 {
ref = reference[0]
}
span := opentracing.StartSpan(opName, opentracing.ChildOf(ref), tags)
return opentracing.ContextWithSpan(parentCtx, span)
} | go | func StartSpan(
parentCtx context.Context,
opName string,
tags opentracing.Tags,
reference ...opentracing.SpanContext,
) context.Context {
var ref opentracing.SpanContext
if len(reference) > 0 {
ref = reference[0]
}
span := opentracing.StartSpan(opName, opentracing.ChildOf(ref), tags)
return opentracing.ContextWithSpan(parentCtx, span)
} | [
"func",
"StartSpan",
"(",
"parentCtx",
"context",
".",
"Context",
",",
"opName",
"string",
",",
"tags",
"opentracing",
".",
"Tags",
",",
"reference",
"...",
"opentracing",
".",
"SpanContext",
",",
")",
"context",
".",
"Context",
"{",
"var",
"ref",
"opentracing",
".",
"SpanContext",
"\n",
"if",
"len",
"(",
"reference",
")",
">",
"0",
"{",
"ref",
"=",
"reference",
"[",
"0",
"]",
"\n",
"}",
"\n",
"span",
":=",
"opentracing",
".",
"StartSpan",
"(",
"opName",
",",
"opentracing",
".",
"ChildOf",
"(",
"ref",
")",
",",
"tags",
")",
"\n",
"return",
"opentracing",
".",
"ContextWithSpan",
"(",
"parentCtx",
",",
"span",
")",
"\n",
"}"
] | // StartSpan starts a new span with a given parent context, operation name, tags and
// optional parent span. It returns a context with the created span. | [
"StartSpan",
"starts",
"a",
"new",
"span",
"with",
"a",
"given",
"parent",
"context",
"operation",
"name",
"tags",
"and",
"optional",
"parent",
"span",
".",
"It",
"returns",
"a",
"context",
"with",
"the",
"created",
"span",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/tracing/span.go#L95-L107 | train |
topfreegames/pitaya | tracing/span.go | FinishSpan | func FinishSpan(ctx context.Context, err error) {
if ctx == nil {
return
}
span := opentracing.SpanFromContext(ctx)
if span == nil {
return
}
defer span.Finish()
if err != nil {
LogError(span, err.Error())
}
} | go | func FinishSpan(ctx context.Context, err error) {
if ctx == nil {
return
}
span := opentracing.SpanFromContext(ctx)
if span == nil {
return
}
defer span.Finish()
if err != nil {
LogError(span, err.Error())
}
} | [
"func",
"FinishSpan",
"(",
"ctx",
"context",
".",
"Context",
",",
"err",
"error",
")",
"{",
"if",
"ctx",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"span",
":=",
"opentracing",
".",
"SpanFromContext",
"(",
"ctx",
")",
"\n",
"if",
"span",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"defer",
"span",
".",
"Finish",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"LogError",
"(",
"span",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"}"
] | // FinishSpan finishes a span retrieved from the given context and logs the error if it exists | [
"FinishSpan",
"finishes",
"a",
"span",
"retrieved",
"from",
"the",
"given",
"context",
"and",
"logs",
"the",
"error",
"if",
"it",
"exists"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/tracing/span.go#L110-L122 | train |
topfreegames/pitaya | router/router.go | Route | func (r *Router) Route(
ctx context.Context,
rpcType protos.RPCType,
svType string,
route *route.Route,
msg *message.Message,
) (*cluster.Server, error) {
if r.serviceDiscovery == nil {
return nil, constants.ErrServiceDiscoveryNotInitialized
}
serversOfType, err := r.serviceDiscovery.GetServersByType(svType)
if err != nil {
return nil, err
}
if rpcType == protos.RPCType_User {
server := r.defaultRoute(serversOfType)
return server, nil
}
routeFunc, ok := r.routesMap[svType]
if !ok {
logger.Log.Debugf("no specific route for svType: %s, using default route", svType)
server := r.defaultRoute(serversOfType)
return server, nil
}
return routeFunc(ctx, route, msg.Data, serversOfType)
} | go | func (r *Router) Route(
ctx context.Context,
rpcType protos.RPCType,
svType string,
route *route.Route,
msg *message.Message,
) (*cluster.Server, error) {
if r.serviceDiscovery == nil {
return nil, constants.ErrServiceDiscoveryNotInitialized
}
serversOfType, err := r.serviceDiscovery.GetServersByType(svType)
if err != nil {
return nil, err
}
if rpcType == protos.RPCType_User {
server := r.defaultRoute(serversOfType)
return server, nil
}
routeFunc, ok := r.routesMap[svType]
if !ok {
logger.Log.Debugf("no specific route for svType: %s, using default route", svType)
server := r.defaultRoute(serversOfType)
return server, nil
}
return routeFunc(ctx, route, msg.Data, serversOfType)
} | [
"func",
"(",
"r",
"*",
"Router",
")",
"Route",
"(",
"ctx",
"context",
".",
"Context",
",",
"rpcType",
"protos",
".",
"RPCType",
",",
"svType",
"string",
",",
"route",
"*",
"route",
".",
"Route",
",",
"msg",
"*",
"message",
".",
"Message",
",",
")",
"(",
"*",
"cluster",
".",
"Server",
",",
"error",
")",
"{",
"if",
"r",
".",
"serviceDiscovery",
"==",
"nil",
"{",
"return",
"nil",
",",
"constants",
".",
"ErrServiceDiscoveryNotInitialized",
"\n",
"}",
"\n",
"serversOfType",
",",
"err",
":=",
"r",
".",
"serviceDiscovery",
".",
"GetServersByType",
"(",
"svType",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"rpcType",
"==",
"protos",
".",
"RPCType_User",
"{",
"server",
":=",
"r",
".",
"defaultRoute",
"(",
"serversOfType",
")",
"\n",
"return",
"server",
",",
"nil",
"\n",
"}",
"\n",
"routeFunc",
",",
"ok",
":=",
"r",
".",
"routesMap",
"[",
"svType",
"]",
"\n",
"if",
"!",
"ok",
"{",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"no specific route for svType: %s, using default route\"",
",",
"svType",
")",
"\n",
"server",
":=",
"r",
".",
"defaultRoute",
"(",
"serversOfType",
")",
"\n",
"return",
"server",
",",
"nil",
"\n",
"}",
"\n",
"return",
"routeFunc",
"(",
"ctx",
",",
"route",
",",
"msg",
".",
"Data",
",",
"serversOfType",
")",
"\n",
"}"
] | // Route gets the right server to use in the call | [
"Route",
"gets",
"the",
"right",
"server",
"to",
"use",
"in",
"the",
"call"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/router/router.go#L76-L101 | train |
topfreegames/pitaya | agent/agent.go | NewAgent | func NewAgent(
conn net.Conn,
packetDecoder codec.PacketDecoder,
packetEncoder codec.PacketEncoder,
serializer serialize.Serializer,
heartbeatTime time.Duration,
messagesBufferSize int,
dieChan chan bool,
messageEncoder message.Encoder,
metricsReporters []metrics.Reporter,
) *Agent {
// initialize heartbeat and handshake data on first user connection
once.Do(func() {
hbdEncode(heartbeatTime, packetEncoder, messageEncoder.IsCompressionEnabled(), serializer.GetName())
})
a := &Agent{
appDieChan: dieChan,
chDie: make(chan struct{}),
chSend: make(chan pendingMessage, messagesBufferSize),
chStopHeartbeat: make(chan struct{}),
chStopWrite: make(chan struct{}),
messagesBufferSize: messagesBufferSize,
conn: conn,
decoder: packetDecoder,
encoder: packetEncoder,
heartbeatTimeout: heartbeatTime,
lastAt: time.Now().Unix(),
serializer: serializer,
state: constants.StatusStart,
messageEncoder: messageEncoder,
metricsReporters: metricsReporters,
}
// binding session
s := session.New(a, true)
metrics.ReportNumberOfConnectedClients(metricsReporters, session.SessionCount)
a.Session = s
return a
} | go | func NewAgent(
conn net.Conn,
packetDecoder codec.PacketDecoder,
packetEncoder codec.PacketEncoder,
serializer serialize.Serializer,
heartbeatTime time.Duration,
messagesBufferSize int,
dieChan chan bool,
messageEncoder message.Encoder,
metricsReporters []metrics.Reporter,
) *Agent {
// initialize heartbeat and handshake data on first user connection
once.Do(func() {
hbdEncode(heartbeatTime, packetEncoder, messageEncoder.IsCompressionEnabled(), serializer.GetName())
})
a := &Agent{
appDieChan: dieChan,
chDie: make(chan struct{}),
chSend: make(chan pendingMessage, messagesBufferSize),
chStopHeartbeat: make(chan struct{}),
chStopWrite: make(chan struct{}),
messagesBufferSize: messagesBufferSize,
conn: conn,
decoder: packetDecoder,
encoder: packetEncoder,
heartbeatTimeout: heartbeatTime,
lastAt: time.Now().Unix(),
serializer: serializer,
state: constants.StatusStart,
messageEncoder: messageEncoder,
metricsReporters: metricsReporters,
}
// binding session
s := session.New(a, true)
metrics.ReportNumberOfConnectedClients(metricsReporters, session.SessionCount)
a.Session = s
return a
} | [
"func",
"NewAgent",
"(",
"conn",
"net",
".",
"Conn",
",",
"packetDecoder",
"codec",
".",
"PacketDecoder",
",",
"packetEncoder",
"codec",
".",
"PacketEncoder",
",",
"serializer",
"serialize",
".",
"Serializer",
",",
"heartbeatTime",
"time",
".",
"Duration",
",",
"messagesBufferSize",
"int",
",",
"dieChan",
"chan",
"bool",
",",
"messageEncoder",
"message",
".",
"Encoder",
",",
"metricsReporters",
"[",
"]",
"metrics",
".",
"Reporter",
",",
")",
"*",
"Agent",
"{",
"once",
".",
"Do",
"(",
"func",
"(",
")",
"{",
"hbdEncode",
"(",
"heartbeatTime",
",",
"packetEncoder",
",",
"messageEncoder",
".",
"IsCompressionEnabled",
"(",
")",
",",
"serializer",
".",
"GetName",
"(",
")",
")",
"\n",
"}",
")",
"\n",
"a",
":=",
"&",
"Agent",
"{",
"appDieChan",
":",
"dieChan",
",",
"chDie",
":",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
",",
"chSend",
":",
"make",
"(",
"chan",
"pendingMessage",
",",
"messagesBufferSize",
")",
",",
"chStopHeartbeat",
":",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
",",
"chStopWrite",
":",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
",",
"messagesBufferSize",
":",
"messagesBufferSize",
",",
"conn",
":",
"conn",
",",
"decoder",
":",
"packetDecoder",
",",
"encoder",
":",
"packetEncoder",
",",
"heartbeatTimeout",
":",
"heartbeatTime",
",",
"lastAt",
":",
"time",
".",
"Now",
"(",
")",
".",
"Unix",
"(",
")",
",",
"serializer",
":",
"serializer",
",",
"state",
":",
"constants",
".",
"StatusStart",
",",
"messageEncoder",
":",
"messageEncoder",
",",
"metricsReporters",
":",
"metricsReporters",
",",
"}",
"\n",
"s",
":=",
"session",
".",
"New",
"(",
"a",
",",
"true",
")",
"\n",
"metrics",
".",
"ReportNumberOfConnectedClients",
"(",
"metricsReporters",
",",
"session",
".",
"SessionCount",
")",
"\n",
"a",
".",
"Session",
"=",
"s",
"\n",
"return",
"a",
"\n",
"}"
] | // NewAgent create new agent instance | [
"NewAgent",
"create",
"new",
"agent",
"instance"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L93-L132 | train |
topfreegames/pitaya | agent/agent.go | Push | func (a *Agent) Push(route string, v interface{}) error {
if a.GetStatus() == constants.StatusClosed {
return constants.ErrBrokenPipe
}
switch d := v.(type) {
case []byte:
logger.Log.Debugf("Type=Push, ID=%d, UID=%d, Route=%s, Data=%dbytes",
a.Session.ID(), a.Session.UID(), route, len(d))
default:
logger.Log.Debugf("Type=Push, ID=%d, UID=%d, Route=%s, Data=%+v",
a.Session.ID(), a.Session.UID(), route, v)
}
return a.send(pendingMessage{typ: message.Push, route: route, payload: v})
} | go | func (a *Agent) Push(route string, v interface{}) error {
if a.GetStatus() == constants.StatusClosed {
return constants.ErrBrokenPipe
}
switch d := v.(type) {
case []byte:
logger.Log.Debugf("Type=Push, ID=%d, UID=%d, Route=%s, Data=%dbytes",
a.Session.ID(), a.Session.UID(), route, len(d))
default:
logger.Log.Debugf("Type=Push, ID=%d, UID=%d, Route=%s, Data=%+v",
a.Session.ID(), a.Session.UID(), route, v)
}
return a.send(pendingMessage{typ: message.Push, route: route, payload: v})
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"Push",
"(",
"route",
"string",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"a",
".",
"GetStatus",
"(",
")",
"==",
"constants",
".",
"StatusClosed",
"{",
"return",
"constants",
".",
"ErrBrokenPipe",
"\n",
"}",
"\n",
"switch",
"d",
":=",
"v",
".",
"(",
"type",
")",
"{",
"case",
"[",
"]",
"byte",
":",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"Type=Push, ID=%d, UID=%d, Route=%s, Data=%dbytes\"",
",",
"a",
".",
"Session",
".",
"ID",
"(",
")",
",",
"a",
".",
"Session",
".",
"UID",
"(",
")",
",",
"route",
",",
"len",
"(",
"d",
")",
")",
"\n",
"default",
":",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"Type=Push, ID=%d, UID=%d, Route=%s, Data=%+v\"",
",",
"a",
".",
"Session",
".",
"ID",
"(",
")",
",",
"a",
".",
"Session",
".",
"UID",
"(",
")",
",",
"route",
",",
"v",
")",
"\n",
"}",
"\n",
"return",
"a",
".",
"send",
"(",
"pendingMessage",
"{",
"typ",
":",
"message",
".",
"Push",
",",
"route",
":",
"route",
",",
"payload",
":",
"v",
"}",
")",
"\n",
"}"
] | // Push implementation for session.NetworkEntity interface | [
"Push",
"implementation",
"for",
"session",
".",
"NetworkEntity",
"interface"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L146-L160 | train |
topfreegames/pitaya | agent/agent.go | ResponseMID | func (a *Agent) ResponseMID(ctx context.Context, mid uint, v interface{}, isError ...bool) error {
err := false
if len(isError) > 0 {
err = isError[0]
}
if a.GetStatus() == constants.StatusClosed {
err := constants.ErrBrokenPipe
tracing.FinishSpan(ctx, err)
metrics.ReportTimingFromCtx(ctx, a.metricsReporters, handlerType, err)
return err
}
if mid <= 0 {
err := constants.ErrSessionOnNotify
tracing.FinishSpan(ctx, err)
metrics.ReportTimingFromCtx(ctx, a.metricsReporters, handlerType, err)
return err
}
switch d := v.(type) {
case []byte:
logger.Log.Debugf("Type=Response, ID=%d, UID=%d, MID=%d, Data=%dbytes",
a.Session.ID(), a.Session.UID(), mid, len(d))
default:
logger.Log.Infof("Type=Response, ID=%d, UID=%d, MID=%d, Data=%+v",
a.Session.ID(), a.Session.UID(), mid, v)
}
return a.send(pendingMessage{ctx: ctx, typ: message.Response, mid: mid, payload: v, err: err})
} | go | func (a *Agent) ResponseMID(ctx context.Context, mid uint, v interface{}, isError ...bool) error {
err := false
if len(isError) > 0 {
err = isError[0]
}
if a.GetStatus() == constants.StatusClosed {
err := constants.ErrBrokenPipe
tracing.FinishSpan(ctx, err)
metrics.ReportTimingFromCtx(ctx, a.metricsReporters, handlerType, err)
return err
}
if mid <= 0 {
err := constants.ErrSessionOnNotify
tracing.FinishSpan(ctx, err)
metrics.ReportTimingFromCtx(ctx, a.metricsReporters, handlerType, err)
return err
}
switch d := v.(type) {
case []byte:
logger.Log.Debugf("Type=Response, ID=%d, UID=%d, MID=%d, Data=%dbytes",
a.Session.ID(), a.Session.UID(), mid, len(d))
default:
logger.Log.Infof("Type=Response, ID=%d, UID=%d, MID=%d, Data=%+v",
a.Session.ID(), a.Session.UID(), mid, v)
}
return a.send(pendingMessage{ctx: ctx, typ: message.Response, mid: mid, payload: v, err: err})
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"ResponseMID",
"(",
"ctx",
"context",
".",
"Context",
",",
"mid",
"uint",
",",
"v",
"interface",
"{",
"}",
",",
"isError",
"...",
"bool",
")",
"error",
"{",
"err",
":=",
"false",
"\n",
"if",
"len",
"(",
"isError",
")",
">",
"0",
"{",
"err",
"=",
"isError",
"[",
"0",
"]",
"\n",
"}",
"\n",
"if",
"a",
".",
"GetStatus",
"(",
")",
"==",
"constants",
".",
"StatusClosed",
"{",
"err",
":=",
"constants",
".",
"ErrBrokenPipe",
"\n",
"tracing",
".",
"FinishSpan",
"(",
"ctx",
",",
"err",
")",
"\n",
"metrics",
".",
"ReportTimingFromCtx",
"(",
"ctx",
",",
"a",
".",
"metricsReporters",
",",
"handlerType",
",",
"err",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"if",
"mid",
"<=",
"0",
"{",
"err",
":=",
"constants",
".",
"ErrSessionOnNotify",
"\n",
"tracing",
".",
"FinishSpan",
"(",
"ctx",
",",
"err",
")",
"\n",
"metrics",
".",
"ReportTimingFromCtx",
"(",
"ctx",
",",
"a",
".",
"metricsReporters",
",",
"handlerType",
",",
"err",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"switch",
"d",
":=",
"v",
".",
"(",
"type",
")",
"{",
"case",
"[",
"]",
"byte",
":",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"Type=Response, ID=%d, UID=%d, MID=%d, Data=%dbytes\"",
",",
"a",
".",
"Session",
".",
"ID",
"(",
")",
",",
"a",
".",
"Session",
".",
"UID",
"(",
")",
",",
"mid",
",",
"len",
"(",
"d",
")",
")",
"\n",
"default",
":",
"logger",
".",
"Log",
".",
"Infof",
"(",
"\"Type=Response, ID=%d, UID=%d, MID=%d, Data=%+v\"",
",",
"a",
".",
"Session",
".",
"ID",
"(",
")",
",",
"a",
".",
"Session",
".",
"UID",
"(",
")",
",",
"mid",
",",
"v",
")",
"\n",
"}",
"\n",
"return",
"a",
".",
"send",
"(",
"pendingMessage",
"{",
"ctx",
":",
"ctx",
",",
"typ",
":",
"message",
".",
"Response",
",",
"mid",
":",
"mid",
",",
"payload",
":",
"v",
",",
"err",
":",
"err",
"}",
")",
"\n",
"}"
] | // ResponseMID implementation for session.NetworkEntity interface
// Respond message to session | [
"ResponseMID",
"implementation",
"for",
"session",
".",
"NetworkEntity",
"interface",
"Respond",
"message",
"to",
"session"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L164-L193 | train |
topfreegames/pitaya | agent/agent.go | Close | func (a *Agent) Close() error {
a.closeMutex.Lock()
defer a.closeMutex.Unlock()
if a.GetStatus() == constants.StatusClosed {
return constants.ErrCloseClosedSession
}
a.SetStatus(constants.StatusClosed)
logger.Log.Debugf("Session closed, ID=%d, UID=%d, IP=%s",
a.Session.ID(), a.Session.UID(), a.conn.RemoteAddr())
// prevent closing closed channel
select {
case <-a.chDie:
// expect
default:
close(a.chStopWrite)
close(a.chStopHeartbeat)
close(a.chDie)
onSessionClosed(a.Session)
}
metrics.ReportNumberOfConnectedClients(a.metricsReporters, session.SessionCount)
return a.conn.Close()
} | go | func (a *Agent) Close() error {
a.closeMutex.Lock()
defer a.closeMutex.Unlock()
if a.GetStatus() == constants.StatusClosed {
return constants.ErrCloseClosedSession
}
a.SetStatus(constants.StatusClosed)
logger.Log.Debugf("Session closed, ID=%d, UID=%d, IP=%s",
a.Session.ID(), a.Session.UID(), a.conn.RemoteAddr())
// prevent closing closed channel
select {
case <-a.chDie:
// expect
default:
close(a.chStopWrite)
close(a.chStopHeartbeat)
close(a.chDie)
onSessionClosed(a.Session)
}
metrics.ReportNumberOfConnectedClients(a.metricsReporters, session.SessionCount)
return a.conn.Close()
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"Close",
"(",
")",
"error",
"{",
"a",
".",
"closeMutex",
".",
"Lock",
"(",
")",
"\n",
"defer",
"a",
".",
"closeMutex",
".",
"Unlock",
"(",
")",
"\n",
"if",
"a",
".",
"GetStatus",
"(",
")",
"==",
"constants",
".",
"StatusClosed",
"{",
"return",
"constants",
".",
"ErrCloseClosedSession",
"\n",
"}",
"\n",
"a",
".",
"SetStatus",
"(",
"constants",
".",
"StatusClosed",
")",
"\n",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"Session closed, ID=%d, UID=%d, IP=%s\"",
",",
"a",
".",
"Session",
".",
"ID",
"(",
")",
",",
"a",
".",
"Session",
".",
"UID",
"(",
")",
",",
"a",
".",
"conn",
".",
"RemoteAddr",
"(",
")",
")",
"\n",
"select",
"{",
"case",
"<-",
"a",
".",
"chDie",
":",
"default",
":",
"close",
"(",
"a",
".",
"chStopWrite",
")",
"\n",
"close",
"(",
"a",
".",
"chStopHeartbeat",
")",
"\n",
"close",
"(",
"a",
".",
"chDie",
")",
"\n",
"onSessionClosed",
"(",
"a",
".",
"Session",
")",
"\n",
"}",
"\n",
"metrics",
".",
"ReportNumberOfConnectedClients",
"(",
"a",
".",
"metricsReporters",
",",
"session",
".",
"SessionCount",
")",
"\n",
"return",
"a",
".",
"conn",
".",
"Close",
"(",
")",
"\n",
"}"
] | // Close closes the agent, cleans inner state and closes low-level connection.
// Any blocked Read or Write operations will be unblocked and return errors. | [
"Close",
"closes",
"the",
"agent",
"cleans",
"inner",
"state",
"and",
"closes",
"low",
"-",
"level",
"connection",
".",
"Any",
"blocked",
"Read",
"or",
"Write",
"operations",
"will",
"be",
"unblocked",
"and",
"return",
"errors",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L197-L222 | train |
topfreegames/pitaya | agent/agent.go | String | func (a *Agent) String() string {
return fmt.Sprintf("Remote=%s, LastTime=%d", a.conn.RemoteAddr().String(), a.lastAt)
} | go | func (a *Agent) String() string {
return fmt.Sprintf("Remote=%s, LastTime=%d", a.conn.RemoteAddr().String(), a.lastAt)
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"String",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"Remote=%s, LastTime=%d\"",
",",
"a",
".",
"conn",
".",
"RemoteAddr",
"(",
")",
".",
"String",
"(",
")",
",",
"a",
".",
"lastAt",
")",
"\n",
"}"
] | // String, implementation for Stringer interface | [
"String",
"implementation",
"for",
"Stringer",
"interface"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L231-L233 | train |
topfreegames/pitaya | agent/agent.go | Kick | func (a *Agent) Kick(ctx context.Context) error {
// packet encode
p, err := a.encoder.Encode(packet.Kick, nil)
if err != nil {
return err
}
_, err = a.conn.Write(p)
return err
} | go | func (a *Agent) Kick(ctx context.Context) error {
// packet encode
p, err := a.encoder.Encode(packet.Kick, nil)
if err != nil {
return err
}
_, err = a.conn.Write(p)
return err
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"Kick",
"(",
"ctx",
"context",
".",
"Context",
")",
"error",
"{",
"p",
",",
"err",
":=",
"a",
".",
"encoder",
".",
"Encode",
"(",
"packet",
".",
"Kick",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"_",
",",
"err",
"=",
"a",
".",
"conn",
".",
"Write",
"(",
"p",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // Kick sends a kick packet to a client | [
"Kick",
"sends",
"a",
"kick",
"packet",
"to",
"a",
"client"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L241-L249 | train |
topfreegames/pitaya | agent/agent.go | SetStatus | func (a *Agent) SetStatus(state int32) {
atomic.StoreInt32(&a.state, state)
} | go | func (a *Agent) SetStatus(state int32) {
atomic.StoreInt32(&a.state, state)
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"SetStatus",
"(",
"state",
"int32",
")",
"{",
"atomic",
".",
"StoreInt32",
"(",
"&",
"a",
".",
"state",
",",
"state",
")",
"\n",
"}"
] | // SetStatus sets the agent status | [
"SetStatus",
"sets",
"the",
"agent",
"status"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L257-L259 | train |
topfreegames/pitaya | agent/agent.go | Handle | func (a *Agent) Handle() {
defer func() {
a.Close()
logger.Log.Debugf("Session handle goroutine exit, SessionID=%d, UID=%d", a.Session.ID(), a.Session.UID())
}()
go a.write()
go a.heartbeat()
select {
case <-a.chDie: // agent closed signal
return
}
} | go | func (a *Agent) Handle() {
defer func() {
a.Close()
logger.Log.Debugf("Session handle goroutine exit, SessionID=%d, UID=%d", a.Session.ID(), a.Session.UID())
}()
go a.write()
go a.heartbeat()
select {
case <-a.chDie: // agent closed signal
return
}
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"Handle",
"(",
")",
"{",
"defer",
"func",
"(",
")",
"{",
"a",
".",
"Close",
"(",
")",
"\n",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"Session handle goroutine exit, SessionID=%d, UID=%d\"",
",",
"a",
".",
"Session",
".",
"ID",
"(",
")",
",",
"a",
".",
"Session",
".",
"UID",
"(",
")",
")",
"\n",
"}",
"(",
")",
"\n",
"go",
"a",
".",
"write",
"(",
")",
"\n",
"go",
"a",
".",
"heartbeat",
"(",
")",
"\n",
"select",
"{",
"case",
"<-",
"a",
".",
"chDie",
":",
"return",
"\n",
"}",
"\n",
"}"
] | // Handle handles the messages from and to a client | [
"Handle",
"handles",
"the",
"messages",
"from",
"and",
"to",
"a",
"client"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L262-L274 | train |
topfreegames/pitaya | agent/agent.go | SendHandshakeResponse | func (a *Agent) SendHandshakeResponse() error {
_, err := a.conn.Write(hrd)
return err
} | go | func (a *Agent) SendHandshakeResponse() error {
_, err := a.conn.Write(hrd)
return err
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"SendHandshakeResponse",
"(",
")",
"error",
"{",
"_",
",",
"err",
":=",
"a",
".",
"conn",
".",
"Write",
"(",
"hrd",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // SendHandshakeResponse sends a handshake response | [
"SendHandshakeResponse",
"sends",
"a",
"handshake",
"response"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L337-L340 | train |
topfreegames/pitaya | agent/agent.go | AnswerWithError | func (a *Agent) AnswerWithError(ctx context.Context, mid uint, err error) {
if ctx != nil && err != nil {
s := opentracing.SpanFromContext(ctx)
if s != nil {
tracing.LogError(s, err.Error())
}
}
p, e := util.GetErrorPayload(a.serializer, err)
if e != nil {
logger.Log.Errorf("error answering the user with an error: %s", e.Error())
return
}
e = a.Session.ResponseMID(ctx, mid, p, true)
if e != nil {
logger.Log.Errorf("error answering the user with an error: %s", e.Error())
}
} | go | func (a *Agent) AnswerWithError(ctx context.Context, mid uint, err error) {
if ctx != nil && err != nil {
s := opentracing.SpanFromContext(ctx)
if s != nil {
tracing.LogError(s, err.Error())
}
}
p, e := util.GetErrorPayload(a.serializer, err)
if e != nil {
logger.Log.Errorf("error answering the user with an error: %s", e.Error())
return
}
e = a.Session.ResponseMID(ctx, mid, p, true)
if e != nil {
logger.Log.Errorf("error answering the user with an error: %s", e.Error())
}
} | [
"func",
"(",
"a",
"*",
"Agent",
")",
"AnswerWithError",
"(",
"ctx",
"context",
".",
"Context",
",",
"mid",
"uint",
",",
"err",
"error",
")",
"{",
"if",
"ctx",
"!=",
"nil",
"&&",
"err",
"!=",
"nil",
"{",
"s",
":=",
"opentracing",
".",
"SpanFromContext",
"(",
"ctx",
")",
"\n",
"if",
"s",
"!=",
"nil",
"{",
"tracing",
".",
"LogError",
"(",
"s",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"p",
",",
"e",
":=",
"util",
".",
"GetErrorPayload",
"(",
"a",
".",
"serializer",
",",
"err",
")",
"\n",
"if",
"e",
"!=",
"nil",
"{",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"error answering the user with an error: %s\"",
",",
"e",
".",
"Error",
"(",
")",
")",
"\n",
"return",
"\n",
"}",
"\n",
"e",
"=",
"a",
".",
"Session",
".",
"ResponseMID",
"(",
"ctx",
",",
"mid",
",",
"p",
",",
"true",
")",
"\n",
"if",
"e",
"!=",
"nil",
"{",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"error answering the user with an error: %s\"",
",",
"e",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"}"
] | // AnswerWithError answers with an error | [
"AnswerWithError",
"answers",
"with",
"an",
"error"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/agent/agent.go#L428-L444 | train |
topfreegames/pitaya | session/session.go | New | func New(entity NetworkEntity, frontend bool, UID ...string) *Session {
s := &Session{
id: sessionIDSvc.sessionID(),
entity: entity,
data: make(map[string]interface{}),
handshakeData: nil,
lastTime: time.Now().Unix(),
OnCloseCallbacks: []func(){},
IsFrontend: frontend,
}
if frontend {
sessionsByID.Store(s.id, s)
atomic.AddInt64(&SessionCount, 1)
}
if len(UID) > 0 {
s.uid = UID[0]
}
return s
} | go | func New(entity NetworkEntity, frontend bool, UID ...string) *Session {
s := &Session{
id: sessionIDSvc.sessionID(),
entity: entity,
data: make(map[string]interface{}),
handshakeData: nil,
lastTime: time.Now().Unix(),
OnCloseCallbacks: []func(){},
IsFrontend: frontend,
}
if frontend {
sessionsByID.Store(s.id, s)
atomic.AddInt64(&SessionCount, 1)
}
if len(UID) > 0 {
s.uid = UID[0]
}
return s
} | [
"func",
"New",
"(",
"entity",
"NetworkEntity",
",",
"frontend",
"bool",
",",
"UID",
"...",
"string",
")",
"*",
"Session",
"{",
"s",
":=",
"&",
"Session",
"{",
"id",
":",
"sessionIDSvc",
".",
"sessionID",
"(",
")",
",",
"entity",
":",
"entity",
",",
"data",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
")",
",",
"handshakeData",
":",
"nil",
",",
"lastTime",
":",
"time",
".",
"Now",
"(",
")",
".",
"Unix",
"(",
")",
",",
"OnCloseCallbacks",
":",
"[",
"]",
"func",
"(",
")",
"{",
"}",
",",
"IsFrontend",
":",
"frontend",
",",
"}",
"\n",
"if",
"frontend",
"{",
"sessionsByID",
".",
"Store",
"(",
"s",
".",
"id",
",",
"s",
")",
"\n",
"atomic",
".",
"AddInt64",
"(",
"&",
"SessionCount",
",",
"1",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"UID",
")",
">",
"0",
"{",
"s",
".",
"uid",
"=",
"UID",
"[",
"0",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] | // New returns a new session instance
// a NetworkEntity is a low-level network instance | [
"New",
"returns",
"a",
"new",
"session",
"instance",
"a",
"NetworkEntity",
"is",
"a",
"low",
"-",
"level",
"network",
"instance"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L114-L132 | train |
topfreegames/pitaya | session/session.go | GetSessionByUID | func GetSessionByUID(uid string) *Session {
// TODO: Block this operation in backend servers
if val, ok := sessionsByUID.Load(uid); ok {
return val.(*Session)
}
return nil
} | go | func GetSessionByUID(uid string) *Session {
// TODO: Block this operation in backend servers
if val, ok := sessionsByUID.Load(uid); ok {
return val.(*Session)
}
return nil
} | [
"func",
"GetSessionByUID",
"(",
"uid",
"string",
")",
"*",
"Session",
"{",
"if",
"val",
",",
"ok",
":=",
"sessionsByUID",
".",
"Load",
"(",
"uid",
")",
";",
"ok",
"{",
"return",
"val",
".",
"(",
"*",
"Session",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // GetSessionByUID return a session bound to an user id | [
"GetSessionByUID",
"return",
"a",
"session",
"bound",
"to",
"an",
"user",
"id"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L135-L141 | train |
topfreegames/pitaya | session/session.go | GetSessionByID | func GetSessionByID(id int64) *Session {
// TODO: Block this operation in backend servers
if val, ok := sessionsByID.Load(id); ok {
return val.(*Session)
}
return nil
} | go | func GetSessionByID(id int64) *Session {
// TODO: Block this operation in backend servers
if val, ok := sessionsByID.Load(id); ok {
return val.(*Session)
}
return nil
} | [
"func",
"GetSessionByID",
"(",
"id",
"int64",
")",
"*",
"Session",
"{",
"if",
"val",
",",
"ok",
":=",
"sessionsByID",
".",
"Load",
"(",
"id",
")",
";",
"ok",
"{",
"return",
"val",
".",
"(",
"*",
"Session",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // GetSessionByID return a session bound to a frontend server id | [
"GetSessionByID",
"return",
"a",
"session",
"bound",
"to",
"a",
"frontend",
"server",
"id"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L144-L150 | train |
topfreegames/pitaya | session/session.go | OnSessionBind | func OnSessionBind(f func(ctx context.Context, s *Session) error) {
// Prevents the same function to be added twice in onSessionBind
sf1 := reflect.ValueOf(f)
for _, fun := range sessionBindCallbacks {
sf2 := reflect.ValueOf(fun)
if sf1.Pointer() == sf2.Pointer() {
return
}
}
sessionBindCallbacks = append(sessionBindCallbacks, f)
} | go | func OnSessionBind(f func(ctx context.Context, s *Session) error) {
// Prevents the same function to be added twice in onSessionBind
sf1 := reflect.ValueOf(f)
for _, fun := range sessionBindCallbacks {
sf2 := reflect.ValueOf(fun)
if sf1.Pointer() == sf2.Pointer() {
return
}
}
sessionBindCallbacks = append(sessionBindCallbacks, f)
} | [
"func",
"OnSessionBind",
"(",
"f",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"s",
"*",
"Session",
")",
"error",
")",
"{",
"sf1",
":=",
"reflect",
".",
"ValueOf",
"(",
"f",
")",
"\n",
"for",
"_",
",",
"fun",
":=",
"range",
"sessionBindCallbacks",
"{",
"sf2",
":=",
"reflect",
".",
"ValueOf",
"(",
"fun",
")",
"\n",
"if",
"sf1",
".",
"Pointer",
"(",
")",
"==",
"sf2",
".",
"Pointer",
"(",
")",
"{",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"sessionBindCallbacks",
"=",
"append",
"(",
"sessionBindCallbacks",
",",
"f",
")",
"\n",
"}"
] | // OnSessionBind adds a method to be called when a session is bound
// same function cannot be added twice! | [
"OnSessionBind",
"adds",
"a",
"method",
"to",
"be",
"called",
"when",
"a",
"session",
"is",
"bound",
"same",
"function",
"cannot",
"be",
"added",
"twice!"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L154-L164 | train |
topfreegames/pitaya | session/session.go | OnAfterSessionBind | func OnAfterSessionBind(f func(ctx context.Context, s *Session) error) {
// Prevents the same function to be added twice in onSessionBind
sf1 := reflect.ValueOf(f)
for _, fun := range afterBindCallbacks {
sf2 := reflect.ValueOf(fun)
if sf1.Pointer() == sf2.Pointer() {
return
}
}
afterBindCallbacks = append(afterBindCallbacks, f)
} | go | func OnAfterSessionBind(f func(ctx context.Context, s *Session) error) {
// Prevents the same function to be added twice in onSessionBind
sf1 := reflect.ValueOf(f)
for _, fun := range afterBindCallbacks {
sf2 := reflect.ValueOf(fun)
if sf1.Pointer() == sf2.Pointer() {
return
}
}
afterBindCallbacks = append(afterBindCallbacks, f)
} | [
"func",
"OnAfterSessionBind",
"(",
"f",
"func",
"(",
"ctx",
"context",
".",
"Context",
",",
"s",
"*",
"Session",
")",
"error",
")",
"{",
"sf1",
":=",
"reflect",
".",
"ValueOf",
"(",
"f",
")",
"\n",
"for",
"_",
",",
"fun",
":=",
"range",
"afterBindCallbacks",
"{",
"sf2",
":=",
"reflect",
".",
"ValueOf",
"(",
"fun",
")",
"\n",
"if",
"sf1",
".",
"Pointer",
"(",
")",
"==",
"sf2",
".",
"Pointer",
"(",
")",
"{",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"afterBindCallbacks",
"=",
"append",
"(",
"afterBindCallbacks",
",",
"f",
")",
"\n",
"}"
] | // OnAfterSessionBind adds a method to be called when session is bound and after all sessionBind callbacks | [
"OnAfterSessionBind",
"adds",
"a",
"method",
"to",
"be",
"called",
"when",
"session",
"is",
"bound",
"and",
"after",
"all",
"sessionBind",
"callbacks"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L167-L177 | train |
topfreegames/pitaya | session/session.go | OnSessionClose | func OnSessionClose(f func(s *Session)) {
sf1 := reflect.ValueOf(f)
for _, fun := range SessionCloseCallbacks {
sf2 := reflect.ValueOf(fun)
if sf1.Pointer() == sf2.Pointer() {
return
}
}
SessionCloseCallbacks = append(SessionCloseCallbacks, f)
} | go | func OnSessionClose(f func(s *Session)) {
sf1 := reflect.ValueOf(f)
for _, fun := range SessionCloseCallbacks {
sf2 := reflect.ValueOf(fun)
if sf1.Pointer() == sf2.Pointer() {
return
}
}
SessionCloseCallbacks = append(SessionCloseCallbacks, f)
} | [
"func",
"OnSessionClose",
"(",
"f",
"func",
"(",
"s",
"*",
"Session",
")",
")",
"{",
"sf1",
":=",
"reflect",
".",
"ValueOf",
"(",
"f",
")",
"\n",
"for",
"_",
",",
"fun",
":=",
"range",
"SessionCloseCallbacks",
"{",
"sf2",
":=",
"reflect",
".",
"ValueOf",
"(",
"fun",
")",
"\n",
"if",
"sf1",
".",
"Pointer",
"(",
")",
"==",
"sf2",
".",
"Pointer",
"(",
")",
"{",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"SessionCloseCallbacks",
"=",
"append",
"(",
"SessionCloseCallbacks",
",",
"f",
")",
"\n",
"}"
] | // OnSessionClose adds a method that will be called when every session closes | [
"OnSessionClose",
"adds",
"a",
"method",
"that",
"will",
"be",
"called",
"when",
"every",
"session",
"closes"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L180-L189 | train |
topfreegames/pitaya | session/session.go | Push | func (s *Session) Push(route string, v interface{}) error {
return s.entity.Push(route, v)
} | go | func (s *Session) Push(route string, v interface{}) error {
return s.entity.Push(route, v)
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Push",
"(",
"route",
"string",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"s",
".",
"entity",
".",
"Push",
"(",
"route",
",",
"v",
")",
"\n",
"}"
] | // Push message to client | [
"Push",
"message",
"to",
"client"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L202-L204 | train |
topfreegames/pitaya | session/session.go | ResponseMID | func (s *Session) ResponseMID(ctx context.Context, mid uint, v interface{}, err ...bool) error {
return s.entity.ResponseMID(ctx, mid, v, err...)
} | go | func (s *Session) ResponseMID(ctx context.Context, mid uint, v interface{}, err ...bool) error {
return s.entity.ResponseMID(ctx, mid, v, err...)
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"ResponseMID",
"(",
"ctx",
"context",
".",
"Context",
",",
"mid",
"uint",
",",
"v",
"interface",
"{",
"}",
",",
"err",
"...",
"bool",
")",
"error",
"{",
"return",
"s",
".",
"entity",
".",
"ResponseMID",
"(",
"ctx",
",",
"mid",
",",
"v",
",",
"err",
"...",
")",
"\n",
"}"
] | // ResponseMID responses message to client, mid is
// request message ID | [
"ResponseMID",
"responses",
"message",
"to",
"client",
"mid",
"is",
"request",
"message",
"ID"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L208-L210 | train |
topfreegames/pitaya | session/session.go | GetData | func (s *Session) GetData() map[string]interface{} {
s.RLock()
defer s.RUnlock()
return s.data
} | go | func (s *Session) GetData() map[string]interface{} {
s.RLock()
defer s.RUnlock()
return s.data
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"GetData",
"(",
")",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"return",
"s",
".",
"data",
"\n",
"}"
] | // GetData gets the data | [
"GetData",
"gets",
"the",
"data"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L223-L228 | train |
topfreegames/pitaya | session/session.go | SetData | func (s *Session) SetData(data map[string]interface{}) error {
s.Lock()
defer s.Unlock()
s.data = data
return s.updateEncodedData()
} | go | func (s *Session) SetData(data map[string]interface{}) error {
s.Lock()
defer s.Unlock()
s.data = data
return s.updateEncodedData()
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"SetData",
"(",
"data",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
")",
"error",
"{",
"s",
".",
"Lock",
"(",
")",
"\n",
"defer",
"s",
".",
"Unlock",
"(",
")",
"\n",
"s",
".",
"data",
"=",
"data",
"\n",
"return",
"s",
".",
"updateEncodedData",
"(",
")",
"\n",
"}"
] | // SetData sets the whole session data | [
"SetData",
"sets",
"the",
"whole",
"session",
"data"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L231-L237 | train |
topfreegames/pitaya | session/session.go | SetDataEncoded | func (s *Session) SetDataEncoded(encodedData []byte) error {
if len(encodedData) == 0 {
return nil
}
var data map[string]interface{}
err := json.Unmarshal(encodedData, &data)
if err != nil {
return err
}
return s.SetData(data)
} | go | func (s *Session) SetDataEncoded(encodedData []byte) error {
if len(encodedData) == 0 {
return nil
}
var data map[string]interface{}
err := json.Unmarshal(encodedData, &data)
if err != nil {
return err
}
return s.SetData(data)
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"SetDataEncoded",
"(",
"encodedData",
"[",
"]",
"byte",
")",
"error",
"{",
"if",
"len",
"(",
"encodedData",
")",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"var",
"data",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
"\n",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"encodedData",
",",
"&",
"data",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"s",
".",
"SetData",
"(",
"data",
")",
"\n",
"}"
] | // SetDataEncoded sets the whole session data from an encoded value | [
"SetDataEncoded",
"sets",
"the",
"whole",
"session",
"data",
"from",
"an",
"encoded",
"value"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L245-L255 | train |
topfreegames/pitaya | session/session.go | SetFrontendData | func (s *Session) SetFrontendData(frontendID string, frontendSessionID int64) {
s.frontendID = frontendID
s.frontendSessionID = frontendSessionID
} | go | func (s *Session) SetFrontendData(frontendID string, frontendSessionID int64) {
s.frontendID = frontendID
s.frontendSessionID = frontendSessionID
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"SetFrontendData",
"(",
"frontendID",
"string",
",",
"frontendSessionID",
"int64",
")",
"{",
"s",
".",
"frontendID",
"=",
"frontendID",
"\n",
"s",
".",
"frontendSessionID",
"=",
"frontendSessionID",
"\n",
"}"
] | // SetFrontendData sets frontend id and session id | [
"SetFrontendData",
"sets",
"frontend",
"id",
"and",
"session",
"id"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L258-L261 | train |
topfreegames/pitaya | session/session.go | Bind | func (s *Session) Bind(ctx context.Context, uid string) error {
if uid == "" {
return constants.ErrIllegalUID
}
if s.UID() != "" {
return constants.ErrSessionAlreadyBound
}
s.uid = uid
for _, cb := range sessionBindCallbacks {
err := cb(ctx, s)
if err != nil {
s.uid = ""
return err
}
}
for _, cb := range afterBindCallbacks {
err := cb(ctx, s)
if err != nil {
s.uid = ""
return err
}
}
// if code running on frontend server
if s.IsFrontend {
sessionsByUID.Store(uid, s)
} else {
// If frontentID is set this means it is a remote call and the current server
// is not the frontend server that received the user request
err := s.bindInFront(ctx)
if err != nil {
logger.Log.Error("error while trying to push session to front: ", err)
s.uid = ""
return err
}
}
return nil
} | go | func (s *Session) Bind(ctx context.Context, uid string) error {
if uid == "" {
return constants.ErrIllegalUID
}
if s.UID() != "" {
return constants.ErrSessionAlreadyBound
}
s.uid = uid
for _, cb := range sessionBindCallbacks {
err := cb(ctx, s)
if err != nil {
s.uid = ""
return err
}
}
for _, cb := range afterBindCallbacks {
err := cb(ctx, s)
if err != nil {
s.uid = ""
return err
}
}
// if code running on frontend server
if s.IsFrontend {
sessionsByUID.Store(uid, s)
} else {
// If frontentID is set this means it is a remote call and the current server
// is not the frontend server that received the user request
err := s.bindInFront(ctx)
if err != nil {
logger.Log.Error("error while trying to push session to front: ", err)
s.uid = ""
return err
}
}
return nil
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Bind",
"(",
"ctx",
"context",
".",
"Context",
",",
"uid",
"string",
")",
"error",
"{",
"if",
"uid",
"==",
"\"\"",
"{",
"return",
"constants",
".",
"ErrIllegalUID",
"\n",
"}",
"\n",
"if",
"s",
".",
"UID",
"(",
")",
"!=",
"\"\"",
"{",
"return",
"constants",
".",
"ErrSessionAlreadyBound",
"\n",
"}",
"\n",
"s",
".",
"uid",
"=",
"uid",
"\n",
"for",
"_",
",",
"cb",
":=",
"range",
"sessionBindCallbacks",
"{",
"err",
":=",
"cb",
"(",
"ctx",
",",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"s",
".",
"uid",
"=",
"\"\"",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"for",
"_",
",",
"cb",
":=",
"range",
"afterBindCallbacks",
"{",
"err",
":=",
"cb",
"(",
"ctx",
",",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"s",
".",
"uid",
"=",
"\"\"",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"s",
".",
"IsFrontend",
"{",
"sessionsByUID",
".",
"Store",
"(",
"uid",
",",
"s",
")",
"\n",
"}",
"else",
"{",
"err",
":=",
"s",
".",
"bindInFront",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"logger",
".",
"Log",
".",
"Error",
"(",
"\"error while trying to push session to front: \"",
",",
"err",
")",
"\n",
"s",
".",
"uid",
"=",
"\"\"",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Bind bind UID to current session | [
"Bind",
"bind",
"UID",
"to",
"current",
"session"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L264-L304 | train |
topfreegames/pitaya | session/session.go | OnClose | func (s *Session) OnClose(c func()) error {
if !s.IsFrontend {
return constants.ErrOnCloseBackend
}
s.OnCloseCallbacks = append(s.OnCloseCallbacks, c)
return nil
} | go | func (s *Session) OnClose(c func()) error {
if !s.IsFrontend {
return constants.ErrOnCloseBackend
}
s.OnCloseCallbacks = append(s.OnCloseCallbacks, c)
return nil
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"OnClose",
"(",
"c",
"func",
"(",
")",
")",
"error",
"{",
"if",
"!",
"s",
".",
"IsFrontend",
"{",
"return",
"constants",
".",
"ErrOnCloseBackend",
"\n",
"}",
"\n",
"s",
".",
"OnCloseCallbacks",
"=",
"append",
"(",
"s",
".",
"OnCloseCallbacks",
",",
"c",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // OnClose adds the function it receives to the callbacks that will be called
// when the session is closed | [
"OnClose",
"adds",
"the",
"function",
"it",
"receives",
"to",
"the",
"callbacks",
"that",
"will",
"be",
"called",
"when",
"the",
"session",
"is",
"closed"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L317-L323 | train |
topfreegames/pitaya | session/session.go | Close | func (s *Session) Close() {
atomic.AddInt64(&SessionCount, -1)
sessionsByID.Delete(s.ID())
sessionsByUID.Delete(s.UID())
// TODO: this logic should be moved to nats rpc server
if s.IsFrontend && s.Subscriptions != nil && len(s.Subscriptions) > 0 {
// if the user is bound to an userid and nats rpc server is being used we need to unsubscribe
for _, sub := range s.Subscriptions {
err := sub.Unsubscribe()
if err != nil {
logger.Log.Errorf("error unsubscribing to user's messages channel: %s, this can cause performance and leak issues", err.Error())
} else {
logger.Log.Debugf("successfully unsubscribed to user's %s messages channel", s.UID())
}
}
}
s.entity.Close()
} | go | func (s *Session) Close() {
atomic.AddInt64(&SessionCount, -1)
sessionsByID.Delete(s.ID())
sessionsByUID.Delete(s.UID())
// TODO: this logic should be moved to nats rpc server
if s.IsFrontend && s.Subscriptions != nil && len(s.Subscriptions) > 0 {
// if the user is bound to an userid and nats rpc server is being used we need to unsubscribe
for _, sub := range s.Subscriptions {
err := sub.Unsubscribe()
if err != nil {
logger.Log.Errorf("error unsubscribing to user's messages channel: %s, this can cause performance and leak issues", err.Error())
} else {
logger.Log.Debugf("successfully unsubscribed to user's %s messages channel", s.UID())
}
}
}
s.entity.Close()
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Close",
"(",
")",
"{",
"atomic",
".",
"AddInt64",
"(",
"&",
"SessionCount",
",",
"-",
"1",
")",
"\n",
"sessionsByID",
".",
"Delete",
"(",
"s",
".",
"ID",
"(",
")",
")",
"\n",
"sessionsByUID",
".",
"Delete",
"(",
"s",
".",
"UID",
"(",
")",
")",
"\n",
"if",
"s",
".",
"IsFrontend",
"&&",
"s",
".",
"Subscriptions",
"!=",
"nil",
"&&",
"len",
"(",
"s",
".",
"Subscriptions",
")",
">",
"0",
"{",
"for",
"_",
",",
"sub",
":=",
"range",
"s",
".",
"Subscriptions",
"{",
"err",
":=",
"sub",
".",
"Unsubscribe",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"error unsubscribing to user's messages channel: %s, this can cause performance and leak issues\"",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"else",
"{",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"successfully unsubscribed to user's %s messages channel\"",
",",
"s",
".",
"UID",
"(",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"s",
".",
"entity",
".",
"Close",
"(",
")",
"\n",
"}"
] | // Close terminates current session, session related data will not be released,
// all related data should be cleared explicitly in Session closed callback | [
"Close",
"terminates",
"current",
"session",
"session",
"related",
"data",
"will",
"not",
"be",
"released",
"all",
"related",
"data",
"should",
"be",
"cleared",
"explicitly",
"in",
"Session",
"closed",
"callback"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L327-L344 | train |
topfreegames/pitaya | session/session.go | Remove | func (s *Session) Remove(key string) error {
s.Lock()
defer s.Unlock()
delete(s.data, key)
return s.updateEncodedData()
} | go | func (s *Session) Remove(key string) error {
s.Lock()
defer s.Unlock()
delete(s.data, key)
return s.updateEncodedData()
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Remove",
"(",
"key",
"string",
")",
"error",
"{",
"s",
".",
"Lock",
"(",
")",
"\n",
"defer",
"s",
".",
"Unlock",
"(",
")",
"\n",
"delete",
"(",
"s",
".",
"data",
",",
"key",
")",
"\n",
"return",
"s",
".",
"updateEncodedData",
"(",
")",
"\n",
"}"
] | // Remove delete data associated with the key from session storage | [
"Remove",
"delete",
"data",
"associated",
"with",
"the",
"key",
"from",
"session",
"storage"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L352-L358 | train |
topfreegames/pitaya | session/session.go | HasKey | func (s *Session) HasKey(key string) bool {
s.RLock()
defer s.RUnlock()
_, has := s.data[key]
return has
} | go | func (s *Session) HasKey(key string) bool {
s.RLock()
defer s.RUnlock()
_, has := s.data[key]
return has
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"HasKey",
"(",
"key",
"string",
")",
"bool",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"_",
",",
"has",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"return",
"has",
"\n",
"}"
] | // HasKey decides whether a key has associated value | [
"HasKey",
"decides",
"whether",
"a",
"key",
"has",
"associated",
"value"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L370-L376 | train |
topfreegames/pitaya | session/session.go | Get | func (s *Session) Get(key string) interface{} {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return nil
}
return v
} | go | func (s *Session) Get(key string) interface{} {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return nil
}
return v
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Get",
"(",
"key",
"string",
")",
"interface",
"{",
"}",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"v",
"\n",
"}"
] | // Get returns a key value | [
"Get",
"returns",
"a",
"key",
"value"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L379-L388 | train |
topfreegames/pitaya | session/session.go | Int | func (s *Session) Int(key string) int {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int)
if !ok {
return 0
}
return value
} | go | func (s *Session) Int(key string) int {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Int",
"(",
"key",
"string",
")",
"int",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"int",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Int returns the value associated with the key as a int. | [
"Int",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"int",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L391-L405 | train |
topfreegames/pitaya | session/session.go | Int8 | func (s *Session) Int8(key string) int8 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int8)
if !ok {
return 0
}
return value
} | go | func (s *Session) Int8(key string) int8 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int8)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Int8",
"(",
"key",
"string",
")",
"int8",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"int8",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Int8 returns the value associated with the key as a int8. | [
"Int8",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"int8",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L408-L422 | train |
topfreegames/pitaya | session/session.go | Int16 | func (s *Session) Int16(key string) int16 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int16)
if !ok {
return 0
}
return value
} | go | func (s *Session) Int16(key string) int16 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int16)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Int16",
"(",
"key",
"string",
")",
"int16",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"int16",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Int16 returns the value associated with the key as a int16. | [
"Int16",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"int16",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L425-L439 | train |
topfreegames/pitaya | session/session.go | Int32 | func (s *Session) Int32(key string) int32 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int32)
if !ok {
return 0
}
return value
} | go | func (s *Session) Int32(key string) int32 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int32)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Int32",
"(",
"key",
"string",
")",
"int32",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"int32",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Int32 returns the value associated with the key as a int32. | [
"Int32",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"int32",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L442-L456 | train |
topfreegames/pitaya | session/session.go | Int64 | func (s *Session) Int64(key string) int64 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int64)
if !ok {
return 0
}
return value
} | go | func (s *Session) Int64(key string) int64 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(int64)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Int64",
"(",
"key",
"string",
")",
"int64",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"int64",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Int64 returns the value associated with the key as a int64. | [
"Int64",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"int64",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L459-L473 | train |
topfreegames/pitaya | session/session.go | Uint | func (s *Session) Uint(key string) uint {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint)
if !ok {
return 0
}
return value
} | go | func (s *Session) Uint(key string) uint {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Uint",
"(",
"key",
"string",
")",
"uint",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"uint",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Uint returns the value associated with the key as a uint. | [
"Uint",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"uint",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L476-L490 | train |
topfreegames/pitaya | session/session.go | Uint8 | func (s *Session) Uint8(key string) uint8 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint8)
if !ok {
return 0
}
return value
} | go | func (s *Session) Uint8(key string) uint8 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint8)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Uint8",
"(",
"key",
"string",
")",
"uint8",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"uint8",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Uint8 returns the value associated with the key as a uint8. | [
"Uint8",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"uint8",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L493-L507 | train |
topfreegames/pitaya | session/session.go | Uint16 | func (s *Session) Uint16(key string) uint16 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint16)
if !ok {
return 0
}
return value
} | go | func (s *Session) Uint16(key string) uint16 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint16)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Uint16",
"(",
"key",
"string",
")",
"uint16",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"uint16",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Uint16 returns the value associated with the key as a uint16. | [
"Uint16",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"uint16",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L510-L524 | train |
topfreegames/pitaya | session/session.go | Uint32 | func (s *Session) Uint32(key string) uint32 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint32)
if !ok {
return 0
}
return value
} | go | func (s *Session) Uint32(key string) uint32 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint32)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Uint32",
"(",
"key",
"string",
")",
"uint32",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"uint32",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Uint32 returns the value associated with the key as a uint32. | [
"Uint32",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"uint32",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L527-L541 | train |
topfreegames/pitaya | session/session.go | Uint64 | func (s *Session) Uint64(key string) uint64 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint64)
if !ok {
return 0
}
return value
} | go | func (s *Session) Uint64(key string) uint64 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(uint64)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Uint64",
"(",
"key",
"string",
")",
"uint64",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"uint64",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Uint64 returns the value associated with the key as a uint64. | [
"Uint64",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"uint64",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L544-L558 | train |
topfreegames/pitaya | session/session.go | Float32 | func (s *Session) Float32(key string) float32 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(float32)
if !ok {
return 0
}
return value
} | go | func (s *Session) Float32(key string) float32 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(float32)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Float32",
"(",
"key",
"string",
")",
"float32",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"float32",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Float32 returns the value associated with the key as a float32. | [
"Float32",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"float32",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L561-L575 | train |
topfreegames/pitaya | session/session.go | Float64 | func (s *Session) Float64(key string) float64 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(float64)
if !ok {
return 0
}
return value
} | go | func (s *Session) Float64(key string) float64 {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return 0
}
value, ok := v.(float64)
if !ok {
return 0
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Float64",
"(",
"key",
"string",
")",
"float64",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"float64",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // Float64 returns the value associated with the key as a float64. | [
"Float64",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"float64",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L578-L592 | train |
topfreegames/pitaya | session/session.go | String | func (s *Session) String(key string) string {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return ""
}
value, ok := v.(string)
if !ok {
return ""
}
return value
} | go | func (s *Session) String(key string) string {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
if !ok {
return ""
}
value, ok := v.(string)
if !ok {
return ""
}
return value
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"String",
"(",
"key",
"string",
")",
"string",
"{",
"s",
".",
"RLock",
"(",
")",
"\n",
"defer",
"s",
".",
"RUnlock",
"(",
")",
"\n",
"v",
",",
"ok",
":=",
"s",
".",
"data",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"\"\"",
"\n",
"}",
"\n",
"value",
",",
"ok",
":=",
"v",
".",
"(",
"string",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"\"\"",
"\n",
"}",
"\n",
"return",
"value",
"\n",
"}"
] | // String returns the value associated with the key as a string. | [
"String",
"returns",
"the",
"value",
"associated",
"with",
"the",
"key",
"as",
"a",
"string",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L595-L609 | train |
topfreegames/pitaya | session/session.go | PushToFront | func (s *Session) PushToFront(ctx context.Context) error {
if s.IsFrontend {
return constants.ErrFrontSessionCantPushToFront
}
return s.sendRequestToFront(ctx, constants.SessionPushRoute, true)
} | go | func (s *Session) PushToFront(ctx context.Context) error {
if s.IsFrontend {
return constants.ErrFrontSessionCantPushToFront
}
return s.sendRequestToFront(ctx, constants.SessionPushRoute, true)
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"PushToFront",
"(",
"ctx",
"context",
".",
"Context",
")",
"error",
"{",
"if",
"s",
".",
"IsFrontend",
"{",
"return",
"constants",
".",
"ErrFrontSessionCantPushToFront",
"\n",
"}",
"\n",
"return",
"s",
".",
"sendRequestToFront",
"(",
"ctx",
",",
"constants",
".",
"SessionPushRoute",
",",
"true",
")",
"\n",
"}"
] | // PushToFront updates the session in the frontend | [
"PushToFront",
"updates",
"the",
"session",
"in",
"the",
"frontend"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L624-L629 | train |
topfreegames/pitaya | session/session.go | Clear | func (s *Session) Clear() {
s.Lock()
defer s.Unlock()
s.uid = ""
s.data = map[string]interface{}{}
s.updateEncodedData()
} | go | func (s *Session) Clear() {
s.Lock()
defer s.Unlock()
s.uid = ""
s.data = map[string]interface{}{}
s.updateEncodedData()
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Clear",
"(",
")",
"{",
"s",
".",
"Lock",
"(",
")",
"\n",
"defer",
"s",
".",
"Unlock",
"(",
")",
"\n",
"s",
".",
"uid",
"=",
"\"\"",
"\n",
"s",
".",
"data",
"=",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
"{",
"}",
"\n",
"s",
".",
"updateEncodedData",
"(",
")",
"\n",
"}"
] | // Clear releases all data related to current session | [
"Clear",
"releases",
"all",
"data",
"related",
"to",
"current",
"session"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L632-L639 | train |
topfreegames/pitaya | session/session.go | SetHandshakeData | func (s *Session) SetHandshakeData(data *HandshakeData) {
s.Lock()
defer s.Unlock()
s.handshakeData = data
} | go | func (s *Session) SetHandshakeData(data *HandshakeData) {
s.Lock()
defer s.Unlock()
s.handshakeData = data
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"SetHandshakeData",
"(",
"data",
"*",
"HandshakeData",
")",
"{",
"s",
".",
"Lock",
"(",
")",
"\n",
"defer",
"s",
".",
"Unlock",
"(",
")",
"\n",
"s",
".",
"handshakeData",
"=",
"data",
"\n",
"}"
] | // SetHandshakeData sets the handshake data received by the client. | [
"SetHandshakeData",
"sets",
"the",
"handshake",
"data",
"received",
"by",
"the",
"client",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/session/session.go#L642-L647 | train |
topfreegames/pitaya | serialize/protobuf/protobuf.go | Marshal | func (s *Serializer) Marshal(v interface{}) ([]byte, error) {
pb, ok := v.(proto.Message)
if !ok {
return nil, constants.ErrWrongValueType
}
return proto.Marshal(pb)
} | go | func (s *Serializer) Marshal(v interface{}) ([]byte, error) {
pb, ok := v.(proto.Message)
if !ok {
return nil, constants.ErrWrongValueType
}
return proto.Marshal(pb)
} | [
"func",
"(",
"s",
"*",
"Serializer",
")",
"Marshal",
"(",
"v",
"interface",
"{",
"}",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"pb",
",",
"ok",
":=",
"v",
".",
"(",
"proto",
".",
"Message",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"constants",
".",
"ErrWrongValueType",
"\n",
"}",
"\n",
"return",
"proto",
".",
"Marshal",
"(",
"pb",
")",
"\n",
"}"
] | // Marshal returns the protobuf encoding of v. | [
"Marshal",
"returns",
"the",
"protobuf",
"encoding",
"of",
"v",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/serialize/protobuf/protobuf.go#L37-L43 | train |
topfreegames/pitaya | serialize/protobuf/protobuf.go | Unmarshal | func (s *Serializer) Unmarshal(data []byte, v interface{}) error {
pb, ok := v.(proto.Message)
if !ok {
return constants.ErrWrongValueType
}
return proto.Unmarshal(data, pb)
} | go | func (s *Serializer) Unmarshal(data []byte, v interface{}) error {
pb, ok := v.(proto.Message)
if !ok {
return constants.ErrWrongValueType
}
return proto.Unmarshal(data, pb)
} | [
"func",
"(",
"s",
"*",
"Serializer",
")",
"Unmarshal",
"(",
"data",
"[",
"]",
"byte",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"pb",
",",
"ok",
":=",
"v",
".",
"(",
"proto",
".",
"Message",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"constants",
".",
"ErrWrongValueType",
"\n",
"}",
"\n",
"return",
"proto",
".",
"Unmarshal",
"(",
"data",
",",
"pb",
")",
"\n",
"}"
] | // Unmarshal parses the protobuf-encoded data and stores the result
// in the value pointed to by v. | [
"Unmarshal",
"parses",
"the",
"protobuf",
"-",
"encoded",
"data",
"and",
"stores",
"the",
"result",
"in",
"the",
"value",
"pointed",
"to",
"by",
"v",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/serialize/protobuf/protobuf.go#L47-L53 | train |
topfreegames/pitaya | timer/timer.go | NewTimer | func NewTimer(fn Func, interval time.Duration, counter int) *Timer {
id := atomic.AddInt64(&Manager.incrementID, 1)
t := &Timer{
ID: id,
fn: fn,
createAt: time.Now().UnixNano(),
interval: interval,
elapse: int64(interval), // first execution will be after interval
counter: counter,
}
// add to manager
Manager.ChCreatedTimer <- t
return t
} | go | func NewTimer(fn Func, interval time.Duration, counter int) *Timer {
id := atomic.AddInt64(&Manager.incrementID, 1)
t := &Timer{
ID: id,
fn: fn,
createAt: time.Now().UnixNano(),
interval: interval,
elapse: int64(interval), // first execution will be after interval
counter: counter,
}
// add to manager
Manager.ChCreatedTimer <- t
return t
} | [
"func",
"NewTimer",
"(",
"fn",
"Func",
",",
"interval",
"time",
".",
"Duration",
",",
"counter",
"int",
")",
"*",
"Timer",
"{",
"id",
":=",
"atomic",
".",
"AddInt64",
"(",
"&",
"Manager",
".",
"incrementID",
",",
"1",
")",
"\n",
"t",
":=",
"&",
"Timer",
"{",
"ID",
":",
"id",
",",
"fn",
":",
"fn",
",",
"createAt",
":",
"time",
".",
"Now",
"(",
")",
".",
"UnixNano",
"(",
")",
",",
"interval",
":",
"interval",
",",
"elapse",
":",
"int64",
"(",
"interval",
")",
",",
"counter",
":",
"counter",
",",
"}",
"\n",
"Manager",
".",
"ChCreatedTimer",
"<-",
"t",
"\n",
"return",
"t",
"\n",
"}"
] | // NewTimer creates a cron job | [
"NewTimer",
"creates",
"a",
"cron",
"job"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/timer/timer.go#L97-L111 | train |
topfreegames/pitaya | timer/timer.go | Stop | func (t *Timer) Stop() {
if atomic.LoadInt32(&t.closed) > 0 {
return
}
// guarantee that logic is not blocked
if len(Manager.ChClosingTimer) < timerBacklog {
Manager.ChClosingTimer <- t.ID
atomic.StoreInt32(&t.closed, 1)
} else {
t.counter = 0 // automatically closed in next Cron
}
} | go | func (t *Timer) Stop() {
if atomic.LoadInt32(&t.closed) > 0 {
return
}
// guarantee that logic is not blocked
if len(Manager.ChClosingTimer) < timerBacklog {
Manager.ChClosingTimer <- t.ID
atomic.StoreInt32(&t.closed, 1)
} else {
t.counter = 0 // automatically closed in next Cron
}
} | [
"func",
"(",
"t",
"*",
"Timer",
")",
"Stop",
"(",
")",
"{",
"if",
"atomic",
".",
"LoadInt32",
"(",
"&",
"t",
".",
"closed",
")",
">",
"0",
"{",
"return",
"\n",
"}",
"\n",
"if",
"len",
"(",
"Manager",
".",
"ChClosingTimer",
")",
"<",
"timerBacklog",
"{",
"Manager",
".",
"ChClosingTimer",
"<-",
"t",
".",
"ID",
"\n",
"atomic",
".",
"StoreInt32",
"(",
"&",
"t",
".",
"closed",
",",
"1",
")",
"\n",
"}",
"else",
"{",
"t",
".",
"counter",
"=",
"0",
"\n",
"}",
"\n",
"}"
] | // Stop turns off a timer. After Stop, fn will not be called forever | [
"Stop",
"turns",
"off",
"a",
"timer",
".",
"After",
"Stop",
"fn",
"will",
"not",
"be",
"called",
"forever"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/timer/timer.go#L119-L131 | train |
topfreegames/pitaya | timer/timer.go | pexec | func pexec(id int64, fn Func) {
defer func() {
if err := recover(); err != nil {
logger.Log.Errorf("Call timer function error, TimerID=%d, Error=%v", id, err)
}
}()
fn()
} | go | func pexec(id int64, fn Func) {
defer func() {
if err := recover(); err != nil {
logger.Log.Errorf("Call timer function error, TimerID=%d, Error=%v", id, err)
}
}()
fn()
} | [
"func",
"pexec",
"(",
"id",
"int64",
",",
"fn",
"Func",
")",
"{",
"defer",
"func",
"(",
")",
"{",
"if",
"err",
":=",
"recover",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"Call timer function error, TimerID=%d, Error=%v\"",
",",
"id",
",",
"err",
")",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n",
"fn",
"(",
")",
"\n",
"}"
] | // execute job function with protection | [
"execute",
"job",
"function",
"with",
"protection"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/timer/timer.go#L134-L142 | train |
topfreegames/pitaya | conn/codec/mocks/packet_encoder.go | NewMockPacketEncoder | func NewMockPacketEncoder(ctrl *gomock.Controller) *MockPacketEncoder {
mock := &MockPacketEncoder{ctrl: ctrl}
mock.recorder = &MockPacketEncoderMockRecorder{mock}
return mock
} | go | func NewMockPacketEncoder(ctrl *gomock.Controller) *MockPacketEncoder {
mock := &MockPacketEncoder{ctrl: ctrl}
mock.recorder = &MockPacketEncoderMockRecorder{mock}
return mock
} | [
"func",
"NewMockPacketEncoder",
"(",
"ctrl",
"*",
"gomock",
".",
"Controller",
")",
"*",
"MockPacketEncoder",
"{",
"mock",
":=",
"&",
"MockPacketEncoder",
"{",
"ctrl",
":",
"ctrl",
"}",
"\n",
"mock",
".",
"recorder",
"=",
"&",
"MockPacketEncoderMockRecorder",
"{",
"mock",
"}",
"\n",
"return",
"mock",
"\n",
"}"
] | // NewMockPacketEncoder creates a new mock instance | [
"NewMockPacketEncoder",
"creates",
"a",
"new",
"mock",
"instance"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/conn/codec/mocks/packet_encoder.go#L25-L29 | train |
topfreegames/pitaya | cluster/server.go | AsJSONString | func (s *Server) AsJSONString() string {
str, err := json.Marshal(s)
if err != nil {
logger.Log.Errorf("error getting server as json: %s", err.Error())
return ""
}
return string(str)
} | go | func (s *Server) AsJSONString() string {
str, err := json.Marshal(s)
if err != nil {
logger.Log.Errorf("error getting server as json: %s", err.Error())
return ""
}
return string(str)
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"AsJSONString",
"(",
")",
"string",
"{",
"str",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"error getting server as json: %s\"",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"return",
"\"\"",
"\n",
"}",
"\n",
"return",
"string",
"(",
"str",
")",
"\n",
"}"
] | // AsJSONString returns the server as a json string | [
"AsJSONString",
"returns",
"the",
"server",
"as",
"a",
"json",
"string"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/server.go#L59-L66 | train |
topfreegames/pitaya | rpc.go | RPC | func RPC(ctx context.Context, routeStr string, reply proto.Message, arg proto.Message) error {
return doSendRPC(ctx, "", routeStr, reply, arg)
} | go | func RPC(ctx context.Context, routeStr string, reply proto.Message, arg proto.Message) error {
return doSendRPC(ctx, "", routeStr, reply, arg)
} | [
"func",
"RPC",
"(",
"ctx",
"context",
".",
"Context",
",",
"routeStr",
"string",
",",
"reply",
"proto",
".",
"Message",
",",
"arg",
"proto",
".",
"Message",
")",
"error",
"{",
"return",
"doSendRPC",
"(",
"ctx",
",",
"\"\"",
",",
"routeStr",
",",
"reply",
",",
"arg",
")",
"\n",
"}"
] | // RPC calls a method in a different server | [
"RPC",
"calls",
"a",
"method",
"in",
"a",
"different",
"server"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/rpc.go#L34-L36 | train |
topfreegames/pitaya | rpc.go | RPCTo | func RPCTo(ctx context.Context, serverID, routeStr string, reply proto.Message, arg proto.Message) error {
return doSendRPC(ctx, serverID, routeStr, reply, arg)
} | go | func RPCTo(ctx context.Context, serverID, routeStr string, reply proto.Message, arg proto.Message) error {
return doSendRPC(ctx, serverID, routeStr, reply, arg)
} | [
"func",
"RPCTo",
"(",
"ctx",
"context",
".",
"Context",
",",
"serverID",
",",
"routeStr",
"string",
",",
"reply",
"proto",
".",
"Message",
",",
"arg",
"proto",
".",
"Message",
")",
"error",
"{",
"return",
"doSendRPC",
"(",
"ctx",
",",
"serverID",
",",
"routeStr",
",",
"reply",
",",
"arg",
")",
"\n",
"}"
] | // RPCTo send a rpc to a specific server | [
"RPCTo",
"send",
"a",
"rpc",
"to",
"a",
"specific",
"server"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/rpc.go#L39-L41 | train |
topfreegames/pitaya | rpc.go | ReliableRPC | func ReliableRPC(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
) (jid string, err error) {
return app.worker.EnqueueRPC(routeStr, metadata, reply, arg)
} | go | func ReliableRPC(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
) (jid string, err error) {
return app.worker.EnqueueRPC(routeStr, metadata, reply, arg)
} | [
"func",
"ReliableRPC",
"(",
"routeStr",
"string",
",",
"metadata",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
",",
"reply",
",",
"arg",
"proto",
".",
"Message",
",",
")",
"(",
"jid",
"string",
",",
"err",
"error",
")",
"{",
"return",
"app",
".",
"worker",
".",
"EnqueueRPC",
"(",
"routeStr",
",",
"metadata",
",",
"reply",
",",
"arg",
")",
"\n",
"}"
] | // ReliableRPC enqueues RPC to worker so it's executed asynchronously
// Default enqueue options are used | [
"ReliableRPC",
"enqueues",
"RPC",
"to",
"worker",
"so",
"it",
"s",
"executed",
"asynchronously",
"Default",
"enqueue",
"options",
"are",
"used"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/rpc.go#L45-L51 | train |
topfreegames/pitaya | rpc.go | ReliableRPCWithOptions | func ReliableRPCWithOptions(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
opts *worker.EnqueueOpts,
) (jid string, err error) {
return app.worker.EnqueueRPCWithOptions(routeStr, metadata, reply, arg, opts)
} | go | func ReliableRPCWithOptions(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
opts *worker.EnqueueOpts,
) (jid string, err error) {
return app.worker.EnqueueRPCWithOptions(routeStr, metadata, reply, arg, opts)
} | [
"func",
"ReliableRPCWithOptions",
"(",
"routeStr",
"string",
",",
"metadata",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
",",
"reply",
",",
"arg",
"proto",
".",
"Message",
",",
"opts",
"*",
"worker",
".",
"EnqueueOpts",
",",
")",
"(",
"jid",
"string",
",",
"err",
"error",
")",
"{",
"return",
"app",
".",
"worker",
".",
"EnqueueRPCWithOptions",
"(",
"routeStr",
",",
"metadata",
",",
"reply",
",",
"arg",
",",
"opts",
")",
"\n",
"}"
] | // ReliableRPCWithOptions enqueues RPC to worker
// Receive worker options for this specific RPC | [
"ReliableRPCWithOptions",
"enqueues",
"RPC",
"to",
"worker",
"Receive",
"worker",
"options",
"for",
"this",
"specific",
"RPC"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/rpc.go#L55-L62 | train |
topfreegames/pitaya | cluster/grpc_rpc_client.go | NewGRPCClient | func NewGRPCClient(
config *config.Config,
server *Server,
metricsReporters []metrics.Reporter,
bindingStorage interfaces.BindingStorage,
infoRetriever InfoRetriever,
) (*GRPCClient, error) {
gs := &GRPCClient{
config: config,
server: server,
metricsReporters: metricsReporters,
bindingStorage: bindingStorage,
infoRetriever: infoRetriever,
}
gs.configure()
return gs, nil
} | go | func NewGRPCClient(
config *config.Config,
server *Server,
metricsReporters []metrics.Reporter,
bindingStorage interfaces.BindingStorage,
infoRetriever InfoRetriever,
) (*GRPCClient, error) {
gs := &GRPCClient{
config: config,
server: server,
metricsReporters: metricsReporters,
bindingStorage: bindingStorage,
infoRetriever: infoRetriever,
}
gs.configure()
return gs, nil
} | [
"func",
"NewGRPCClient",
"(",
"config",
"*",
"config",
".",
"Config",
",",
"server",
"*",
"Server",
",",
"metricsReporters",
"[",
"]",
"metrics",
".",
"Reporter",
",",
"bindingStorage",
"interfaces",
".",
"BindingStorage",
",",
"infoRetriever",
"InfoRetriever",
",",
")",
"(",
"*",
"GRPCClient",
",",
"error",
")",
"{",
"gs",
":=",
"&",
"GRPCClient",
"{",
"config",
":",
"config",
",",
"server",
":",
"server",
",",
"metricsReporters",
":",
"metricsReporters",
",",
"bindingStorage",
":",
"bindingStorage",
",",
"infoRetriever",
":",
"infoRetriever",
",",
"}",
"\n",
"gs",
".",
"configure",
"(",
")",
"\n",
"return",
"gs",
",",
"nil",
"\n",
"}"
] | // NewGRPCClient returns a new instance of GRPCClient | [
"NewGRPCClient",
"returns",
"a",
"new",
"instance",
"of",
"GRPCClient"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/grpc_rpc_client.go#L58-L76 | train |
topfreegames/pitaya | cluster/grpc_rpc_client.go | Call | func (gs *GRPCClient) Call(ctx context.Context, rpcType protos.RPCType, route *route.Route, session *session.Session, msg *message.Message, server *Server) (*protos.Response, error) {
parent, err := tracing.ExtractSpan(ctx)
if err != nil {
logger.Log.Warnf("failed to retrieve parent span: %s", err.Error())
}
tags := opentracing.Tags{
"span.kind": "client",
"local.id": gs.server.ID,
"peer.serverType": server.Type,
"peer.id": server.ID,
}
ctx = tracing.StartSpan(ctx, "RPC Call", tags, parent)
defer tracing.FinishSpan(ctx, err)
req, err := buildRequest(ctx, rpcType, route, session, msg, gs.server)
if err != nil {
return nil, err
}
if c, ok := gs.clientMap.Load(server.ID); ok {
ctxT, done := context.WithTimeout(ctx, gs.reqTimeout)
defer done()
if gs.metricsReporters != nil {
startTime := time.Now()
ctxT = pcontext.AddToPropagateCtx(ctxT, constants.StartTimeKey, startTime.UnixNano())
ctxT = pcontext.AddToPropagateCtx(ctxT, constants.RouteKey, route.String())
defer metrics.ReportTimingFromCtx(ctxT, gs.metricsReporters, "rpc", err)
}
res, err := c.(protos.PitayaClient).Call(ctxT, &req)
if err != nil {
return nil, err
}
if res.Error != nil {
if res.Error.Code == "" {
res.Error.Code = pitErrors.ErrUnknownCode
}
err = &pitErrors.Error{
Code: res.Error.Code,
Message: res.Error.Msg,
Metadata: res.Error.Metadata,
}
return nil, err
}
return res, nil
}
return nil, constants.ErrNoConnectionToServer
} | go | func (gs *GRPCClient) Call(ctx context.Context, rpcType protos.RPCType, route *route.Route, session *session.Session, msg *message.Message, server *Server) (*protos.Response, error) {
parent, err := tracing.ExtractSpan(ctx)
if err != nil {
logger.Log.Warnf("failed to retrieve parent span: %s", err.Error())
}
tags := opentracing.Tags{
"span.kind": "client",
"local.id": gs.server.ID,
"peer.serverType": server.Type,
"peer.id": server.ID,
}
ctx = tracing.StartSpan(ctx, "RPC Call", tags, parent)
defer tracing.FinishSpan(ctx, err)
req, err := buildRequest(ctx, rpcType, route, session, msg, gs.server)
if err != nil {
return nil, err
}
if c, ok := gs.clientMap.Load(server.ID); ok {
ctxT, done := context.WithTimeout(ctx, gs.reqTimeout)
defer done()
if gs.metricsReporters != nil {
startTime := time.Now()
ctxT = pcontext.AddToPropagateCtx(ctxT, constants.StartTimeKey, startTime.UnixNano())
ctxT = pcontext.AddToPropagateCtx(ctxT, constants.RouteKey, route.String())
defer metrics.ReportTimingFromCtx(ctxT, gs.metricsReporters, "rpc", err)
}
res, err := c.(protos.PitayaClient).Call(ctxT, &req)
if err != nil {
return nil, err
}
if res.Error != nil {
if res.Error.Code == "" {
res.Error.Code = pitErrors.ErrUnknownCode
}
err = &pitErrors.Error{
Code: res.Error.Code,
Message: res.Error.Msg,
Metadata: res.Error.Metadata,
}
return nil, err
}
return res, nil
}
return nil, constants.ErrNoConnectionToServer
} | [
"func",
"(",
"gs",
"*",
"GRPCClient",
")",
"Call",
"(",
"ctx",
"context",
".",
"Context",
",",
"rpcType",
"protos",
".",
"RPCType",
",",
"route",
"*",
"route",
".",
"Route",
",",
"session",
"*",
"session",
".",
"Session",
",",
"msg",
"*",
"message",
".",
"Message",
",",
"server",
"*",
"Server",
")",
"(",
"*",
"protos",
".",
"Response",
",",
"error",
")",
"{",
"parent",
",",
"err",
":=",
"tracing",
".",
"ExtractSpan",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"logger",
".",
"Log",
".",
"Warnf",
"(",
"\"failed to retrieve parent span: %s\"",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"tags",
":=",
"opentracing",
".",
"Tags",
"{",
"\"span.kind\"",
":",
"\"client\"",
",",
"\"local.id\"",
":",
"gs",
".",
"server",
".",
"ID",
",",
"\"peer.serverType\"",
":",
"server",
".",
"Type",
",",
"\"peer.id\"",
":",
"server",
".",
"ID",
",",
"}",
"\n",
"ctx",
"=",
"tracing",
".",
"StartSpan",
"(",
"ctx",
",",
"\"RPC Call\"",
",",
"tags",
",",
"parent",
")",
"\n",
"defer",
"tracing",
".",
"FinishSpan",
"(",
"ctx",
",",
"err",
")",
"\n",
"req",
",",
"err",
":=",
"buildRequest",
"(",
"ctx",
",",
"rpcType",
",",
"route",
",",
"session",
",",
"msg",
",",
"gs",
".",
"server",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"c",
",",
"ok",
":=",
"gs",
".",
"clientMap",
".",
"Load",
"(",
"server",
".",
"ID",
")",
";",
"ok",
"{",
"ctxT",
",",
"done",
":=",
"context",
".",
"WithTimeout",
"(",
"ctx",
",",
"gs",
".",
"reqTimeout",
")",
"\n",
"defer",
"done",
"(",
")",
"\n",
"if",
"gs",
".",
"metricsReporters",
"!=",
"nil",
"{",
"startTime",
":=",
"time",
".",
"Now",
"(",
")",
"\n",
"ctxT",
"=",
"pcontext",
".",
"AddToPropagateCtx",
"(",
"ctxT",
",",
"constants",
".",
"StartTimeKey",
",",
"startTime",
".",
"UnixNano",
"(",
")",
")",
"\n",
"ctxT",
"=",
"pcontext",
".",
"AddToPropagateCtx",
"(",
"ctxT",
",",
"constants",
".",
"RouteKey",
",",
"route",
".",
"String",
"(",
")",
")",
"\n",
"defer",
"metrics",
".",
"ReportTimingFromCtx",
"(",
"ctxT",
",",
"gs",
".",
"metricsReporters",
",",
"\"rpc\"",
",",
"err",
")",
"\n",
"}",
"\n",
"res",
",",
"err",
":=",
"c",
".",
"(",
"protos",
".",
"PitayaClient",
")",
".",
"Call",
"(",
"ctxT",
",",
"&",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"res",
".",
"Error",
"!=",
"nil",
"{",
"if",
"res",
".",
"Error",
".",
"Code",
"==",
"\"\"",
"{",
"res",
".",
"Error",
".",
"Code",
"=",
"pitErrors",
".",
"ErrUnknownCode",
"\n",
"}",
"\n",
"err",
"=",
"&",
"pitErrors",
".",
"Error",
"{",
"Code",
":",
"res",
".",
"Error",
".",
"Code",
",",
"Message",
":",
"res",
".",
"Error",
".",
"Msg",
",",
"Metadata",
":",
"res",
".",
"Error",
".",
"Metadata",
",",
"}",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"res",
",",
"nil",
"\n",
"}",
"\n",
"return",
"nil",
",",
"constants",
".",
"ErrNoConnectionToServer",
"\n",
"}"
] | // Call makes a RPC Call | [
"Call",
"makes",
"a",
"RPC",
"Call"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/grpc_rpc_client.go#L89-L137 | train |
topfreegames/pitaya | cluster/grpc_rpc_client.go | Send | func (gs *GRPCClient) Send(uid string, d []byte) error {
return constants.ErrNotImplemented
} | go | func (gs *GRPCClient) Send(uid string, d []byte) error {
return constants.ErrNotImplemented
} | [
"func",
"(",
"gs",
"*",
"GRPCClient",
")",
"Send",
"(",
"uid",
"string",
",",
"d",
"[",
"]",
"byte",
")",
"error",
"{",
"return",
"constants",
".",
"ErrNotImplemented",
"\n",
"}"
] | // Send not implemented in grpc client | [
"Send",
"not",
"implemented",
"in",
"grpc",
"client"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/grpc_rpc_client.go#L140-L142 | train |
topfreegames/pitaya | cluster/grpc_rpc_client.go | BroadcastSessionBind | func (gs *GRPCClient) BroadcastSessionBind(uid string) error {
if gs.bindingStorage == nil {
return constants.ErrNoBindingStorageModule
}
fid, _ := gs.bindingStorage.GetUserFrontendID(uid, gs.server.Type)
if fid != "" {
if c, ok := gs.clientMap.Load(fid); ok {
msg := &protos.BindMsg{
Uid: uid,
Fid: gs.server.ID,
}
ctxT, done := context.WithTimeout(context.Background(), gs.reqTimeout)
defer done()
_, err := c.(protos.PitayaClient).SessionBindRemote(ctxT, msg)
return err
}
}
return nil
} | go | func (gs *GRPCClient) BroadcastSessionBind(uid string) error {
if gs.bindingStorage == nil {
return constants.ErrNoBindingStorageModule
}
fid, _ := gs.bindingStorage.GetUserFrontendID(uid, gs.server.Type)
if fid != "" {
if c, ok := gs.clientMap.Load(fid); ok {
msg := &protos.BindMsg{
Uid: uid,
Fid: gs.server.ID,
}
ctxT, done := context.WithTimeout(context.Background(), gs.reqTimeout)
defer done()
_, err := c.(protos.PitayaClient).SessionBindRemote(ctxT, msg)
return err
}
}
return nil
} | [
"func",
"(",
"gs",
"*",
"GRPCClient",
")",
"BroadcastSessionBind",
"(",
"uid",
"string",
")",
"error",
"{",
"if",
"gs",
".",
"bindingStorage",
"==",
"nil",
"{",
"return",
"constants",
".",
"ErrNoBindingStorageModule",
"\n",
"}",
"\n",
"fid",
",",
"_",
":=",
"gs",
".",
"bindingStorage",
".",
"GetUserFrontendID",
"(",
"uid",
",",
"gs",
".",
"server",
".",
"Type",
")",
"\n",
"if",
"fid",
"!=",
"\"\"",
"{",
"if",
"c",
",",
"ok",
":=",
"gs",
".",
"clientMap",
".",
"Load",
"(",
"fid",
")",
";",
"ok",
"{",
"msg",
":=",
"&",
"protos",
".",
"BindMsg",
"{",
"Uid",
":",
"uid",
",",
"Fid",
":",
"gs",
".",
"server",
".",
"ID",
",",
"}",
"\n",
"ctxT",
",",
"done",
":=",
"context",
".",
"WithTimeout",
"(",
"context",
".",
"Background",
"(",
")",
",",
"gs",
".",
"reqTimeout",
")",
"\n",
"defer",
"done",
"(",
")",
"\n",
"_",
",",
"err",
":=",
"c",
".",
"(",
"protos",
".",
"PitayaClient",
")",
".",
"SessionBindRemote",
"(",
"ctxT",
",",
"msg",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // BroadcastSessionBind sends the binding information to other servers that may be interested in this info | [
"BroadcastSessionBind",
"sends",
"the",
"binding",
"information",
"to",
"other",
"servers",
"that",
"may",
"be",
"interested",
"in",
"this",
"info"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/grpc_rpc_client.go#L145-L163 | train |
topfreegames/pitaya | cluster/grpc_rpc_client.go | SendKick | func (gs *GRPCClient) SendKick(userID string, serverType string, kick *protos.KickMsg) error {
var svID string
var err error
if gs.bindingStorage == nil {
return constants.ErrNoBindingStorageModule
}
svID, err = gs.bindingStorage.GetUserFrontendID(userID, serverType)
if err != nil {
return err
}
if c, ok := gs.clientMap.Load(svID); ok {
ctxT, done := context.WithTimeout(context.Background(), gs.reqTimeout)
defer done()
_, err := c.(protos.PitayaClient).KickUser(ctxT, kick)
return err
}
return constants.ErrNoConnectionToServer
} | go | func (gs *GRPCClient) SendKick(userID string, serverType string, kick *protos.KickMsg) error {
var svID string
var err error
if gs.bindingStorage == nil {
return constants.ErrNoBindingStorageModule
}
svID, err = gs.bindingStorage.GetUserFrontendID(userID, serverType)
if err != nil {
return err
}
if c, ok := gs.clientMap.Load(svID); ok {
ctxT, done := context.WithTimeout(context.Background(), gs.reqTimeout)
defer done()
_, err := c.(protos.PitayaClient).KickUser(ctxT, kick)
return err
}
return constants.ErrNoConnectionToServer
} | [
"func",
"(",
"gs",
"*",
"GRPCClient",
")",
"SendKick",
"(",
"userID",
"string",
",",
"serverType",
"string",
",",
"kick",
"*",
"protos",
".",
"KickMsg",
")",
"error",
"{",
"var",
"svID",
"string",
"\n",
"var",
"err",
"error",
"\n",
"if",
"gs",
".",
"bindingStorage",
"==",
"nil",
"{",
"return",
"constants",
".",
"ErrNoBindingStorageModule",
"\n",
"}",
"\n",
"svID",
",",
"err",
"=",
"gs",
".",
"bindingStorage",
".",
"GetUserFrontendID",
"(",
"userID",
",",
"serverType",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"c",
",",
"ok",
":=",
"gs",
".",
"clientMap",
".",
"Load",
"(",
"svID",
")",
";",
"ok",
"{",
"ctxT",
",",
"done",
":=",
"context",
".",
"WithTimeout",
"(",
"context",
".",
"Background",
"(",
")",
",",
"gs",
".",
"reqTimeout",
")",
"\n",
"defer",
"done",
"(",
")",
"\n",
"_",
",",
"err",
":=",
"c",
".",
"(",
"protos",
".",
"PitayaClient",
")",
".",
"KickUser",
"(",
"ctxT",
",",
"kick",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"return",
"constants",
".",
"ErrNoConnectionToServer",
"\n",
"}"
] | // SendKick sends a kick to an user | [
"SendKick",
"sends",
"a",
"kick",
"to",
"an",
"user"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/grpc_rpc_client.go#L166-L186 | train |
topfreegames/pitaya | cluster/grpc_rpc_client.go | AddServer | func (gs *GRPCClient) AddServer(sv *Server) {
var host, port, portKey string
var ok bool
host, portKey = gs.getServerHost(sv)
if host == "" {
logger.Log.Errorf("server %s has no grpcHost specified in metadata", sv.ID)
return
}
if port, ok = sv.Metadata[portKey]; !ok {
logger.Log.Errorf("server %s has no %s specified in metadata", sv.ID, portKey)
return
}
address := fmt.Sprintf("%s:%s", host, port)
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
logger.Log.Errorf("unable to connect to server %s at %s: %v", sv.ID, address, err)
return
}
c := protos.NewPitayaClient(conn)
gs.clientMap.Store(sv.ID, c)
logger.Log.Debugf("[grpc client] added server %s at %s", sv.ID, address)
} | go | func (gs *GRPCClient) AddServer(sv *Server) {
var host, port, portKey string
var ok bool
host, portKey = gs.getServerHost(sv)
if host == "" {
logger.Log.Errorf("server %s has no grpcHost specified in metadata", sv.ID)
return
}
if port, ok = sv.Metadata[portKey]; !ok {
logger.Log.Errorf("server %s has no %s specified in metadata", sv.ID, portKey)
return
}
address := fmt.Sprintf("%s:%s", host, port)
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
logger.Log.Errorf("unable to connect to server %s at %s: %v", sv.ID, address, err)
return
}
c := protos.NewPitayaClient(conn)
gs.clientMap.Store(sv.ID, c)
logger.Log.Debugf("[grpc client] added server %s at %s", sv.ID, address)
} | [
"func",
"(",
"gs",
"*",
"GRPCClient",
")",
"AddServer",
"(",
"sv",
"*",
"Server",
")",
"{",
"var",
"host",
",",
"port",
",",
"portKey",
"string",
"\n",
"var",
"ok",
"bool",
"\n",
"host",
",",
"portKey",
"=",
"gs",
".",
"getServerHost",
"(",
"sv",
")",
"\n",
"if",
"host",
"==",
"\"\"",
"{",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"server %s has no grpcHost specified in metadata\"",
",",
"sv",
".",
"ID",
")",
"\n",
"return",
"\n",
"}",
"\n",
"if",
"port",
",",
"ok",
"=",
"sv",
".",
"Metadata",
"[",
"portKey",
"]",
";",
"!",
"ok",
"{",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"server %s has no %s specified in metadata\"",
",",
"sv",
".",
"ID",
",",
"portKey",
")",
"\n",
"return",
"\n",
"}",
"\n",
"address",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"%s:%s\"",
",",
"host",
",",
"port",
")",
"\n",
"conn",
",",
"err",
":=",
"grpc",
".",
"Dial",
"(",
"address",
",",
"grpc",
".",
"WithInsecure",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"unable to connect to server %s at %s: %v\"",
",",
"sv",
".",
"ID",
",",
"address",
",",
"err",
")",
"\n",
"return",
"\n",
"}",
"\n",
"c",
":=",
"protos",
".",
"NewPitayaClient",
"(",
"conn",
")",
"\n",
"gs",
".",
"clientMap",
".",
"Store",
"(",
"sv",
".",
"ID",
",",
"c",
")",
"\n",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"[grpc client] added server %s at %s\"",
",",
"sv",
".",
"ID",
",",
"address",
")",
"\n",
"}"
] | // AddServer is called when a new server is discovered | [
"AddServer",
"is",
"called",
"when",
"a",
"new",
"server",
"is",
"discovered"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/grpc_rpc_client.go#L214-L238 | train |
topfreegames/pitaya | cluster/grpc_rpc_client.go | RemoveServer | func (gs *GRPCClient) RemoveServer(sv *Server) {
if _, ok := gs.clientMap.Load(sv.ID); ok {
// TODO: do I need to disconnect client?
gs.clientMap.Delete(sv.ID)
logger.Log.Debugf("[grpc client] removed server %s", sv.ID)
}
} | go | func (gs *GRPCClient) RemoveServer(sv *Server) {
if _, ok := gs.clientMap.Load(sv.ID); ok {
// TODO: do I need to disconnect client?
gs.clientMap.Delete(sv.ID)
logger.Log.Debugf("[grpc client] removed server %s", sv.ID)
}
} | [
"func",
"(",
"gs",
"*",
"GRPCClient",
")",
"RemoveServer",
"(",
"sv",
"*",
"Server",
")",
"{",
"if",
"_",
",",
"ok",
":=",
"gs",
".",
"clientMap",
".",
"Load",
"(",
"sv",
".",
"ID",
")",
";",
"ok",
"{",
"gs",
".",
"clientMap",
".",
"Delete",
"(",
"sv",
".",
"ID",
")",
"\n",
"logger",
".",
"Log",
".",
"Debugf",
"(",
"\"[grpc client] removed server %s\"",
",",
"sv",
".",
"ID",
")",
"\n",
"}",
"\n",
"}"
] | // RemoveServer is called when a server is removed | [
"RemoveServer",
"is",
"called",
"when",
"a",
"server",
"is",
"removed"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/grpc_rpc_client.go#L241-L247 | train |
topfreegames/pitaya | serialize/json/json.go | Unmarshal | func (s *Serializer) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
} | go | func (s *Serializer) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
} | [
"func",
"(",
"s",
"*",
"Serializer",
")",
"Unmarshal",
"(",
"data",
"[",
"]",
"byte",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"json",
".",
"Unmarshal",
"(",
"data",
",",
"v",
")",
"\n",
"}"
] | // Unmarshal parses the JSON-encoded data and stores the result
// in the value pointed to by v. | [
"Unmarshal",
"parses",
"the",
"JSON",
"-",
"encoded",
"data",
"and",
"stores",
"the",
"result",
"in",
"the",
"value",
"pointed",
"to",
"by",
"v",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/serialize/json/json.go#L42-L44 | train |
topfreegames/pitaya | examples/demo/pipeline/main.go | HandlerNoArg | func (g *MetagameServer) HandlerNoArg(ctx context.Context) (*HandlerNoArgResponse, error) {
return &HandlerNoArgResponse{
Msg: "ok",
}, nil
} | go | func (g *MetagameServer) HandlerNoArg(ctx context.Context) (*HandlerNoArgResponse, error) {
return &HandlerNoArgResponse{
Msg: "ok",
}, nil
} | [
"func",
"(",
"g",
"*",
"MetagameServer",
")",
"HandlerNoArg",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"*",
"HandlerNoArgResponse",
",",
"error",
")",
"{",
"return",
"&",
"HandlerNoArgResponse",
"{",
"Msg",
":",
"\"ok\"",
",",
"}",
",",
"nil",
"\n",
"}"
] | // HandlerNoArg is a simple handler that do not have any arguments | [
"HandlerNoArg",
"is",
"a",
"simple",
"handler",
"that",
"do",
"not",
"have",
"any",
"arguments"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/examples/demo/pipeline/main.go#L57-L61 | train |
topfreegames/pitaya | examples/demo/pipeline/main.go | simpleAfter | func (g *MetagameServer) simpleAfter(ctx context.Context, resp interface{}, err error) (interface{}, error) {
logger := pitaya.GetDefaultLoggerFromCtx(ctx)
logger.Infof("Simple After exec - response: %v , error: %v", resp, err)
return resp, err
} | go | func (g *MetagameServer) simpleAfter(ctx context.Context, resp interface{}, err error) (interface{}, error) {
logger := pitaya.GetDefaultLoggerFromCtx(ctx)
logger.Infof("Simple After exec - response: %v , error: %v", resp, err)
return resp, err
} | [
"func",
"(",
"g",
"*",
"MetagameServer",
")",
"simpleAfter",
"(",
"ctx",
"context",
".",
"Context",
",",
"resp",
"interface",
"{",
"}",
",",
"err",
"error",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"logger",
":=",
"pitaya",
".",
"GetDefaultLoggerFromCtx",
"(",
"ctx",
")",
"\n",
"logger",
".",
"Infof",
"(",
"\"Simple After exec - response: %v , error: %v\"",
",",
"resp",
",",
"err",
")",
"\n",
"return",
"resp",
",",
"err",
"\n",
"}"
] | // Simple example of an after pipeline. The 2nd argument is the handler response. | [
"Simple",
"example",
"of",
"an",
"after",
"pipeline",
".",
"The",
"2nd",
"argument",
"is",
"the",
"handler",
"response",
"."
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/examples/demo/pipeline/main.go#L84-L89 | train |
topfreegames/pitaya | component.go | Register | func Register(c component.Component, options ...component.Option) {
handlerComp = append(handlerComp, regComp{c, options})
} | go | func Register(c component.Component, options ...component.Option) {
handlerComp = append(handlerComp, regComp{c, options})
} | [
"func",
"Register",
"(",
"c",
"component",
".",
"Component",
",",
"options",
"...",
"component",
".",
"Option",
")",
"{",
"handlerComp",
"=",
"append",
"(",
"handlerComp",
",",
"regComp",
"{",
"c",
",",
"options",
"}",
")",
"\n",
"}"
] | // Register register a component with options | [
"Register",
"register",
"a",
"component",
"with",
"options"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/component.go#L39-L41 | train |
topfreegames/pitaya | component.go | RegisterRemote | func RegisterRemote(c component.Component, options ...component.Option) {
remoteComp = append(remoteComp, regComp{c, options})
} | go | func RegisterRemote(c component.Component, options ...component.Option) {
remoteComp = append(remoteComp, regComp{c, options})
} | [
"func",
"RegisterRemote",
"(",
"c",
"component",
".",
"Component",
",",
"options",
"...",
"component",
".",
"Option",
")",
"{",
"remoteComp",
"=",
"append",
"(",
"remoteComp",
",",
"regComp",
"{",
"c",
",",
"options",
"}",
")",
"\n",
"}"
] | // RegisterRemote register a remote component with options | [
"RegisterRemote",
"register",
"a",
"remote",
"component",
"with",
"options"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/component.go#L44-L46 | train |
topfreegames/pitaya | cluster/nats_rpc_server.go | GetUserMessagesTopic | func GetUserMessagesTopic(uid string, svType string) string {
return fmt.Sprintf("pitaya/%s/user/%s/push", svType, uid)
} | go | func GetUserMessagesTopic(uid string, svType string) string {
return fmt.Sprintf("pitaya/%s/user/%s/push", svType, uid)
} | [
"func",
"GetUserMessagesTopic",
"(",
"uid",
"string",
",",
"svType",
"string",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"pitaya/%s/user/%s/push\"",
",",
"svType",
",",
"uid",
")",
"\n",
"}"
] | // GetUserMessagesTopic get the topic for user | [
"GetUserMessagesTopic",
"get",
"the",
"topic",
"for",
"user"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/nats_rpc_server.go#L115-L117 | train |
topfreegames/pitaya | cluster/nats_rpc_server.go | GetUserKickTopic | func GetUserKickTopic(uid string, svType string) string {
return fmt.Sprintf("pitaya/%s/user/%s/kick", svType, uid)
} | go | func GetUserKickTopic(uid string, svType string) string {
return fmt.Sprintf("pitaya/%s/user/%s/kick", svType, uid)
} | [
"func",
"GetUserKickTopic",
"(",
"uid",
"string",
",",
"svType",
"string",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"pitaya/%s/user/%s/kick\"",
",",
"svType",
",",
"uid",
")",
"\n",
"}"
] | // GetUserKickTopic get the topic for kicking an user | [
"GetUserKickTopic",
"get",
"the",
"topic",
"for",
"kicking",
"an",
"user"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/nats_rpc_server.go#L120-L122 | train |
topfreegames/pitaya | cluster/nats_rpc_server.go | onSessionBind | func (ns *NatsRPCServer) onSessionBind(ctx context.Context, s *session.Session) error {
if ns.server.Frontend {
subu, err := ns.subscribeToUserMessages(s.UID(), ns.server.Type)
if err != nil {
return err
}
subk, err := ns.subscribeToUserKickChannel(s.UID(), ns.server.Type)
if err != nil {
return err
}
s.Subscriptions = []*nats.Subscription{subu, subk}
}
return nil
} | go | func (ns *NatsRPCServer) onSessionBind(ctx context.Context, s *session.Session) error {
if ns.server.Frontend {
subu, err := ns.subscribeToUserMessages(s.UID(), ns.server.Type)
if err != nil {
return err
}
subk, err := ns.subscribeToUserKickChannel(s.UID(), ns.server.Type)
if err != nil {
return err
}
s.Subscriptions = []*nats.Subscription{subu, subk}
}
return nil
} | [
"func",
"(",
"ns",
"*",
"NatsRPCServer",
")",
"onSessionBind",
"(",
"ctx",
"context",
".",
"Context",
",",
"s",
"*",
"session",
".",
"Session",
")",
"error",
"{",
"if",
"ns",
".",
"server",
".",
"Frontend",
"{",
"subu",
",",
"err",
":=",
"ns",
".",
"subscribeToUserMessages",
"(",
"s",
".",
"UID",
"(",
")",
",",
"ns",
".",
"server",
".",
"Type",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"subk",
",",
"err",
":=",
"ns",
".",
"subscribeToUserKickChannel",
"(",
"s",
".",
"UID",
"(",
")",
",",
"ns",
".",
"server",
".",
"Type",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"s",
".",
"Subscriptions",
"=",
"[",
"]",
"*",
"nats",
".",
"Subscription",
"{",
"subu",
",",
"subk",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // onSessionBind should be called on each session bind | [
"onSessionBind",
"should",
"be",
"called",
"on",
"each",
"session",
"bind"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/nats_rpc_server.go#L130-L143 | train |
topfreegames/pitaya | cluster/nats_rpc_server.go | Init | func (ns *NatsRPCServer) Init() error {
// TODO should we have concurrency here? it feels like we should
go ns.handleMessages()
conn, err := setupNatsConn(ns.connString, ns.appDieChan, nats.MaxReconnects(ns.maxReconnectionRetries))
if err != nil {
return err
}
ns.conn = conn
if ns.sub, err = ns.subscribe(getChannel(ns.server.Type, ns.server.ID)); err != nil {
return err
}
err = ns.subscribeToBindingsChannel()
if err != nil {
return err
}
// this handles remote messages
for i := 0; i < ns.config.GetInt("pitaya.concurrency.remote.service"); i++ {
go ns.processMessages(i)
}
session.OnSessionBind(ns.onSessionBind)
// this should be so fast that we shoudn't need concurrency
go ns.processPushes()
go ns.processSessionBindings()
go ns.processKick()
return nil
} | go | func (ns *NatsRPCServer) Init() error {
// TODO should we have concurrency here? it feels like we should
go ns.handleMessages()
conn, err := setupNatsConn(ns.connString, ns.appDieChan, nats.MaxReconnects(ns.maxReconnectionRetries))
if err != nil {
return err
}
ns.conn = conn
if ns.sub, err = ns.subscribe(getChannel(ns.server.Type, ns.server.ID)); err != nil {
return err
}
err = ns.subscribeToBindingsChannel()
if err != nil {
return err
}
// this handles remote messages
for i := 0; i < ns.config.GetInt("pitaya.concurrency.remote.service"); i++ {
go ns.processMessages(i)
}
session.OnSessionBind(ns.onSessionBind)
// this should be so fast that we shoudn't need concurrency
go ns.processPushes()
go ns.processSessionBindings()
go ns.processKick()
return nil
} | [
"func",
"(",
"ns",
"*",
"NatsRPCServer",
")",
"Init",
"(",
")",
"error",
"{",
"go",
"ns",
".",
"handleMessages",
"(",
")",
"\n",
"conn",
",",
"err",
":=",
"setupNatsConn",
"(",
"ns",
".",
"connString",
",",
"ns",
".",
"appDieChan",
",",
"nats",
".",
"MaxReconnects",
"(",
"ns",
".",
"maxReconnectionRetries",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"ns",
".",
"conn",
"=",
"conn",
"\n",
"if",
"ns",
".",
"sub",
",",
"err",
"=",
"ns",
".",
"subscribe",
"(",
"getChannel",
"(",
"ns",
".",
"server",
".",
"Type",
",",
"ns",
".",
"server",
".",
"ID",
")",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"err",
"=",
"ns",
".",
"subscribeToBindingsChannel",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"ns",
".",
"config",
".",
"GetInt",
"(",
"\"pitaya.concurrency.remote.service\"",
")",
";",
"i",
"++",
"{",
"go",
"ns",
".",
"processMessages",
"(",
"i",
")",
"\n",
"}",
"\n",
"session",
".",
"OnSessionBind",
"(",
"ns",
".",
"onSessionBind",
")",
"\n",
"go",
"ns",
".",
"processPushes",
"(",
")",
"\n",
"go",
"ns",
".",
"processSessionBindings",
"(",
")",
"\n",
"go",
"ns",
".",
"processKick",
"(",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // Init inits nats rpc server | [
"Init",
"inits",
"nats",
"rpc",
"server"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/cluster/nats_rpc_server.go#L308-L337 | train |
topfreegames/pitaya | route/route.go | NewRoute | func NewRoute(server, service, method string) *Route {
return &Route{server, service, method}
} | go | func NewRoute(server, service, method string) *Route {
return &Route{server, service, method}
} | [
"func",
"NewRoute",
"(",
"server",
",",
"service",
",",
"method",
"string",
")",
"*",
"Route",
"{",
"return",
"&",
"Route",
"{",
"server",
",",
"service",
",",
"method",
"}",
"\n",
"}"
] | // NewRoute creates a new route | [
"NewRoute",
"creates",
"a",
"new",
"route"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/route/route.go#L46-L48 | train |
topfreegames/pitaya | route/route.go | String | func (r *Route) String() string {
if r.SvType != "" {
return fmt.Sprintf("%s.%s.%s", r.SvType, r.Service, r.Method)
}
return r.Short()
} | go | func (r *Route) String() string {
if r.SvType != "" {
return fmt.Sprintf("%s.%s.%s", r.SvType, r.Service, r.Method)
}
return r.Short()
} | [
"func",
"(",
"r",
"*",
"Route",
")",
"String",
"(",
")",
"string",
"{",
"if",
"r",
".",
"SvType",
"!=",
"\"\"",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"%s.%s.%s\"",
",",
"r",
".",
"SvType",
",",
"r",
".",
"Service",
",",
"r",
".",
"Method",
")",
"\n",
"}",
"\n",
"return",
"r",
".",
"Short",
"(",
")",
"\n",
"}"
] | // String transforms the route into a string | [
"String",
"transforms",
"the",
"route",
"into",
"a",
"string"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/route/route.go#L51-L56 | train |
topfreegames/pitaya | route/route.go | Short | func (r *Route) Short() string {
return fmt.Sprintf("%s.%s", r.Service, r.Method)
} | go | func (r *Route) Short() string {
return fmt.Sprintf("%s.%s", r.Service, r.Method)
} | [
"func",
"(",
"r",
"*",
"Route",
")",
"Short",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"%s.%s\"",
",",
"r",
".",
"Service",
",",
"r",
".",
"Method",
")",
"\n",
"}"
] | // Short transforms the route into a string without the server type | [
"Short",
"transforms",
"the",
"route",
"into",
"a",
"string",
"without",
"the",
"server",
"type"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/route/route.go#L59-L61 | train |
topfreegames/pitaya | route/route.go | Decode | func Decode(route string) (*Route, error) {
r := strings.Split(route, ".")
for _, s := range r {
if strings.TrimSpace(s) == "" {
return nil, ErrRouteFieldCantEmpty
}
}
switch len(r) {
case 3:
return NewRoute(r[0], r[1], r[2]), nil
case 2:
return NewRoute("", r[0], r[1]), nil
default:
logger.Log.Errorf("invalid route: " + route)
return nil, ErrInvalidRoute
}
} | go | func Decode(route string) (*Route, error) {
r := strings.Split(route, ".")
for _, s := range r {
if strings.TrimSpace(s) == "" {
return nil, ErrRouteFieldCantEmpty
}
}
switch len(r) {
case 3:
return NewRoute(r[0], r[1], r[2]), nil
case 2:
return NewRoute("", r[0], r[1]), nil
default:
logger.Log.Errorf("invalid route: " + route)
return nil, ErrInvalidRoute
}
} | [
"func",
"Decode",
"(",
"route",
"string",
")",
"(",
"*",
"Route",
",",
"error",
")",
"{",
"r",
":=",
"strings",
".",
"Split",
"(",
"route",
",",
"\".\"",
")",
"\n",
"for",
"_",
",",
"s",
":=",
"range",
"r",
"{",
"if",
"strings",
".",
"TrimSpace",
"(",
"s",
")",
"==",
"\"\"",
"{",
"return",
"nil",
",",
"ErrRouteFieldCantEmpty",
"\n",
"}",
"\n",
"}",
"\n",
"switch",
"len",
"(",
"r",
")",
"{",
"case",
"3",
":",
"return",
"NewRoute",
"(",
"r",
"[",
"0",
"]",
",",
"r",
"[",
"1",
"]",
",",
"r",
"[",
"2",
"]",
")",
",",
"nil",
"\n",
"case",
"2",
":",
"return",
"NewRoute",
"(",
"\"\"",
",",
"r",
"[",
"0",
"]",
",",
"r",
"[",
"1",
"]",
")",
",",
"nil",
"\n",
"default",
":",
"logger",
".",
"Log",
".",
"Errorf",
"(",
"\"invalid route: \"",
"+",
"route",
")",
"\n",
"return",
"nil",
",",
"ErrInvalidRoute",
"\n",
"}",
"\n",
"}"
] | // Decode decodes the route | [
"Decode",
"decodes",
"the",
"route"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/route/route.go#L64-L80 | train |
topfreegames/pitaya | examples/demo/custom_metrics/services/room.go | SetCounter | func (*Room) SetCounter(
ctx context.Context,
arg *messages.SetCounterArg,
) (*messages.Response, error) {
counterMetricName := "my_counter"
for _, reporter := range pitaya.GetMetricsReporters() {
reporter.ReportCount(counterMetricName, map[string]string{
"tag1": arg.Tag1,
"tag2": arg.Tag2,
}, arg.Value)
}
return messages.OKResponse(), nil
} | go | func (*Room) SetCounter(
ctx context.Context,
arg *messages.SetCounterArg,
) (*messages.Response, error) {
counterMetricName := "my_counter"
for _, reporter := range pitaya.GetMetricsReporters() {
reporter.ReportCount(counterMetricName, map[string]string{
"tag1": arg.Tag1,
"tag2": arg.Tag2,
}, arg.Value)
}
return messages.OKResponse(), nil
} | [
"func",
"(",
"*",
"Room",
")",
"SetCounter",
"(",
"ctx",
"context",
".",
"Context",
",",
"arg",
"*",
"messages",
".",
"SetCounterArg",
",",
")",
"(",
"*",
"messages",
".",
"Response",
",",
"error",
")",
"{",
"counterMetricName",
":=",
"\"my_counter\"",
"\n",
"for",
"_",
",",
"reporter",
":=",
"range",
"pitaya",
".",
"GetMetricsReporters",
"(",
")",
"{",
"reporter",
".",
"ReportCount",
"(",
"counterMetricName",
",",
"map",
"[",
"string",
"]",
"string",
"{",
"\"tag1\"",
":",
"arg",
".",
"Tag1",
",",
"\"tag2\"",
":",
"arg",
".",
"Tag2",
",",
"}",
",",
"arg",
".",
"Value",
")",
"\n",
"}",
"\n",
"return",
"messages",
".",
"OKResponse",
"(",
")",
",",
"nil",
"\n",
"}"
] | // SetCounter sets custom my_counter | [
"SetCounter",
"sets",
"custom",
"my_counter"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/examples/demo/custom_metrics/services/room.go#L17-L31 | train |
topfreegames/pitaya | examples/demo/custom_metrics/services/room.go | SetGauge1 | func (*Room) SetGauge1(
ctx context.Context,
arg *messages.SetGaugeArg,
) (*messages.Response, error) {
counterMetricName := "my_gauge_1"
for _, reporter := range pitaya.GetMetricsReporters() {
reporter.ReportGauge(counterMetricName, map[string]string{
"tag1": arg.Tag,
}, arg.Value)
}
return messages.OKResponse(), nil
} | go | func (*Room) SetGauge1(
ctx context.Context,
arg *messages.SetGaugeArg,
) (*messages.Response, error) {
counterMetricName := "my_gauge_1"
for _, reporter := range pitaya.GetMetricsReporters() {
reporter.ReportGauge(counterMetricName, map[string]string{
"tag1": arg.Tag,
}, arg.Value)
}
return messages.OKResponse(), nil
} | [
"func",
"(",
"*",
"Room",
")",
"SetGauge1",
"(",
"ctx",
"context",
".",
"Context",
",",
"arg",
"*",
"messages",
".",
"SetGaugeArg",
",",
")",
"(",
"*",
"messages",
".",
"Response",
",",
"error",
")",
"{",
"counterMetricName",
":=",
"\"my_gauge_1\"",
"\n",
"for",
"_",
",",
"reporter",
":=",
"range",
"pitaya",
".",
"GetMetricsReporters",
"(",
")",
"{",
"reporter",
".",
"ReportGauge",
"(",
"counterMetricName",
",",
"map",
"[",
"string",
"]",
"string",
"{",
"\"tag1\"",
":",
"arg",
".",
"Tag",
",",
"}",
",",
"arg",
".",
"Value",
")",
"\n",
"}",
"\n",
"return",
"messages",
".",
"OKResponse",
"(",
")",
",",
"nil",
"\n",
"}"
] | // SetGauge1 sets custom my_gauge_1 | [
"SetGauge1",
"sets",
"custom",
"my_gauge_1"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/examples/demo/custom_metrics/services/room.go#L34-L47 | train |
topfreegames/pitaya | examples/demo/custom_metrics/services/room.go | SetSummary | func (*Room) SetSummary(
ctx context.Context,
arg *messages.SetSummaryArg,
) (*messages.Response, error) {
counterMetricName := "my_summary"
for _, reporter := range pitaya.GetMetricsReporters() {
reporter.ReportSummary(counterMetricName, map[string]string{
"tag1": arg.Tag,
}, arg.Value)
}
return messages.OKResponse(), nil
} | go | func (*Room) SetSummary(
ctx context.Context,
arg *messages.SetSummaryArg,
) (*messages.Response, error) {
counterMetricName := "my_summary"
for _, reporter := range pitaya.GetMetricsReporters() {
reporter.ReportSummary(counterMetricName, map[string]string{
"tag1": arg.Tag,
}, arg.Value)
}
return messages.OKResponse(), nil
} | [
"func",
"(",
"*",
"Room",
")",
"SetSummary",
"(",
"ctx",
"context",
".",
"Context",
",",
"arg",
"*",
"messages",
".",
"SetSummaryArg",
",",
")",
"(",
"*",
"messages",
".",
"Response",
",",
"error",
")",
"{",
"counterMetricName",
":=",
"\"my_summary\"",
"\n",
"for",
"_",
",",
"reporter",
":=",
"range",
"pitaya",
".",
"GetMetricsReporters",
"(",
")",
"{",
"reporter",
".",
"ReportSummary",
"(",
"counterMetricName",
",",
"map",
"[",
"string",
"]",
"string",
"{",
"\"tag1\"",
":",
"arg",
".",
"Tag",
",",
"}",
",",
"arg",
".",
"Value",
")",
"\n",
"}",
"\n",
"return",
"messages",
".",
"OKResponse",
"(",
")",
",",
"nil",
"\n",
"}"
] | // SetSummary sets custom my_summary | [
"SetSummary",
"sets",
"custom",
"my_summary"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/examples/demo/custom_metrics/services/room.go#L66-L79 | train |
topfreegames/pitaya | service/remote.go | NewRemoteService | func NewRemoteService(
rpcClient cluster.RPCClient,
rpcServer cluster.RPCServer,
sd cluster.ServiceDiscovery,
encoder codec.PacketEncoder,
serializer serialize.Serializer,
router *router.Router,
messageEncoder message.Encoder,
server *cluster.Server,
) *RemoteService {
return &RemoteService{
services: make(map[string]*component.Service),
rpcClient: rpcClient,
rpcServer: rpcServer,
encoder: encoder,
serviceDiscovery: sd,
serializer: serializer,
router: router,
messageEncoder: messageEncoder,
server: server,
remoteBindingListeners: make([]cluster.RemoteBindingListener, 0),
}
} | go | func NewRemoteService(
rpcClient cluster.RPCClient,
rpcServer cluster.RPCServer,
sd cluster.ServiceDiscovery,
encoder codec.PacketEncoder,
serializer serialize.Serializer,
router *router.Router,
messageEncoder message.Encoder,
server *cluster.Server,
) *RemoteService {
return &RemoteService{
services: make(map[string]*component.Service),
rpcClient: rpcClient,
rpcServer: rpcServer,
encoder: encoder,
serviceDiscovery: sd,
serializer: serializer,
router: router,
messageEncoder: messageEncoder,
server: server,
remoteBindingListeners: make([]cluster.RemoteBindingListener, 0),
}
} | [
"func",
"NewRemoteService",
"(",
"rpcClient",
"cluster",
".",
"RPCClient",
",",
"rpcServer",
"cluster",
".",
"RPCServer",
",",
"sd",
"cluster",
".",
"ServiceDiscovery",
",",
"encoder",
"codec",
".",
"PacketEncoder",
",",
"serializer",
"serialize",
".",
"Serializer",
",",
"router",
"*",
"router",
".",
"Router",
",",
"messageEncoder",
"message",
".",
"Encoder",
",",
"server",
"*",
"cluster",
".",
"Server",
",",
")",
"*",
"RemoteService",
"{",
"return",
"&",
"RemoteService",
"{",
"services",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"component",
".",
"Service",
")",
",",
"rpcClient",
":",
"rpcClient",
",",
"rpcServer",
":",
"rpcServer",
",",
"encoder",
":",
"encoder",
",",
"serviceDiscovery",
":",
"sd",
",",
"serializer",
":",
"serializer",
",",
"router",
":",
"router",
",",
"messageEncoder",
":",
"messageEncoder",
",",
"server",
":",
"server",
",",
"remoteBindingListeners",
":",
"make",
"(",
"[",
"]",
"cluster",
".",
"RemoteBindingListener",
",",
"0",
")",
",",
"}",
"\n",
"}"
] | // NewRemoteService creates and return a new RemoteService | [
"NewRemoteService",
"creates",
"and",
"return",
"a",
"new",
"RemoteService"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/service/remote.go#L64-L86 | train |
topfreegames/pitaya | service/remote.go | AddRemoteBindingListener | func (r *RemoteService) AddRemoteBindingListener(bindingListener cluster.RemoteBindingListener) {
r.remoteBindingListeners = append(r.remoteBindingListeners, bindingListener)
} | go | func (r *RemoteService) AddRemoteBindingListener(bindingListener cluster.RemoteBindingListener) {
r.remoteBindingListeners = append(r.remoteBindingListeners, bindingListener)
} | [
"func",
"(",
"r",
"*",
"RemoteService",
")",
"AddRemoteBindingListener",
"(",
"bindingListener",
"cluster",
".",
"RemoteBindingListener",
")",
"{",
"r",
".",
"remoteBindingListeners",
"=",
"append",
"(",
"r",
".",
"remoteBindingListeners",
",",
"bindingListener",
")",
"\n",
"}"
] | // AddRemoteBindingListener adds a listener | [
"AddRemoteBindingListener",
"adds",
"a",
"listener"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/service/remote.go#L122-L124 | train |
topfreegames/pitaya | service/remote.go | Call | func (r *RemoteService) Call(ctx context.Context, req *protos.Request) (*protos.Response, error) {
c, err := util.GetContextFromRequest(req, r.server.ID)
var res *protos.Response
if err != nil {
res = &protos.Response{
Error: &protos.Error{
Code: e.ErrInternalCode,
Msg: err.Error(),
},
}
} else {
res = processRemoteMessage(c, req, r)
}
if res.Error != nil {
err = errors.New(res.Error.Msg)
}
defer tracing.FinishSpan(c, err)
return res, nil
} | go | func (r *RemoteService) Call(ctx context.Context, req *protos.Request) (*protos.Response, error) {
c, err := util.GetContextFromRequest(req, r.server.ID)
var res *protos.Response
if err != nil {
res = &protos.Response{
Error: &protos.Error{
Code: e.ErrInternalCode,
Msg: err.Error(),
},
}
} else {
res = processRemoteMessage(c, req, r)
}
if res.Error != nil {
err = errors.New(res.Error.Msg)
}
defer tracing.FinishSpan(c, err)
return res, nil
} | [
"func",
"(",
"r",
"*",
"RemoteService",
")",
"Call",
"(",
"ctx",
"context",
".",
"Context",
",",
"req",
"*",
"protos",
".",
"Request",
")",
"(",
"*",
"protos",
".",
"Response",
",",
"error",
")",
"{",
"c",
",",
"err",
":=",
"util",
".",
"GetContextFromRequest",
"(",
"req",
",",
"r",
".",
"server",
".",
"ID",
")",
"\n",
"var",
"res",
"*",
"protos",
".",
"Response",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"res",
"=",
"&",
"protos",
".",
"Response",
"{",
"Error",
":",
"&",
"protos",
".",
"Error",
"{",
"Code",
":",
"e",
".",
"ErrInternalCode",
",",
"Msg",
":",
"err",
".",
"Error",
"(",
")",
",",
"}",
",",
"}",
"\n",
"}",
"else",
"{",
"res",
"=",
"processRemoteMessage",
"(",
"c",
",",
"req",
",",
"r",
")",
"\n",
"}",
"\n",
"if",
"res",
".",
"Error",
"!=",
"nil",
"{",
"err",
"=",
"errors",
".",
"New",
"(",
"res",
".",
"Error",
".",
"Msg",
")",
"\n",
"}",
"\n",
"defer",
"tracing",
".",
"FinishSpan",
"(",
"c",
",",
"err",
")",
"\n",
"return",
"res",
",",
"nil",
"\n",
"}"
] | // Call processes a remote call | [
"Call",
"processes",
"a",
"remote",
"call"
] | b92161d7a48c87a7759ac9cabcc23e7d56f9eebd | https://github.com/topfreegames/pitaya/blob/b92161d7a48c87a7759ac9cabcc23e7d56f9eebd/service/remote.go#L127-L147 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.