Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FinClip
ligase
提交
b06d1124
ligase
项目概览
FinClip
/
ligase
通知
33
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
ligase
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b06d1124
编写于
7月 17, 2017
作者:
M
Mark Haines
提交者:
GitHub
7月 17, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Factor out runTransaction to common code (#162)
上级
d3a29b78
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
58 addition
and
69 deletion
+58
-69
src/github.com/matrix-org/dendrite/clientapi/auth/auth.go
src/github.com/matrix-org/dendrite/clientapi/auth/auth.go
+7
-2
src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/storage.go
...ix-org/dendrite/clientapi/auth/storage/devices/storage.go
+3
-22
src/github.com/matrix-org/dendrite/common/httpapi.go
src/github.com/matrix-org/dendrite/common/httpapi.go
+3
-3
src/github.com/matrix-org/dendrite/common/sql.go
src/github.com/matrix-org/dendrite/common/sql.go
+41
-0
src/github.com/matrix-org/dendrite/federationsender/storage/storage.go
...m/matrix-org/dendrite/federationsender/storage/storage.go
+1
-20
src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go
...hub.com/matrix-org/dendrite/syncapi/storage/syncserver.go
+3
-22
未找到文件。
src/github.com/matrix-org/dendrite/clientapi/auth/auth.go
浏览文件 @
b06d1124
...
...
@@ -24,7 +24,6 @@ import (
"strings"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/util"
)
...
...
@@ -40,10 +39,16 @@ var UnknownDeviceID = "unknown-device"
// 32 bytes => 256 bits
var
tokenByteLength
=
32
// DeviceDatabase represents a device database.
type
DeviceDatabase
interface
{
// Lookup the device matching the given access token.
GetDeviceByAccessToken
(
token
string
)
(
*
authtypes
.
Device
,
error
)
}
// VerifyAccessToken verifies that an access token was supplied in the given HTTP request
// and returns the device it corresponds to. Returns resErr (an error response which can be
// sent to the client) if the token is invalid or there was a problem querying the database.
func
VerifyAccessToken
(
req
*
http
.
Request
,
deviceDB
*
devices
.
Database
)
(
device
*
authtypes
.
Device
,
resErr
*
util
.
JSONResponse
)
{
func
VerifyAccessToken
(
req
*
http
.
Request
,
deviceDB
Device
Database
)
(
device
*
authtypes
.
Device
,
resErr
*
util
.
JSONResponse
)
{
token
,
err
:=
extractAccessToken
(
req
)
if
err
!=
nil
{
resErr
=
&
util
.
JSONResponse
{
...
...
src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/storage.go
浏览文件 @
b06d1124
...
...
@@ -18,6 +18,7 @@ import (
"database/sql"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/gomatrixserverlib"
)
...
...
@@ -53,7 +54,7 @@ func (d *Database) GetDeviceByAccessToken(token string) (*authtypes.Device, erro
// an error will be returned.
// Returns the device on success.
func
(
d
*
Database
)
CreateDevice
(
localpart
,
deviceID
,
accessToken
string
)
(
dev
*
authtypes
.
Device
,
returnErr
error
)
{
returnErr
=
run
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
returnErr
=
common
.
With
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
var
err
error
// Revoke existing token for this device
if
err
=
d
.
devices
.
deleteDevice
(
txn
,
deviceID
,
localpart
);
err
!=
nil
{
...
...
@@ -74,30 +75,10 @@ func (d *Database) CreateDevice(localpart, deviceID, accessToken string) (dev *a
// If the device doesn't exist, it will not return an error
// If something went wrong during the deletion, it will return the SQL error
func
(
d
*
Database
)
RemoveDevice
(
deviceID
string
,
localpart
string
)
error
{
return
run
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
return
common
.
With
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
if
err
:=
d
.
devices
.
deleteDevice
(
txn
,
deviceID
,
localpart
);
err
!=
sql
.
ErrNoRows
{
return
err
}
return
nil
})
}
// TODO: factor out to common
func
runTransaction
(
db
*
sql
.
DB
,
fn
func
(
txn
*
sql
.
Tx
)
error
)
(
err
error
)
{
txn
,
err
:=
db
.
Begin
()
if
err
!=
nil
{
return
}
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
txn
.
Rollback
()
panic
(
r
)
}
else
if
err
!=
nil
{
txn
.
Rollback
()
}
else
{
err
=
txn
.
Commit
()
}
}()
err
=
fn
(
txn
)
return
}
src/github.com/matrix-org/dendrite/common/httpapi.go
浏览文件 @
b06d1124
package
common
import
(
"net/http"
"github.com/matrix-org/dendrite/clientapi/auth"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
"github.com/matrix-org/util"
"github.com/prometheus/client_golang/prometheus"
"net/http"
)
// MakeAuthAPI turns a util.JSONRequestHandler function into an http.Handler which checks the access token in the request.
func
MakeAuthAPI
(
metricsName
string
,
deviceDB
*
devices
.
Database
,
f
func
(
*
http
.
Request
,
*
authtypes
.
Device
)
util
.
JSONResponse
)
http
.
Handler
{
func
MakeAuthAPI
(
metricsName
string
,
deviceDB
auth
.
Device
Database
,
f
func
(
*
http
.
Request
,
*
authtypes
.
Device
)
util
.
JSONResponse
)
http
.
Handler
{
h
:=
util
.
NewJSONRequestHandler
(
func
(
req
*
http
.
Request
)
util
.
JSONResponse
{
device
,
resErr
:=
auth
.
VerifyAccessToken
(
req
,
deviceDB
)
if
resErr
!=
nil
{
...
...
src/github.com/matrix-org/dendrite/common/sql.go
0 → 100644
浏览文件 @
b06d1124
// Copyright 2017 Vector Creations Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package
common
import
(
"database/sql"
)
// WithTransaction runs a block of code passing in an SQL transaction
// If the code returns an error or panics then the transactions is rolledback
// Otherwise the transaction is committed.
func
WithTransaction
(
db
*
sql
.
DB
,
fn
func
(
txn
*
sql
.
Tx
)
error
)
(
err
error
)
{
txn
,
err
:=
db
.
Begin
()
if
err
!=
nil
{
return
}
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
txn
.
Rollback
()
panic
(
r
)
}
else
if
err
!=
nil
{
txn
.
Rollback
()
}
else
{
err
=
txn
.
Commit
()
}
}()
err
=
fn
(
txn
)
return
}
src/github.com/matrix-org/dendrite/federationsender/storage/storage.go
浏览文件 @
b06d1124
...
...
@@ -77,7 +77,7 @@ func (d *Database) UpdateRoom(
addHosts
[]
types
.
JoinedHost
,
removeHosts
[]
string
,
)
(
joinedHosts
[]
types
.
JoinedHost
,
err
error
)
{
err
=
run
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
err
=
common
.
With
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
if
err
=
d
.
insertRoom
(
txn
,
roomID
);
err
!=
nil
{
return
err
}
...
...
@@ -105,22 +105,3 @@ func (d *Database) UpdateRoom(
})
return
}
func
runTransaction
(
db
*
sql
.
DB
,
fn
func
(
txn
*
sql
.
Tx
)
error
)
(
err
error
)
{
txn
,
err
:=
db
.
Begin
()
if
err
!=
nil
{
return
}
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
txn
.
Rollback
()
panic
(
r
)
}
else
if
err
!=
nil
{
txn
.
Rollback
()
}
else
{
err
=
txn
.
Commit
()
}
}()
err
=
fn
(
txn
)
return
}
src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go
浏览文件 @
b06d1124
...
...
@@ -92,7 +92,7 @@ func (d *SyncServerDatabase) Events(eventIDs []string) ([]gomatrixserverlib.Even
func
(
d
*
SyncServerDatabase
)
WriteEvent
(
ev
*
gomatrixserverlib
.
Event
,
addStateEvents
[]
gomatrixserverlib
.
Event
,
addStateEventIDs
,
removeStateEventIDs
[]
string
,
)
(
streamPos
types
.
StreamPosition
,
returnErr
error
)
{
returnErr
=
run
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
returnErr
=
common
.
With
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
var
err
error
pos
,
err
:=
d
.
events
.
insertEvent
(
txn
,
ev
,
addStateEventIDs
,
removeStateEventIDs
)
if
err
!=
nil
{
...
...
@@ -162,7 +162,7 @@ func (d *SyncServerDatabase) SyncStreamPosition() (types.StreamPosition, error)
// IncrementalSync returns all the data needed in order to create an incremental sync response.
func
(
d
*
SyncServerDatabase
)
IncrementalSync
(
userID
string
,
fromPos
,
toPos
types
.
StreamPosition
,
numRecentEventsPerRoom
int
)
(
res
*
types
.
Response
,
returnErr
error
)
{
returnErr
=
run
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
returnErr
=
common
.
With
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
// Work out which rooms to return in the response. This is done by getting not only the currently
// joined rooms, but also which rooms have membership transitions for this user between the 2 stream positions.
// This works out what the 'state' key should be for each room as well as which membership block
...
...
@@ -223,7 +223,7 @@ func (d *SyncServerDatabase) CompleteSync(userID string, numRecentEventsPerRoom
// a consistent view of the database throughout. This includes extracting the sync stream position.
// This does have the unfortunate side-effect that all the matrixy logic resides in this function,
// but it's better to not hide the fact that this is being done in a transaction.
returnErr
=
run
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
returnErr
=
common
.
With
Transaction
(
d
.
db
,
func
(
txn
*
sql
.
Tx
)
error
{
// Get the current stream position which we will base the sync response on.
id
,
err
:=
d
.
events
.
selectMaxID
(
txn
)
if
err
!=
nil
{
...
...
@@ -479,22 +479,3 @@ func getMembershipFromEvent(ev *gomatrixserverlib.Event, userID string) string {
}
return
""
}
func
runTransaction
(
db
*
sql
.
DB
,
fn
func
(
txn
*
sql
.
Tx
)
error
)
(
err
error
)
{
txn
,
err
:=
db
.
Begin
()
if
err
!=
nil
{
return
}
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
txn
.
Rollback
()
panic
(
r
)
}
else
if
err
!=
nil
{
txn
.
Rollback
()
}
else
{
err
=
txn
.
Commit
()
}
}()
err
=
fn
(
txn
)
return
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录