diff --git a/appservice/consumers/roomserver.go b/appservice/consumers/roomserver.go index 0b251d43d..b2ca4eca2 100644 --- a/appservice/consumers/roomserver.go +++ b/appservice/consumers/roomserver.go @@ -16,7 +16,6 @@ package consumers import ( "context" - "encoding/json" "github.com/matrix-org/dendrite/appservice/storage" "github.com/matrix-org/dendrite/appservice/types" @@ -26,9 +25,12 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/Shopify/sarama" + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // OutputRoomEventConsumer consumes events that originated in the room server. type OutputRoomEventConsumer struct { roomServerConsumer *internal.ContinualConsumer diff --git a/appservice/inthttp/server.go b/appservice/inthttp/server.go index 009b7b5db..298415061 100644 --- a/appservice/inthttp/server.go +++ b/appservice/inthttp/server.go @@ -1,15 +1,17 @@ package inthttp import ( - "encoding/json" "net/http" "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/util" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // AddRoutes adds the AppServiceQueryAPI handlers to the http.ServeMux. func AddRoutes(a api.AppServiceQueryAPI, internalAPIMux *mux.Router) { internalAPIMux.Handle( diff --git a/appservice/storage/postgres/appservice_events_table.go b/appservice/storage/postgres/appservice_events_table.go index a95be6b8a..3ce190315 100644 --- a/appservice/storage/postgres/appservice_events_table.go +++ b/appservice/storage/postgres/appservice_events_table.go @@ -18,10 +18,11 @@ package postgres import ( "context" "database/sql" - "encoding/json" "time" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) @@ -64,6 +65,8 @@ const ( invalidTxnID = -2 ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type eventsStatements struct { selectEventsByApplicationServiceIDStmt *sql.Stmt countEventsByApplicationServiceIDStmt *sql.Stmt diff --git a/appservice/storage/sqlite3/appservice_events_table.go b/appservice/storage/sqlite3/appservice_events_table.go index 34b4859ea..b35f58b3d 100644 --- a/appservice/storage/sqlite3/appservice_events_table.go +++ b/appservice/storage/sqlite3/appservice_events_table.go @@ -18,11 +18,12 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "time" "github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) @@ -65,6 +66,8 @@ const ( invalidTxnID = -2 ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type eventsStatements struct { db *sql.DB writer sqlutil.Writer diff --git a/appservice/workers/transaction_scheduler.go b/appservice/workers/transaction_scheduler.go index 6528fc1b6..ac8fd8d91 100644 --- a/appservice/workers/transaction_scheduler.go +++ b/appservice/workers/transaction_scheduler.go @@ -17,7 +17,6 @@ package workers import ( "bytes" "context" - "encoding/json" "fmt" "math" "net/http" @@ -28,6 +27,8 @@ import ( "github.com/matrix-org/dendrite/appservice/types" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) @@ -36,6 +37,8 @@ var ( transactionBatchSize = 50 // Timeout for sending a single transaction to an application service. transactionTimeout = time.Second * 60 + + json = jsoniter.ConfigCompatibleWithStandardLibrary ) // SetupTransactionWorkers spawns a separate goroutine for each application diff --git a/clientapi/auth/user_interactive.go b/clientapi/auth/user_interactive.go index 839637351..0385cf8c7 100644 --- a/clientapi/auth/user_interactive.go +++ b/clientapi/auth/user_interactive.go @@ -16,17 +16,20 @@ package auth import ( "context" - "encoding/json" "net/http" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/util" + + jsoniter "github.com/json-iterator/go" "github.com/sirupsen/logrus" "github.com/tidwall/gjson" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // Type represents an auth type // https://matrix.org/docs/spec/client_server/r0.6.1#authentication-types type Type interface { diff --git a/clientapi/auth/user_interactive_test.go b/clientapi/auth/user_interactive_test.go index 0b7df3545..4bd74bf6a 100644 --- a/clientapi/auth/user_interactive_test.go +++ b/clientapi/auth/user_interactive_test.go @@ -2,7 +2,6 @@ package auth import ( "context" - "encoding/json" "fmt" "testing" @@ -62,7 +61,7 @@ func TestUserInteractivePasswordLogin(t *testing.T) { UserID: fmt.Sprintf("@alice:%s", serverName), } // valid password requests - testCases := []json.RawMessage{ + testCases := [][]byte{ // deprecated form []byte(`{ "auth": { @@ -101,7 +100,7 @@ func TestUserInteractivePasswordBadLogin(t *testing.T) { } // invalid password requests testCases := []struct { - body json.RawMessage + body []byte wantRes util.JSONResponse }{ { diff --git a/clientapi/httputil/httputil.go b/clientapi/httputil/httputil.go index 29d7b0b37..030670bd2 100644 --- a/clientapi/httputil/httputil.go +++ b/clientapi/httputil/httputil.go @@ -15,15 +15,18 @@ package httputil import ( - "encoding/json" "io/ioutil" "net/http" "unicode/utf8" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/util" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // UnmarshalJSONRequest into the given interface pointer. Returns an error JSON response if // there was a problem unmarshalling. Calling this function consumes the request body. func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse { diff --git a/clientapi/jsonerror/jsonerror_test.go b/clientapi/jsonerror/jsonerror_test.go index 9f3754cbc..70b30f226 100644 --- a/clientapi/jsonerror/jsonerror_test.go +++ b/clientapi/jsonerror/jsonerror_test.go @@ -15,10 +15,13 @@ package jsonerror import ( - "encoding/json" "testing" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + func TestLimitExceeded(t *testing.T) { e := LimitExceeded("too fast", 5000) jsonBytes, err := json.Marshal(&e) diff --git a/clientapi/producers/syncapi.go b/clientapi/producers/syncapi.go index 6ab8eef28..76ce46a53 100644 --- a/clientapi/producers/syncapi.go +++ b/clientapi/producers/syncapi.go @@ -15,13 +15,15 @@ package producers import ( - "encoding/json" + "github.com/matrix-org/dendrite/internal/eventutil" "github.com/Shopify/sarama" - "github.com/matrix-org/dendrite/internal/eventutil" + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // SyncAPIProducer produces events for the sync API server to consume type SyncAPIProducer struct { Topic string diff --git a/clientapi/routing/account_data.go b/clientapi/routing/account_data.go index 22e635139..6290c95fc 100644 --- a/clientapi/routing/account_data.go +++ b/clientapi/routing/account_data.go @@ -15,7 +15,6 @@ package routing import ( - "encoding/json" "fmt" "io/ioutil" "net/http" @@ -53,7 +52,7 @@ func GetAccountData( return util.ErrorResponse(fmt.Errorf("userAPI.QueryAccountData: %w", err)) } - var data json.RawMessage + var data []byte var ok bool if roomID != "" { data, ok = dataRes.RoomAccountData[roomID][dataType] @@ -118,7 +117,7 @@ func SaveAccountData( UserID: userID, DataType: dataType, RoomID: roomID, - AccountData: json.RawMessage(body), + AccountData: []byte(body), } dataRes := api.InputAccountDataResponse{} if err := userAPI.InputAccountData(req.Context(), &dataReq, &dataRes); err != nil { diff --git a/clientapi/routing/createroom.go b/clientapi/routing/createroom.go index 5a2ffea34..405a2f7cf 100644 --- a/clientapi/routing/createroom.go +++ b/clientapi/routing/createroom.go @@ -15,7 +15,6 @@ package routing import ( - "encoding/json" "fmt" "net/http" "strings" diff --git a/clientapi/routing/keys.go b/clientapi/routing/keys.go index ba03a352f..118409f08 100644 --- a/clientapi/routing/keys.go +++ b/clientapi/routing/keys.go @@ -15,7 +15,6 @@ package routing import ( - "encoding/json" "net/http" "time" @@ -27,8 +26,8 @@ import ( ) type uploadKeysRequest struct { - DeviceKeys json.RawMessage `json:"device_keys"` - OneTimeKeys map[string]json.RawMessage `json:"one_time_keys"` + DeviceKeys []byte `json:"device_keys"` + OneTimeKeys map[string][]byte `json:"one_time_keys"` } func UploadKeys(req *http.Request, keyAPI api.KeyInternalAPI, device *userapi.Device) util.JSONResponse { diff --git a/clientapi/routing/memberships.go b/clientapi/routing/memberships.go index 6ddcf1be3..64e278e8f 100644 --- a/clientapi/routing/memberships.go +++ b/clientapi/routing/memberships.go @@ -15,7 +15,6 @@ package routing import ( - "encoding/json" "net/http" "github.com/matrix-org/dendrite/clientapi/jsonerror" diff --git a/clientapi/routing/register.go b/clientapi/routing/register.go index 614e19d50..f1d32f8dd 100644 --- a/clientapi/routing/register.go +++ b/clientapi/routing/register.go @@ -19,7 +19,6 @@ import ( "context" "crypto/hmac" "crypto/sha1" - "encoding/json" "errors" "fmt" "io/ioutil" diff --git a/clientapi/routing/room_tagging.go b/clientapi/routing/room_tagging.go index c683cc949..03b91a5a2 100644 --- a/clientapi/routing/room_tagging.go +++ b/clientapi/routing/room_tagging.go @@ -15,7 +15,6 @@ package routing import ( - "encoding/json" "net/http" "github.com/sirupsen/logrus" @@ -205,7 +204,7 @@ func saveTagData( UserID: userID, RoomID: roomID, DataType: "m.tag", - AccountData: json.RawMessage(newTagData), + AccountData: []byte(newTagData), } dataRes := api.InputAccountDataResponse{} return userAPI.InputAccountData(req.Context(), &dataReq, &dataRes) diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index 8dbfc551d..f88a066dd 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -15,11 +15,9 @@ package routing import ( - "encoding/json" "net/http" "strings" - "github.com/gorilla/mux" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/api" "github.com/matrix-org/dendrite/clientapi/auth" @@ -37,8 +35,13 @@ import ( "github.com/matrix-org/dendrite/userapi/storage/accounts" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client // to clients which need to make outbound HTTP requests. // @@ -480,7 +483,7 @@ func Setup( r0mux.Handle("/pushrules/", httputil.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse { // TODO: Implement push rules API - res := json.RawMessage(`{ + res := []byte(`{ "global": { "content": [], "override": [], diff --git a/clientapi/routing/sendtodevice.go b/clientapi/routing/sendtodevice.go index 768e8e0e7..cfb8fe272 100644 --- a/clientapi/routing/sendtodevice.go +++ b/clientapi/routing/sendtodevice.go @@ -13,7 +13,6 @@ package routing import ( - "encoding/json" "net/http" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -39,7 +38,7 @@ func SendToDevice( } var httpReq struct { - Messages map[string]map[string]json.RawMessage `json:"messages"` + Messages map[string]map[string][]byte `json:"messages"` } resErr := httputil.UnmarshalJSONRequest(req, &httpReq) if resErr != nil { diff --git a/clientapi/routing/state.go b/clientapi/routing/state.go index 57014bc3b..367acf92e 100644 --- a/clientapi/routing/state.go +++ b/clientapi/routing/state.go @@ -16,7 +16,6 @@ package routing import ( "context" - "encoding/json" "fmt" "net/http" @@ -30,8 +29,8 @@ import ( type stateEventInStateResp struct { gomatrixserverlib.ClientEvent - PrevContent json.RawMessage `json:"prev_content,omitempty"` - ReplacesState string `json:"replaces_state,omitempty"` + PrevContent []byte `json:"prev_content,omitempty"` + ReplacesState string `json:"replaces_state,omitempty"` } // OnIncomingStateRequest is called when a client makes a /rooms/{roomID}/state diff --git a/clientapi/threepid/invites.go b/clientapi/threepid/invites.go index 53cd6b8ca..7ea0010b4 100644 --- a/clientapi/threepid/invites.go +++ b/clientapi/threepid/invites.go @@ -16,7 +16,6 @@ package threepid import ( "context" - "encoding/json" "errors" "fmt" "net/http" diff --git a/clientapi/threepid/threepid.go b/clientapi/threepid/threepid.go index 2f817ef42..61c316711 100644 --- a/clientapi/threepid/threepid.go +++ b/clientapi/threepid/threepid.go @@ -16,7 +16,6 @@ package threepid import ( "context" - "encoding/json" "errors" "fmt" "net/http" @@ -25,8 +24,12 @@ import ( "strings" "github.com/matrix-org/dendrite/setup/config" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // EmailAssociationRequest represents the request defined at https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-register-email-requesttoken type EmailAssociationRequest struct { IDServer string `json:"id_server"` diff --git a/cmd/create-room-events/main.go b/cmd/create-room-events/main.go index 23b44193a..9e64491c4 100644 --- a/cmd/create-room-events/main.go +++ b/cmd/create-room-events/main.go @@ -18,7 +18,6 @@ package main import ( "encoding/base64" - "encoding/json" "flag" "fmt" "os" @@ -27,6 +26,8 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" "golang.org/x/crypto/ed25519" ) @@ -48,6 +49,8 @@ var ( messageCount = flag.Int("message-count", 10, "The number of m.room.messsage events to generate") format = flag.String("Format", "InputRoomEvent", "The output format to use for the messages: InputRoomEvent or Event") ver = flag.String("version", string(gomatrixserverlib.RoomVersionV1), "Room version to generate events as") + + json = jsoniter.ConfigCompatibleWithStandardLibrary ) // By default we use a private key of 0. diff --git a/cmd/dendrite-demo-libp2p/publicrooms.go b/cmd/dendrite-demo-libp2p/publicrooms.go index 96e8ab5e1..120327a38 100644 --- a/cmd/dendrite-demo-libp2p/publicrooms.go +++ b/cmd/dendrite-demo-libp2p/publicrooms.go @@ -16,7 +16,6 @@ package main import ( "context" - "encoding/json" "fmt" "sync" "sync/atomic" @@ -24,6 +23,7 @@ import ( roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" + jsoniter "github.com/json-iterator/go" pubsub "github.com/libp2p/go-libp2p-pubsub" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" @@ -31,6 +31,8 @@ import ( const MaintenanceInterval = time.Second * 10 +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type discoveredRoom struct { time time.Time room gomatrixserverlib.PublicRoom diff --git a/cmd/dendrite-demo-yggdrasil/yggconn/node.go b/cmd/dendrite-demo-yggdrasil/yggconn/node.go index c036c0d87..77d59e7a2 100644 --- a/cmd/dendrite-demo-yggdrasil/yggconn/node.go +++ b/cmd/dendrite-demo-yggdrasil/yggconn/node.go @@ -19,7 +19,6 @@ import ( "crypto/ed25519" "crypto/tls" "encoding/hex" - "encoding/json" "fmt" "io/ioutil" "log" @@ -39,8 +38,11 @@ import ( "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" gologme "github.com/gologme/log" + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type Node struct { core *yggdrasil.Core config *yggdrasilconfig.NodeConfig diff --git a/cmd/furl/main.go b/cmd/furl/main.go index 3955ef0cd..b13a2b773 100644 --- a/cmd/furl/main.go +++ b/cmd/furl/main.go @@ -5,7 +5,6 @@ import ( "bytes" "context" "crypto/ed25519" - "encoding/json" "encoding/pem" "flag" "fmt" @@ -14,12 +13,16 @@ import ( "os" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" ) var requestFrom = flag.String("from", "", "the server name that the request should originate from") var requestKey = flag.String("key", "matrix_key.pem", "the private key to use when signing the request") var requestPost = flag.Bool("post", false, "send a POST request instead of GET (pipe input into stdin or type followed by Ctrl-D)") +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // nolint:gocyclo func main() { flag.Parse() diff --git a/cmd/roomserver-integration-tests/main.go b/cmd/roomserver-integration-tests/main.go index ff3f06b6e..e3bb30021 100644 --- a/cmd/roomserver-integration-tests/main.go +++ b/cmd/roomserver-integration-tests/main.go @@ -18,22 +18,21 @@ import ( "context" "fmt" "io/ioutil" + "net/http" "os" "os/exec" "path/filepath" "strings" "time" - "encoding/json" - - "net/http" - "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/test" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/inthttp" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" ) var ( @@ -55,6 +54,8 @@ var ( testDatabaseName = defaulting(os.Getenv("DATABASE_NAME"), "roomserver_test") // The postgres connection config for connecting to the test database. testDatabase = defaulting(os.Getenv("DATABASE"), fmt.Sprintf("dbname=%s binary_parameters=yes", testDatabaseName)) + + json = jsoniter.ConfigCompatibleWithStandardLibrary ) var exe = test.KafkaExecutor{ diff --git a/cmd/syncserver-integration-tests/main.go b/cmd/syncserver-integration-tests/main.go index 332bde10e..4c52cf7e2 100644 --- a/cmd/syncserver-integration-tests/main.go +++ b/cmd/syncserver-integration-tests/main.go @@ -15,7 +15,6 @@ package main import ( - "encoding/json" "fmt" "io/ioutil" "net/http" @@ -28,6 +27,8 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" ) var ( @@ -51,6 +52,8 @@ var ( testDatabaseName = test.Defaulting(os.Getenv("DATABASE_NAME"), "syncserver_test") // The postgres connection config for connecting to the test database. testDatabase = test.Defaulting(os.Getenv("DATABASE"), fmt.Sprintf("dbname=%s sslmode=disable binary_parameters=yes", testDatabaseName)) + + json = jsoniter.ConfigCompatibleWithStandardLibrary ) const inputTopic = "syncserverInput" diff --git a/eduserver/api/wrapper.go b/eduserver/api/wrapper.go index 7907f4d39..426740df5 100644 --- a/eduserver/api/wrapper.go +++ b/eduserver/api/wrapper.go @@ -16,12 +16,14 @@ package api import ( "context" - "encoding/json" "time" + jsoniter "github.com/json-iterator/go" "github.com/matrix-org/gomatrixserverlib" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // SendTyping sends a typing event to EDU server func SendTyping( ctx context.Context, eduAPI EDUServerInputAPI, userID, roomID string, diff --git a/eduserver/input/input.go b/eduserver/input/input.go index c54fb9de8..035648dda 100644 --- a/eduserver/input/input.go +++ b/eduserver/input/input.go @@ -18,10 +18,10 @@ package input import ( "context" - "encoding/json" "time" "github.com/Shopify/sarama" + jsoniter "github.com/json-iterator/go" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/eduserver/cache" userapi "github.com/matrix-org/dendrite/userapi/api" @@ -29,6 +29,8 @@ import ( "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // EDUServerInputAPI implements api.EDUServerInputAPI type EDUServerInputAPI struct { // Cache to store the current typing members in each room. diff --git a/eduserver/inthttp/server.go b/eduserver/inthttp/server.go index a34943750..06cc0a4ab 100644 --- a/eduserver/inthttp/server.go +++ b/eduserver/inthttp/server.go @@ -1,15 +1,17 @@ package inthttp import ( - "encoding/json" "net/http" "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/util" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // AddRoutes adds the EDUServerInputAPI handlers to the http.ServeMux. func AddRoutes(t api.EDUServerInputAPI, internalAPIMux *mux.Router) { internalAPIMux.Handle(EDUServerInputTypingEventPath, diff --git a/federationapi/routing/backfill.go b/federationapi/routing/backfill.go index 31005209f..4cebb47fc 100644 --- a/federationapi/routing/backfill.go +++ b/federationapi/routing/backfill.go @@ -15,7 +15,7 @@ package routing import ( - "encoding/json" + stdjson "encoding/json" "fmt" "net/http" "strconv" @@ -102,7 +102,7 @@ func Backfill( } } - eventJSONs := []json.RawMessage{} + eventJSONs := []stdjson.RawMessage{} for _, e := range gomatrixserverlib.ReverseTopologicalOrdering( evs, gomatrixserverlib.TopologicalOrderByPrevEvents, diff --git a/federationapi/routing/devices.go b/federationapi/routing/devices.go index 07862451f..b2d991d4c 100644 --- a/federationapi/routing/devices.go +++ b/federationapi/routing/devices.go @@ -13,7 +13,6 @@ package routing import ( - "encoding/json" "net/http" "github.com/matrix-org/dendrite/clientapi/jsonerror" diff --git a/federationapi/routing/events.go b/federationapi/routing/events.go index 312ef9f8e..6e04dfba3 100644 --- a/federationapi/routing/events.go +++ b/federationapi/routing/events.go @@ -16,7 +16,7 @@ package routing import ( "context" - "encoding/json" + stdjson "encoding/json" "net/http" "time" @@ -45,7 +45,7 @@ func GetEvent( return util.JSONResponse{Code: http.StatusOK, JSON: gomatrixserverlib.Transaction{ Origin: origin, OriginServerTS: gomatrixserverlib.AsTimestamp(time.Now()), - PDUs: []json.RawMessage{ + PDUs: []stdjson.RawMessage{ event.JSON(), }, }} diff --git a/federationapi/routing/invite.go b/federationapi/routing/invite.go index 8795118ee..51202c4d5 100644 --- a/federationapi/routing/invite.go +++ b/federationapi/routing/invite.go @@ -16,7 +16,6 @@ package routing import ( "context" - "encoding/json" "fmt" "net/http" diff --git a/federationapi/routing/keys.go b/federationapi/routing/keys.go index 1f39094bc..591cb8aeb 100644 --- a/federationapi/routing/keys.go +++ b/federationapi/routing/keys.go @@ -15,7 +15,6 @@ package routing import ( - "encoding/json" "net/http" "time" @@ -184,9 +183,9 @@ func NotaryKeys( } var response struct { - ServerKeys []json.RawMessage `json:"server_keys"` + ServerKeys [][]byte `json:"server_keys"` } - response.ServerKeys = []json.RawMessage{} + response.ServerKeys = [][]byte{} for serverName := range req.ServerKeys { var keys *gomatrixserverlib.ServerKeys diff --git a/federationapi/routing/missingevents.go b/federationapi/routing/missingevents.go index f79a2d2d8..4f77158df 100644 --- a/federationapi/routing/missingevents.go +++ b/federationapi/routing/missingevents.go @@ -13,7 +13,6 @@ package routing import ( - "encoding/json" "net/http" "github.com/matrix-org/dendrite/clientapi/jsonerror" diff --git a/federationapi/routing/routing.go b/federationapi/routing/routing.go index c957e26d0..04b84b84c 100644 --- a/federationapi/routing/routing.go +++ b/federationapi/routing/routing.go @@ -17,7 +17,6 @@ package routing import ( "net/http" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/jsonerror" eduserverAPI "github.com/matrix-org/dendrite/eduserver/api" federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api" @@ -28,8 +27,13 @@ import ( userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // Setup registers HTTP handlers with the given ServeMux. // The provided publicAPIMux MUST have `UseEncodedPath()` enabled or else routes will incorrectly // path unescape twice (once from the router, once from MakeFedAPI). We need to have this enabled diff --git a/federationapi/routing/send.go b/federationapi/routing/send.go index f50b9c3d6..c65ebed7b 100644 --- a/federationapi/routing/send.go +++ b/federationapi/routing/send.go @@ -17,7 +17,7 @@ package routing import ( "context" "database/sql" - "encoding/json" + stdjson "encoding/json" "fmt" "net/http" "sync" @@ -56,7 +56,7 @@ func Send( } var txnEvents struct { - PDUs []json.RawMessage `json:"pdus"` + PDUs []stdjson.RawMessage `json:"pdus"` EDUs []gomatrixserverlib.EDU `json:"edus"` } diff --git a/federationapi/routing/send_test.go b/federationapi/routing/send_test.go index 8bdf54c4a..6c00a059c 100644 --- a/federationapi/routing/send_test.go +++ b/federationapi/routing/send_test.go @@ -2,7 +2,7 @@ package routing import ( "context" - "encoding/json" + stdjson "encoding/json" "fmt" "reflect" "testing" @@ -21,7 +21,7 @@ const ( var ( testRoomVersion = gomatrixserverlib.RoomVersionV1 - testData = []json.RawMessage{ + testData = [][]byte{ []byte(`{"auth_events":[],"content":{"creator":"@userid:kaer.morhen"},"depth":0,"event_id":"$0ok8ynDp7kjc95e3:kaer.morhen","hashes":{"sha256":"17kPoH+h0Dk4Omn7Sus0qMb6+oGcf+CZFEgDhv7UKWs"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"jP4a04f5/F10Pw95FPpdCyKAO44JOwUQ/MZOOeA/RTU1Dn+AHPMzGSaZnuGjRr/xQuADt+I3ctb5ZQfLKNzHDw"}},"state_key":"","type":"m.room.create"}`), []byte(`{"auth_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}]],"content":{"membership":"join"},"depth":1,"event_id":"$LEwEu0kxrtu5fOiS:kaer.morhen","hashes":{"sha256":"B7M88PhXf3vd1LaFtjQutFu4x/w7fHD28XKZ4sAsJTo"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}]],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"p2vqmuJn7ZBRImctSaKbXCAxCcBlIjPH9JHte1ouIUGy84gpu4eLipOvSBCLL26hXfC0Zrm4WUto6Hr+ohdrCg"}},"state_key":"@userid:kaer.morhen","type":"m.room.member"}`), []byte(`{"auth_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}],["$LEwEu0kxrtu5fOiS:kaer.morhen",{"sha256":"1aKajq6DWHru1R1HJjvdWMEavkJJHGaTmPvfuERUXaA"}]],"content":{"join_rule":"public"},"depth":2,"event_id":"$SMHlqUrNhhBBRLeN:kaer.morhen","hashes":{"sha256":"vIuJQvmMjrGxshAkj1SXe0C4RqvMbv4ZADDw9pFCWqQ"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[["$LEwEu0kxrtu5fOiS:kaer.morhen",{"sha256":"1aKajq6DWHru1R1HJjvdWMEavkJJHGaTmPvfuERUXaA"}]],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"hBMsb3Qppo3RaqqAl4JyTgaiWEbW5hlckATky6PrHun+F3YM203TzG7w9clwuQU5F5pZoB1a6nw+to0hN90FAw"}},"state_key":"","type":"m.room.join_rules"}`), @@ -362,7 +362,7 @@ func (c *txnFedClient) LookupMissingEvents(ctx context.Context, s gomatrixserver return c.getMissingEvents(missing) } -func mustCreateTransaction(rsAPI api.RoomserverInternalAPI, fedClient txnFederationClient, pdus []json.RawMessage) *txnReq { +func mustCreateTransaction(rsAPI api.RoomserverInternalAPI, fedClient txnFederationClient, pdus []stdjson.RawMessage) *txnReq { t := &txnReq{ rsAPI: rsAPI, eduAPI: &testEDUProducer{}, @@ -445,7 +445,7 @@ func TestBasicTransaction(t *testing.T) { } }, } - pdus := []json.RawMessage{ + pdus := []stdjson.RawMessage{ testData[len(testData)-1], // a message event } txn := mustCreateTransaction(rsAPI, &txnFedClient{}, pdus) @@ -465,7 +465,7 @@ func TestTransactionFailAuthChecks(t *testing.T) { } }, } - pdus := []json.RawMessage{ + pdus := []stdjson.RawMessage{ testData[len(testData)-1], // a message event } txn := mustCreateTransaction(rsAPI, &txnFedClient{}, pdus) @@ -550,7 +550,7 @@ func TestTransactionFetchMissingPrevEvents(t *testing.T) { }, } - pdus := []json.RawMessage{ + pdus := []stdjson.RawMessage{ inputEvent.JSON(), } txn := mustCreateTransaction(rsAPI, cli, pdus) @@ -713,7 +713,7 @@ func TestTransactionFetchMissingStateByStateIDs(t *testing.T) { // /event for event B returns it getEvent: map[string]gomatrixserverlib.Transaction{ eventB.EventID(): { - PDUs: []json.RawMessage{ + PDUs: []stdjson.RawMessage{ eventB.JSON(), }, }, @@ -735,7 +735,7 @@ func TestTransactionFetchMissingStateByStateIDs(t *testing.T) { }, } - pdus := []json.RawMessage{ + pdus := []stdjson.RawMessage{ eventD.JSON(), } txn := mustCreateTransaction(rsAPI, cli, pdus) diff --git a/federationapi/routing/threepid.go b/federationapi/routing/threepid.go index 5ba28881c..7a215d671 100644 --- a/federationapi/routing/threepid.go +++ b/federationapi/routing/threepid.go @@ -16,7 +16,6 @@ package routing import ( "context" - "encoding/json" "errors" "net/http" "time" diff --git a/federationsender/consumers/eduserver.go b/federationsender/consumers/eduserver.go index 6d11eb88a..4c81f7680 100644 --- a/federationsender/consumers/eduserver.go +++ b/federationsender/consumers/eduserver.go @@ -16,10 +16,9 @@ package consumers import ( "context" - "encoding/json" + stdjson "encoding/json" "fmt" - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/federationsender/queue" "github.com/matrix-org/dendrite/federationsender/storage" @@ -27,9 +26,14 @@ import ( "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + "github.com/Shopify/sarama" + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // OutputEDUConsumer consumes events that originate in EDU server. type OutputEDUConsumer struct { typingConsumer *internal.ContinualConsumer @@ -131,7 +135,7 @@ func (t *OutputEDUConsumer) onSendToDeviceEvent(msg *sarama.ConsumerMessage) err Sender: ote.Sender, Type: ote.Type, MessageID: util.RandomString(32), - Messages: map[string]map[string]json.RawMessage{ + Messages: map[string]map[string]stdjson.RawMessage{ ote.UserID: { ote.DeviceID: ote.Content, }, diff --git a/federationsender/consumers/keychange.go b/federationsender/consumers/keychange.go index 5006ac28d..e6318eb79 100644 --- a/federationsender/consumers/keychange.go +++ b/federationsender/consumers/keychange.go @@ -16,7 +16,6 @@ package consumers import ( "context" - "encoding/json" "fmt" "github.com/Shopify/sarama" diff --git a/federationsender/consumers/roomserver.go b/federationsender/consumers/roomserver.go index b53cb251b..b16bfc6db 100644 --- a/federationsender/consumers/roomserver.go +++ b/federationsender/consumers/roomserver.go @@ -16,7 +16,6 @@ package consumers import ( "context" - "encoding/json" "fmt" "github.com/Shopify/sarama" diff --git a/federationsender/internal/perform.go b/federationsender/internal/perform.go index 45f33ff70..04dac92d7 100644 --- a/federationsender/internal/perform.go +++ b/federationsender/internal/perform.go @@ -2,7 +2,6 @@ package internal import ( "context" - "encoding/json" "errors" "fmt" "time" @@ -14,9 +13,13 @@ import ( "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + jsoniter "github.com/json-iterator/go" "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // PerformLeaveRequest implements api.FederationSenderInternalAPI func (r *FederationSenderInternalAPI) PerformDirectoryLookup( ctx context.Context, diff --git a/federationsender/inthttp/server.go b/federationsender/inthttp/server.go index 293fb4209..ffa818bf4 100644 --- a/federationsender/inthttp/server.go +++ b/federationsender/inthttp/server.go @@ -1,15 +1,18 @@ package inthttp import ( - "encoding/json" "net/http" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/util" + + "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // AddRoutes adds the FederationSenderInternalAPI handlers to the http.ServeMux. // nolint:gocyclo func AddRoutes(intAPI api.FederationSenderInternalAPI, internalAPIMux *mux.Router) { diff --git a/federationsender/queue/destinationqueue.go b/federationsender/queue/destinationqueue.go index c8b0bf658..641d6ef0d 100644 --- a/federationsender/queue/destinationqueue.go +++ b/federationsender/queue/destinationqueue.go @@ -16,7 +16,7 @@ package queue import ( "context" - "encoding/json" + stdjson "encoding/json" "fmt" "sync" "time" @@ -27,6 +27,7 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" + "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus" "go.uber.org/atomic" @@ -367,7 +368,7 @@ func (oq *destinationQueue) nextTransaction( // Create the transaction. t := gomatrixserverlib.Transaction{ - PDUs: []json.RawMessage{}, + PDUs: []stdjson.RawMessage{}, EDUs: []gomatrixserverlib.EDU{}, } t.Origin = oq.origin @@ -390,7 +391,7 @@ func (oq *destinationQueue) nextTransaction( if pdu == nil || pdu.pdu == nil { continue } - // Append the JSON of the event, since this is a json.RawMessage type in the + // Append the JSON of the event, since this is a []byte type in the // gomatrixserverlib.Transaction struct t.PDUs = append(t.PDUs, pdu.pdu.JSON()) pduReceipts = append(pduReceipts, pdu.receipt) diff --git a/federationsender/queue/queue.go b/federationsender/queue/queue.go index 8054856e3..b440bcdae 100644 --- a/federationsender/queue/queue.go +++ b/federationsender/queue/queue.go @@ -17,7 +17,6 @@ package queue import ( "context" "crypto/ed25519" - "encoding/json" "fmt" "sync" "time" @@ -27,11 +26,15 @@ import ( "github.com/matrix-org/dendrite/federationsender/storage/shared" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // OutgoingQueues is a collection of queues for sending transactions to other // matrix servers type OutgoingQueues struct { diff --git a/federationsender/storage/shared/storage.go b/federationsender/storage/shared/storage.go index fbf84c705..cd43e7929 100644 --- a/federationsender/storage/shared/storage.go +++ b/federationsender/storage/shared/storage.go @@ -24,8 +24,12 @@ import ( "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type Database struct { DB *sql.DB Cache caching.FederationSenderCache diff --git a/federationsender/storage/shared/storage_edus.go b/federationsender/storage/shared/storage_edus.go index 86fee1a37..425df9715 100644 --- a/federationsender/storage/shared/storage_edus.go +++ b/federationsender/storage/shared/storage_edus.go @@ -17,7 +17,6 @@ package shared import ( "context" "database/sql" - "encoding/json" "errors" "fmt" diff --git a/federationsender/storage/shared/storage_pdus.go b/federationsender/storage/shared/storage_pdus.go index bc298a905..55324cec5 100644 --- a/federationsender/storage/shared/storage_pdus.go +++ b/federationsender/storage/shared/storage_pdus.go @@ -17,7 +17,6 @@ package shared import ( "context" "database/sql" - "encoding/json" "errors" "fmt" diff --git a/go.mod b/go.mod index eddf96a7c..04fc8541e 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/gologme/log v1.2.0 github.com/gorilla/mux v1.8.0 github.com/hashicorp/golang-lru v0.5.4 + github.com/json-iterator/go v1.1.10 github.com/lib/pq v1.8.0 github.com/libp2p/go-libp2p v0.11.0 github.com/libp2p/go-libp2p-circuit v0.3.1 @@ -33,6 +34,7 @@ require ( github.com/pressly/goose v2.7.0-rc5+incompatible github.com/prometheus/client_golang v1.7.1 github.com/sirupsen/logrus v1.7.0 + github.com/stretchr/testify v1.6.1 github.com/tidwall/gjson v1.6.3 github.com/tidwall/match v1.0.2 // indirect github.com/tidwall/sjson v1.1.2 diff --git a/go.sum b/go.sum index fc48085a3..94169ecae 100644 --- a/go.sum +++ b/go.sum @@ -282,6 +282,7 @@ github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= @@ -607,8 +608,10 @@ github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= diff --git a/internal/httputil/http.go b/internal/httputil/http.go index 9197371aa..563cd0624 100644 --- a/internal/httputil/http.go +++ b/internal/httputil/http.go @@ -17,16 +17,18 @@ package httputil import ( "bytes" "context" - "encoding/json" "fmt" "net/http" "net/url" "strings" + jsoniter "github.com/json-iterator/go" opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // PostJSON performs a POST request with JSON on an internal HTTP API func PostJSON( ctx context.Context, span opentracing.Span, httpClient *http.Client, diff --git a/keyserver/api/api.go b/keyserver/api/api.go index 442af8715..2cbcc15e5 100644 --- a/keyserver/api/api.go +++ b/keyserver/api/api.go @@ -16,7 +16,6 @@ package api import ( "context" - "encoding/json" "strings" "time" @@ -83,7 +82,7 @@ type OneTimeKeys struct { // The device ID of this device DeviceID string // A map of algorithm:key_id => key JSON - KeyJSON map[string]json.RawMessage + KeyJSON map[string][]byte } // Split a key in KeyJSON into algorithm and key ID @@ -142,7 +141,7 @@ type PerformClaimKeysRequest struct { type PerformClaimKeysResponse struct { // Map of user_id to device_id to algorithm:key_id to key JSON - OneTimeKeys map[string]map[string]map[string]json.RawMessage + OneTimeKeys map[string]map[string]map[string][]byte // Map of remote server domain to error JSON Failures map[string]interface{} // Set if there was a fatal error processing this action @@ -159,7 +158,7 @@ type QueryKeysResponse struct { // Map of remote server domain to error JSON Failures map[string]interface{} // Map of user_id to device_id to device_key - DeviceKeys map[string]map[string]json.RawMessage + DeviceKeys map[string]map[string][]byte // Set if there was a fatal error processing this query Error *KeyError } diff --git a/keyserver/internal/device_list_update.go b/keyserver/internal/device_list_update.go index 4d1b1107c..d2493b42a 100644 --- a/keyserver/internal/device_list_update.go +++ b/keyserver/internal/device_list_update.go @@ -16,7 +16,6 @@ package internal import ( "context" - "encoding/json" "fmt" "hash/fnv" "sync" diff --git a/keyserver/internal/internal.go b/keyserver/internal/internal.go index 53afe0a60..c88edf144 100644 --- a/keyserver/internal/internal.go +++ b/keyserver/internal/internal.go @@ -17,7 +17,6 @@ package internal import ( "bytes" "context" - "encoding/json" "fmt" "sync" "time" @@ -29,11 +28,15 @@ import ( userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + jsoniter "github.com/json-iterator/go" "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type KeyInternalAPI struct { DB storage.Database ThisServer gomatrixserverlib.ServerName @@ -80,7 +83,7 @@ func (a *KeyInternalAPI) PerformUploadKeys(ctx context.Context, req *api.Perform } func (a *KeyInternalAPI) PerformClaimKeys(ctx context.Context, req *api.PerformClaimKeysRequest, res *api.PerformClaimKeysResponse) { - res.OneTimeKeys = make(map[string]map[string]map[string]json.RawMessage) + res.OneTimeKeys = make(map[string]map[string]map[string][]byte) res.Failures = make(map[string]interface{}) // wrap request map in a top-level by-domain map domainToDeviceKeys := make(map[string]map[string]map[string]string) @@ -108,11 +111,11 @@ func (a *KeyInternalAPI) PerformClaimKeys(ctx context.Context, req *api.PerformC for _, key := range keys { _, ok := res.OneTimeKeys[key.UserID] if !ok { - res.OneTimeKeys[key.UserID] = make(map[string]map[string]json.RawMessage) + res.OneTimeKeys[key.UserID] = make(map[string]map[string][]byte) } _, ok = res.OneTimeKeys[key.UserID][key.DeviceID] if !ok { - res.OneTimeKeys[key.UserID][key.DeviceID] = make(map[string]json.RawMessage) + res.OneTimeKeys[key.UserID][key.DeviceID] = make(map[string][]byte) } for keyID, keyJSON := range key.KeyJSON { res.OneTimeKeys[key.UserID][key.DeviceID][keyID] = keyJSON @@ -165,9 +168,9 @@ func (a *KeyInternalAPI) claimRemoteKeys( keysClaimed := 0 for result := range resultCh { for userID, nest := range result.OneTimeKeys { - res.OneTimeKeys[userID] = make(map[string]map[string]json.RawMessage) + res.OneTimeKeys[userID] = make(map[string]map[string][]byte) for deviceID, nest2 := range nest { - res.OneTimeKeys[userID][deviceID] = make(map[string]json.RawMessage) + res.OneTimeKeys[userID][deviceID] = make(map[string][]byte) for keyIDWithAlgo, otk := range nest2 { keyJSON, err := json.Marshal(otk) if err != nil { @@ -220,7 +223,7 @@ func (a *KeyInternalAPI) QueryDeviceMessages(ctx context.Context, req *api.Query } func (a *KeyInternalAPI) QueryKeys(ctx context.Context, req *api.QueryKeysRequest, res *api.QueryKeysResponse) { - res.DeviceKeys = make(map[string]map[string]json.RawMessage) + res.DeviceKeys = make(map[string]map[string][]byte) res.Failures = make(map[string]interface{}) // make a map from domain to device keys domainToDeviceKeys := make(map[string]map[string][]string) @@ -254,7 +257,7 @@ func (a *KeyInternalAPI) QueryKeys(ctx context.Context, req *api.QueryKeysReques } if res.DeviceKeys[userID] == nil { - res.DeviceKeys[userID] = make(map[string]json.RawMessage) + res.DeviceKeys[userID] = make(map[string][]byte) } for _, dk := range deviceKeys { if len(dk.KeyJSON) == 0 { @@ -335,7 +338,7 @@ func (a *KeyInternalAPI) queryRemoteKeys( for result := range resultCh { for userID, nest := range result.DeviceKeys { - res.DeviceKeys[userID] = make(map[string]json.RawMessage) + res.DeviceKeys[userID] = make(map[string][]byte) for deviceID, deviceKey := range nest { keyJSON, err := json.Marshal(deviceKey) if err != nil { @@ -432,7 +435,7 @@ func (a *KeyInternalAPI) populateResponseWithDeviceKeysFromDatabase( return fmt.Errorf("DeviceKeysForUser %s returned no keys but wanted all keys, falling back to remote", userID) } if res.DeviceKeys[userID] == nil { - res.DeviceKeys[userID] = make(map[string]json.RawMessage) + res.DeviceKeys[userID] = make(map[string][]byte) } for _, key := range keys { diff --git a/keyserver/inthttp/server.go b/keyserver/inthttp/server.go index 7dfaed2e7..ff783baac 100644 --- a/keyserver/inthttp/server.go +++ b/keyserver/inthttp/server.go @@ -15,15 +15,18 @@ package inthttp import ( - "encoding/json" "net/http" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/keyserver/api" "github.com/matrix-org/util" + + "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + func AddRoutes(internalAPIMux *mux.Router, s api.KeyInternalAPI) { internalAPIMux.Handle(InputDeviceListUpdatePath, httputil.MakeInternalAPI("inputDeviceListUpdate", func(req *http.Request) util.JSONResponse { diff --git a/keyserver/producers/keychange.go b/keyserver/producers/keychange.go index 0fe21d8b1..d976577f5 100644 --- a/keyserver/producers/keychange.go +++ b/keyserver/producers/keychange.go @@ -16,14 +16,16 @@ package producers import ( "context" - "encoding/json" "github.com/Shopify/sarama" + jsoniter "github.com/json-iterator/go" "github.com/matrix-org/dendrite/keyserver/api" "github.com/matrix-org/dendrite/keyserver/storage" "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // KeyChange produces key change events for the sync API and federation sender to consume type KeyChange struct { Topic string diff --git a/keyserver/storage/interface.go b/keyserver/storage/interface.go index 0ec62f567..fc2aa078d 100644 --- a/keyserver/storage/interface.go +++ b/keyserver/storage/interface.go @@ -16,7 +16,6 @@ package storage import ( "context" - "encoding/json" "github.com/matrix-org/dendrite/keyserver/api" "github.com/matrix-org/gomatrixserverlib" @@ -25,7 +24,7 @@ import ( type Database interface { // ExistingOneTimeKeys returns a map of keyIDWithAlgorithm to key JSON for the given parameters. If no keys exist with this combination // of user/device/key/algorithm 4-uple then it is omitted from the map. Returns an error when failing to communicate with the database. - ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) + ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error) // StoreOneTimeKeys persists the given one-time keys. StoreOneTimeKeys(ctx context.Context, keys api.OneTimeKeys) (*api.OneTimeKeysCount, error) diff --git a/keyserver/storage/postgres/one_time_keys_table.go b/keyserver/storage/postgres/one_time_keys_table.go index 6e32838b1..58399fd86 100644 --- a/keyserver/storage/postgres/one_time_keys_table.go +++ b/keyserver/storage/postgres/one_time_keys_table.go @@ -17,7 +17,6 @@ package postgres import ( "context" "database/sql" - "encoding/json" "time" "github.com/matrix-org/dendrite/internal" @@ -93,7 +92,7 @@ func NewPostgresOneTimeKeysTable(db *sql.DB) (tables.OneTimeKeys, error) { return s, nil } -func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) { +func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error) { rows, err := s.selectKeysStmt.QueryContext(ctx, userID, deviceID) if err != nil { return nil, err @@ -105,7 +104,7 @@ func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, d wantSet[ka] = true } - result := make(map[string]json.RawMessage) + result := make(map[string][]byte) for rows.Next() { var keyID string var algorithm string @@ -115,7 +114,7 @@ func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, d } keyIDWithAlgo := algorithm + ":" + keyID if wantSet[keyIDWithAlgo] { - result[keyIDWithAlgo] = json.RawMessage(keyJSONStr) + result[keyIDWithAlgo] = []byte(keyJSONStr) } } return result, rows.Err() @@ -178,7 +177,7 @@ func (s *oneTimeKeysStatements) InsertOneTimeKeys(ctx context.Context, txn *sql. func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey( ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string, -) (map[string]json.RawMessage, error) { +) (map[string][]byte, error) { var keyID string var keyJSON string err := sqlutil.TxStmtContext(ctx, txn, s.selectKeyByAlgorithmStmt).QueryRowContext(ctx, userID, deviceID, algorithm).Scan(&keyID, &keyJSON) @@ -189,7 +188,7 @@ func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey( return nil, err } _, err = sqlutil.TxStmtContext(ctx, txn, s.deleteOneTimeKeyStmt).ExecContext(ctx, userID, deviceID, algorithm, keyID) - return map[string]json.RawMessage{ - algorithm + ":" + keyID: json.RawMessage(keyJSON), + return map[string][]byte{ + algorithm + ":" + keyID: []byte(keyJSON), }, err } diff --git a/keyserver/storage/shared/storage.go b/keyserver/storage/shared/storage.go index de757f294..04734b982 100644 --- a/keyserver/storage/shared/storage.go +++ b/keyserver/storage/shared/storage.go @@ -17,7 +17,6 @@ package shared import ( "context" "database/sql" - "encoding/json" "github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/keyserver/api" @@ -34,7 +33,7 @@ type Database struct { StaleDeviceListsTable tables.StaleDeviceLists } -func (d *Database) ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) { +func (d *Database) ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error) { return d.OneTimeKeysTable.SelectOneTimeKeys(ctx, userID, deviceID, keyIDsWithAlgorithms) } diff --git a/keyserver/storage/sqlite3/one_time_keys_table.go b/keyserver/storage/sqlite3/one_time_keys_table.go index 185b88612..f048d155d 100644 --- a/keyserver/storage/sqlite3/one_time_keys_table.go +++ b/keyserver/storage/sqlite3/one_time_keys_table.go @@ -17,7 +17,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "time" "github.com/matrix-org/dendrite/internal" @@ -93,7 +92,7 @@ func NewSqliteOneTimeKeysTable(db *sql.DB) (tables.OneTimeKeys, error) { return s, nil } -func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) { +func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error) { rows, err := s.selectKeysStmt.QueryContext(ctx, userID, deviceID) if err != nil { return nil, err @@ -105,7 +104,7 @@ func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, d wantSet[ka] = true } - result := make(map[string]json.RawMessage) + result := make(map[string][]byte) for rows.Next() { var keyID string var algorithm string @@ -115,7 +114,7 @@ func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, d } keyIDWithAlgo := algorithm + ":" + keyID if wantSet[keyIDWithAlgo] { - result[keyIDWithAlgo] = json.RawMessage(keyJSONStr) + result[keyIDWithAlgo] = []byte(keyJSONStr) } } return result, rows.Err() @@ -180,7 +179,7 @@ func (s *oneTimeKeysStatements) InsertOneTimeKeys( func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey( ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string, -) (map[string]json.RawMessage, error) { +) (map[string][]byte, error) { var keyID string var keyJSON string err := sqlutil.TxStmtContext(ctx, txn, s.selectKeyByAlgorithmStmt).QueryRowContext(ctx, userID, deviceID, algorithm).Scan(&keyID, &keyJSON) @@ -197,7 +196,7 @@ func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey( if keyJSON == "" { return nil, nil } - return map[string]json.RawMessage{ - algorithm + ":" + keyID: json.RawMessage(keyJSON), + return map[string][]byte{ + algorithm + ":" + keyID: []byte(keyJSON), }, err } diff --git a/keyserver/storage/tables/interface.go b/keyserver/storage/tables/interface.go index b70c9bce6..f8177b10e 100644 --- a/keyserver/storage/tables/interface.go +++ b/keyserver/storage/tables/interface.go @@ -17,19 +17,18 @@ package tables import ( "context" "database/sql" - "encoding/json" "github.com/matrix-org/dendrite/keyserver/api" "github.com/matrix-org/gomatrixserverlib" ) type OneTimeKeys interface { - SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) + SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error) CountOneTimeKeys(ctx context.Context, userID, deviceID string) (*api.OneTimeKeysCount, error) InsertOneTimeKeys(ctx context.Context, txn *sql.Tx, keys api.OneTimeKeys) (*api.OneTimeKeysCount, error) // SelectAndDeleteOneTimeKey selects a single one time key matching the user/device/algorithm specified and returns the algo:key_id => JSON. // Returns an empty map if the key does not exist. - SelectAndDeleteOneTimeKey(ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string) (map[string]json.RawMessage, error) + SelectAndDeleteOneTimeKey(ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string) (map[string][]byte, error) } type DeviceKeys interface { diff --git a/mediaapi/routing/download.go b/mediaapi/routing/download.go index 8ea1b97f7..ee51fb614 100644 --- a/mediaapi/routing/download.go +++ b/mediaapi/routing/download.go @@ -16,7 +16,6 @@ package routing import ( "context" - "encoding/json" "fmt" "io" "mime" diff --git a/mediaapi/routing/routing.go b/mediaapi/routing/routing.go index 917a85964..a561e460b 100644 --- a/mediaapi/routing/routing.go +++ b/mediaapi/routing/routing.go @@ -20,18 +20,22 @@ import ( userapi "github.com/matrix-org/dendrite/userapi/api" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/mediaapi/storage" "github.com/matrix-org/dendrite/mediaapi/types" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // Setup registers the media API HTTP handlers // // Due to Setup being used to call many other functions, a gocyclo nolint is diff --git a/roomserver/acls/acls.go b/roomserver/acls/acls.go index b18daa3de..a8d248201 100644 --- a/roomserver/acls/acls.go +++ b/roomserver/acls/acls.go @@ -16,7 +16,6 @@ package acls import ( "context" - "encoding/json" "fmt" "net" "regexp" @@ -24,9 +23,13 @@ import ( "sync" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type ServerACLDatabase interface { // GetKnownRooms returns a list of all rooms we know about. GetKnownRooms(ctx context.Context) ([]string, error) diff --git a/roomserver/api/api.go b/roomserver/api/api.go index cedd61930..0aecd188f 100644 --- a/roomserver/api/api.go +++ b/roomserver/api/api.go @@ -5,8 +5,12 @@ import ( asAPI "github.com/matrix-org/dendrite/appservice/api" fsAPI "github.com/matrix-org/dendrite/federationsender/api" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // RoomserverInputAPI is used to write events to the room server. type RoomserverInternalAPI interface { // needed to avoid chicken and egg scenario when setting up the diff --git a/roomserver/api/api_trace.go b/roomserver/api/api_trace.go index 40745975e..ef46751ae 100644 --- a/roomserver/api/api_trace.go +++ b/roomserver/api/api_trace.go @@ -2,7 +2,6 @@ package api import ( "context" - "encoding/json" "fmt" asAPI "github.com/matrix-org/dendrite/appservice/api" diff --git a/roomserver/api/perform.go b/roomserver/api/perform.go index ae2d6d975..a212d3d7e 100644 --- a/roomserver/api/perform.go +++ b/roomserver/api/perform.go @@ -1,7 +1,6 @@ package api import ( - "encoding/json" "fmt" "net/http" @@ -54,7 +53,7 @@ func (p *PerformError) JSONResponse() util.JSONResponse { return util.JSONResponse{ Code: p.RemoteCode, // TODO: Should we assert this is in fact JSON? E.g gjson parse? - JSON: json.RawMessage(p.Msg), + JSON: []byte(p.Msg), } default: return util.ErrorResponse(p) diff --git a/roomserver/api/query.go b/roomserver/api/query.go index 43e562a98..814f7c3b1 100644 --- a/roomserver/api/query.go +++ b/roomserver/api/query.go @@ -17,7 +17,6 @@ package api import ( - "encoding/json" "fmt" "strings" diff --git a/roomserver/auth/auth.go b/roomserver/auth/auth.go index aa1d5bc25..9a7a3c397 100644 --- a/roomserver/auth/auth.go +++ b/roomserver/auth/auth.go @@ -13,11 +13,13 @@ package auth import ( - "encoding/json" - "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // TODO: This logic should live in gomatrixserverlib // IsServerAllowed returns true if the server is allowed to see events in the room diff --git a/roomserver/internal/alias.go b/roomserver/internal/alias.go index 843b0bccf..a6e064053 100644 --- a/roomserver/internal/alias.go +++ b/roomserver/internal/alias.go @@ -16,7 +16,6 @@ package internal import ( "context" - "encoding/json" "errors" "fmt" "time" @@ -25,8 +24,12 @@ import ( "github.com/matrix-org/gomatrixserverlib" asAPI "github.com/matrix-org/dendrite/appservice/api" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // RoomserverInternalAPIDatabase has the storage APIs needed to implement the alias API. type RoomserverInternalAPIDatabase interface { // Save a given room alias with the room ID it refers to. @@ -200,7 +203,7 @@ func (r *RoomserverInternalAPI) sendUpdatedAliasesEvent( if err != nil { return err } - err = builder.SetContent(json.RawMessage(rawContent)) + err = builder.SetContent([]byte(rawContent)) if err != nil { return err } diff --git a/roomserver/internal/input/input.go b/roomserver/internal/input/input.go index 404bc7423..6b767812b 100644 --- a/roomserver/internal/input/input.go +++ b/roomserver/internal/input/input.go @@ -17,20 +17,23 @@ package input import ( "context" - "encoding/json" "sync" "time" - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/internal/hooks" "github.com/matrix-org/dendrite/roomserver/acls" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/storage" "github.com/matrix-org/gomatrixserverlib" + + "github.com/Shopify/sarama" + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" "go.uber.org/atomic" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type Inputer struct { DB storage.Database Producer sarama.SyncProducer diff --git a/roomserver/internal/perform/perform_peek.go b/roomserver/internal/perform/perform_peek.go index 2f4694c86..07fda574f 100644 --- a/roomserver/internal/perform/perform_peek.go +++ b/roomserver/internal/perform/perform_peek.go @@ -16,7 +16,6 @@ package perform import ( "context" - "encoding/json" "fmt" "strings" @@ -27,9 +26,13 @@ import ( "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + jsoniter "github.com/json-iterator/go" "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type Peeker struct { ServerName gomatrixserverlib.ServerName Cfg *config.RoomServer diff --git a/roomserver/internal/query/query_test.go b/roomserver/internal/query/query_test.go index 4e761d8ec..3af69d95c 100644 --- a/roomserver/internal/query/query_test.go +++ b/roomserver/internal/query/query_test.go @@ -16,14 +16,17 @@ package query import ( "context" - "encoding/json" "testing" "github.com/matrix-org/dendrite/internal/test" "github.com/matrix-org/dendrite/roomserver/types" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // used to implement RoomserverInternalAPIEventDB to test getAuthChain type getEventDB struct { eventMap map[string]*gomatrixserverlib.Event diff --git a/roomserver/inthttp/server.go b/roomserver/inthttp/server.go index f9c8ef9fd..4d094a11c 100644 --- a/roomserver/inthttp/server.go +++ b/roomserver/inthttp/server.go @@ -1,15 +1,18 @@ package inthttp import ( - "encoding/json" "net/http" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/util" + + "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // AddRoutes adds the RoomserverInternalAPI handlers to the http.ServeMux. // nolint: gocyclo func AddRoutes(r api.RoomserverInternalAPI, internalAPIMux *mux.Router) { diff --git a/roomserver/roomserver_test.go b/roomserver/roomserver_test.go index 5c9540071..acd2f6968 100644 --- a/roomserver/roomserver_test.go +++ b/roomserver/roomserver_test.go @@ -4,14 +4,12 @@ import ( "bytes" "context" "crypto/ed25519" - "encoding/json" "fmt" "os" "reflect" "testing" "time" - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/test" "github.com/matrix-org/dendrite/roomserver/api" @@ -20,6 +18,9 @@ import ( "github.com/matrix-org/dendrite/setup" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" + + "github.com/Shopify/sarama" + jsoniter "github.com/json-iterator/go" "github.com/sirupsen/logrus" ) @@ -33,7 +34,8 @@ const ( ) var ( - ctx = context.Background() + ctx = context.Background() + json = jsoniter.ConfigCompatibleWithStandardLibrary ) type dummyProducer struct { @@ -49,7 +51,7 @@ func (p *dummyProducer) SendMessage(msg *sarama.ProducerMessage) (partition int3 return 0, 0, nil } be := msg.Value.(sarama.ByteEncoder) - b := json.RawMessage(be) + b := []byte(be) fmt.Println("SENDING >>>>>>>> ", string(b)) var out api.OutputEvent err = json.Unmarshal(b, &out) @@ -143,7 +145,7 @@ func mustCreateEvents(t *testing.T, roomVer gomatrixserverlib.RoomVersion, event return } -func mustLoadRawEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events []json.RawMessage) []*gomatrixserverlib.HeaderedEvent { +func mustLoadRawEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events [][]byte) []*gomatrixserverlib.HeaderedEvent { t.Helper() hs := make([]*gomatrixserverlib.HeaderedEvent, len(events)) for i := range events { @@ -186,7 +188,7 @@ func mustCreateRoomserverAPI(t *testing.T) (api.RoomserverInternalAPI, *dummyPro ), dp } -func mustSendEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events []json.RawMessage) (api.RoomserverInternalAPI, *dummyProducer, []*gomatrixserverlib.HeaderedEvent) { +func mustSendEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events [][]byte) (api.RoomserverInternalAPI, *dummyProducer, []*gomatrixserverlib.HeaderedEvent) { t.Helper() rsAPI, dp := mustCreateRoomserverAPI(t) hevents := mustLoadRawEvents(t, ver, events) @@ -197,7 +199,7 @@ func mustSendEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events []js } func TestOutputRedactedEvent(t *testing.T) { - redactionEvents := []json.RawMessage{ + redactionEvents := [][]byte{ // create event []byte(`{"auth_events":[],"content":{"creator":"@userid:kaer.morhen"},"depth":0,"event_id":"$N4us6vqqq3RjvpKd:kaer.morhen","hashes":{"sha256":"WTdrCn/YsiounXcJPsLP8xT0ZjHiO5Ov0NvXYmK2onE"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"9+5JcpaN5b5KlHYHGp6r+GoNDH98lbfzGYwjfxensa5C5D/bDACaYnMDLnhwsHOE5nxgI+jT/GV271pz6PMSBQ"}},"state_key":"","type":"m.room.create"}`), // join event diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index b4d9d5624..91aa4c047 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -3,7 +3,6 @@ package shared import ( "context" "database/sql" - "encoding/json" "fmt" "sort" @@ -14,6 +13,8 @@ import ( "github.com/matrix-org/dendrite/roomserver/types" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + jsoniter "github.com/json-iterator/go" "github.com/tidwall/gjson" ) @@ -26,6 +27,8 @@ import ( // unsigned.redacted_because - we just don't clear out the content fields yet. const redactionsArePermanent = true +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type Database struct { DB *sql.DB Cache caching.RoomServerCaches diff --git a/roomserver/storage/sqlite3/events_table.go b/roomserver/storage/sqlite3/events_table.go index 53269657e..e616d429d 100644 --- a/roomserver/storage/sqlite3/events_table.go +++ b/roomserver/storage/sqlite3/events_table.go @@ -18,7 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "fmt" "strings" diff --git a/roomserver/storage/sqlite3/rooms_table.go b/roomserver/storage/sqlite3/rooms_table.go index fe8e601f5..b0d119c50 100644 --- a/roomserver/storage/sqlite3/rooms_table.go +++ b/roomserver/storage/sqlite3/rooms_table.go @@ -18,7 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "fmt" "strings" diff --git a/roomserver/storage/sqlite3/state_snapshot_table.go b/roomserver/storage/sqlite3/state_snapshot_table.go index bf49f62c2..22e843920 100644 --- a/roomserver/storage/sqlite3/state_snapshot_table.go +++ b/roomserver/storage/sqlite3/state_snapshot_table.go @@ -18,7 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "fmt" "strings" diff --git a/roomserver/storage/sqlite3/storage.go b/roomserver/storage/sqlite3/storage.go index 8e608a6db..9552cce05 100644 --- a/roomserver/storage/sqlite3/storage.go +++ b/roomserver/storage/sqlite3/storage.go @@ -28,8 +28,12 @@ import ( "github.com/matrix-org/dendrite/roomserver/types" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // A Database is used to store room events and stream offsets. type Database struct { shared.Database diff --git a/setup/mscs/msc2836/msc2836.go b/setup/mscs/msc2836/msc2836.go index 95473f97c..112b479e0 100644 --- a/setup/mscs/msc2836/msc2836.go +++ b/setup/mscs/msc2836/msc2836.go @@ -19,7 +19,6 @@ import ( "bytes" "context" "crypto/sha256" - "encoding/json" "fmt" "io" "net/http" @@ -27,6 +26,7 @@ import ( "strings" "time" + jsoniter "github.com/json-iterator/go" "github.com/matrix-org/dendrite/clientapi/jsonerror" fs "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/internal/hooks" @@ -42,6 +42,8 @@ const ( constRelType = "m.reference" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type EventRelationshipRequest struct { EventID string `json:"event_id"` RoomID string `json:"room_id"` diff --git a/setup/mscs/msc2836/msc2836_test.go b/setup/mscs/msc2836/msc2836_test.go index 4eb5708c1..98b8938c2 100644 --- a/setup/mscs/msc2836/msc2836_test.go +++ b/setup/mscs/msc2836/msc2836_test.go @@ -6,7 +6,6 @@ import ( "crypto/ed25519" "crypto/sha256" "encoding/base64" - "encoding/json" "fmt" "io/ioutil" "net/http" @@ -16,6 +15,7 @@ import ( "time" "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" "github.com/matrix-org/dendrite/internal/hooks" "github.com/matrix-org/dendrite/internal/httputil" roomserver "github.com/matrix-org/dendrite/roomserver/api" @@ -30,6 +30,7 @@ var ( client = &http.Client{ Timeout: 10 * time.Second, } + json = jsoniter.ConfigCompatibleWithStandardLibrary ) // Basic sanity check of MSC2836 logic. Injects a thread that looks like: @@ -162,9 +163,9 @@ func TestMSC2836(t *testing.T) { // make everyone joined to each other's rooms nopRsAPI := &testRoomserverAPI{ userToJoinedRooms: map[string][]string{ - alice: []string{roomID}, - bob: []string{roomID}, - charlie: []string{roomID}, + alice: {roomID}, + bob: {roomID}, + charlie: {roomID}, }, events: map[string]*gomatrixserverlib.HeaderedEvent{ eventA.EventID(): eventA, diff --git a/setup/mscs/msc2836/storage.go b/setup/mscs/msc2836/storage.go index 72523916b..6bd3d279b 100644 --- a/setup/mscs/msc2836/storage.go +++ b/setup/mscs/msc2836/storage.go @@ -5,7 +5,6 @@ import ( "context" "database/sql" "encoding/base64" - "encoding/json" "github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/setup/config" diff --git a/signingkeyserver/inthttp/server.go b/signingkeyserver/inthttp/server.go index d26f73805..ab9aafc9e 100644 --- a/signingkeyserver/inthttp/server.go +++ b/signingkeyserver/inthttp/server.go @@ -1,7 +1,6 @@ package inthttp import ( - "encoding/json" "net/http" "github.com/gorilla/mux" @@ -9,8 +8,12 @@ import ( "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/signingkeyserver/api" "github.com/matrix-org/util" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + func AddRoutes(s api.SigningKeyServerAPI, internalAPIMux *mux.Router, cache caching.ServerKeyCache) { internalAPIMux.Handle(ServerKeyQueryPublicKeyPath, httputil.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse { diff --git a/signingkeyserver/serverkeyapi_test.go b/signingkeyserver/serverkeyapi_test.go index e59deb4d7..263c2d5ad 100644 --- a/signingkeyserver/serverkeyapi_test.go +++ b/signingkeyserver/serverkeyapi_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "crypto/ed25519" - "encoding/json" "fmt" "io/ioutil" "net/http" @@ -18,8 +17,12 @@ import ( "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/signingkeyserver/api" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type server struct { name gomatrixserverlib.ServerName // server name validity time.Duration // key validity duration from now diff --git a/syncapi/consumers/clientapi.go b/syncapi/consumers/clientapi.go index 9883c6b03..dd38771b2 100644 --- a/syncapi/consumers/clientapi.go +++ b/syncapi/consumers/clientapi.go @@ -16,18 +16,21 @@ package consumers import ( "context" - "encoding/json" - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/eventutil" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/sync" "github.com/matrix-org/dendrite/syncapi/types" + + "github.com/Shopify/sarama" + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // OutputClientDataConsumer consumes events that originated in the client API server. type OutputClientDataConsumer struct { clientAPIConsumer *internal.ContinualConsumer diff --git a/syncapi/consumers/eduserver_receipts.go b/syncapi/consumers/eduserver_receipts.go index 88334b654..4cdac47de 100644 --- a/syncapi/consumers/eduserver_receipts.go +++ b/syncapi/consumers/eduserver_receipts.go @@ -16,16 +16,16 @@ package consumers import ( "context" - "encoding/json" "github.com/matrix-org/dendrite/syncapi/types" - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/sync" + + "github.com/Shopify/sarama" log "github.com/sirupsen/logrus" ) diff --git a/syncapi/consumers/eduserver_sendtodevice.go b/syncapi/consumers/eduserver_sendtodevice.go index a375baf83..fc480b326 100644 --- a/syncapi/consumers/eduserver_sendtodevice.go +++ b/syncapi/consumers/eduserver_sendtodevice.go @@ -16,9 +16,7 @@ package consumers import ( "context" - "encoding/json" - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/setup/config" @@ -27,6 +25,8 @@ import ( "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + "github.com/Shopify/sarama" log "github.com/sirupsen/logrus" ) diff --git a/syncapi/consumers/eduserver_typing.go b/syncapi/consumers/eduserver_typing.go index 28574b502..fdf6993f2 100644 --- a/syncapi/consumers/eduserver_typing.go +++ b/syncapi/consumers/eduserver_typing.go @@ -15,15 +15,14 @@ package consumers import ( - "encoding/json" - - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/sync" "github.com/matrix-org/dendrite/syncapi/types" + + "github.com/Shopify/sarama" log "github.com/sirupsen/logrus" ) diff --git a/syncapi/consumers/keychange.go b/syncapi/consumers/keychange.go index 59cd583d1..6ea0e007d 100644 --- a/syncapi/consumers/keychange.go +++ b/syncapi/consumers/keychange.go @@ -16,10 +16,8 @@ package consumers import ( "context" - "encoding/json" "sync" - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/keyserver/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" @@ -27,6 +25,8 @@ import ( syncapi "github.com/matrix-org/dendrite/syncapi/sync" "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" + + "github.com/Shopify/sarama" log "github.com/sirupsen/logrus" ) diff --git a/syncapi/consumers/roomserver.go b/syncapi/consumers/roomserver.go index 399f67ba8..61f8820eb 100644 --- a/syncapi/consumers/roomserver.go +++ b/syncapi/consumers/roomserver.go @@ -16,10 +16,8 @@ package consumers import ( "context" - "encoding/json" "fmt" - "github.com/Shopify/sarama" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/config" diff --git a/syncapi/routing/filter.go b/syncapi/routing/filter.go index baa4d841c..d3c9e25e2 100644 --- a/syncapi/routing/filter.go +++ b/syncapi/routing/filter.go @@ -15,7 +15,6 @@ package routing import ( - "encoding/json" "io/ioutil" "net/http" @@ -25,6 +24,7 @@ import ( "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + "github.com/tidwall/gjson" ) diff --git a/syncapi/routing/routing.go b/syncapi/routing/routing.go index e2ff27395..9b001b371 100644 --- a/syncapi/routing/routing.go +++ b/syncapi/routing/routing.go @@ -17,7 +17,6 @@ package routing import ( "net/http" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/config" @@ -26,8 +25,13 @@ import ( userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + + "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // Setup configures the given mux with sync-server listeners // // Due to Setup being used to call many other functions, a gocyclo nolint is diff --git a/syncapi/storage/postgres/current_room_state_table.go b/syncapi/storage/postgres/current_room_state_table.go index 77e1e363f..deb2ce0a8 100644 --- a/syncapi/storage/postgres/current_room_state_table.go +++ b/syncapi/storage/postgres/current_room_state_table.go @@ -18,7 +18,6 @@ package postgres import ( "context" "database/sql" - "encoding/json" "github.com/lib/pq" "github.com/matrix-org/dendrite/internal" diff --git a/syncapi/storage/postgres/filter_table.go b/syncapi/storage/postgres/filter_table.go index beeb864ba..4938c118a 100644 --- a/syncapi/storage/postgres/filter_table.go +++ b/syncapi/storage/postgres/filter_table.go @@ -17,7 +17,6 @@ package postgres import ( "context" "database/sql" - "encoding/json" "github.com/matrix-org/dendrite/syncapi/storage/tables" "github.com/matrix-org/gomatrixserverlib" diff --git a/syncapi/storage/postgres/invites_table.go b/syncapi/storage/postgres/invites_table.go index 48ad58c05..8f41774fa 100644 --- a/syncapi/storage/postgres/invites_table.go +++ b/syncapi/storage/postgres/invites_table.go @@ -18,7 +18,6 @@ package postgres import ( "context" "database/sql" - "encoding/json" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" diff --git a/syncapi/storage/postgres/output_room_events_table.go b/syncapi/storage/postgres/output_room_events_table.go index f4bbebd26..9e65ca50b 100644 --- a/syncapi/storage/postgres/output_room_events_table.go +++ b/syncapi/storage/postgres/output_room_events_table.go @@ -18,7 +18,6 @@ package postgres import ( "context" "database/sql" - "encoding/json" "sort" "github.com/matrix-org/dendrite/internal" diff --git a/syncapi/storage/postgres/send_to_device_table.go b/syncapi/storage/postgres/send_to_device_table.go index ac60989c1..69afadae3 100644 --- a/syncapi/storage/postgres/send_to_device_table.go +++ b/syncapi/storage/postgres/send_to_device_table.go @@ -17,7 +17,6 @@ package postgres import ( "context" "database/sql" - "encoding/json" "github.com/lib/pq" "github.com/matrix-org/dendrite/internal" diff --git a/syncapi/storage/postgres/syncserver.go b/syncapi/storage/postgres/syncserver.go index 60d67ac0e..782ee1c40 100644 --- a/syncapi/storage/postgres/syncserver.go +++ b/syncapi/storage/postgres/syncserver.go @@ -18,6 +18,8 @@ package postgres import ( "database/sql" + jsoniter "github.com/json-iterator/go" + // Import the postgres database driver. _ "github.com/lib/pq" "github.com/matrix-org/dendrite/eduserver/cache" @@ -27,6 +29,8 @@ import ( "github.com/matrix-org/dendrite/syncapi/storage/shared" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // SyncServerDatasource represents a sync server datasource which manages // both the database for PDUs and caches for EDUs. type SyncServerDatasource struct { diff --git a/syncapi/storage/shared/syncserver.go b/syncapi/storage/shared/syncserver.go index ba9403a53..ec759904b 100644 --- a/syncapi/storage/shared/syncserver.go +++ b/syncapi/storage/shared/syncserver.go @@ -17,7 +17,6 @@ package shared import ( "context" "database/sql" - "encoding/json" "fmt" "time" @@ -31,9 +30,13 @@ import ( "github.com/matrix-org/dendrite/syncapi/storage/tables" "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // Database is a temporary struct until we have made syncserver.go the same for both pq/sqlite // For now this contains the shared functions type Database struct { diff --git a/syncapi/storage/sqlite3/current_room_state_table.go b/syncapi/storage/sqlite3/current_room_state_table.go index ac6590575..9b99b3c4e 100644 --- a/syncapi/storage/sqlite3/current_room_state_table.go +++ b/syncapi/storage/sqlite3/current_room_state_table.go @@ -18,7 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "strings" "github.com/matrix-org/dendrite/internal" diff --git a/syncapi/storage/sqlite3/filter_table.go b/syncapi/storage/sqlite3/filter_table.go index 3092bcd7d..520717d97 100644 --- a/syncapi/storage/sqlite3/filter_table.go +++ b/syncapi/storage/sqlite3/filter_table.go @@ -17,7 +17,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "fmt" "github.com/matrix-org/dendrite/syncapi/storage/tables" diff --git a/syncapi/storage/sqlite3/invites_table.go b/syncapi/storage/sqlite3/invites_table.go index f9dcfdbcd..49b246a42 100644 --- a/syncapi/storage/sqlite3/invites_table.go +++ b/syncapi/storage/sqlite3/invites_table.go @@ -18,7 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" diff --git a/syncapi/storage/sqlite3/output_room_events_table.go b/syncapi/storage/sqlite3/output_room_events_table.go index edbd36fb1..119129878 100644 --- a/syncapi/storage/sqlite3/output_room_events_table.go +++ b/syncapi/storage/sqlite3/output_room_events_table.go @@ -18,7 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "sort" "github.com/matrix-org/dendrite/internal" diff --git a/syncapi/storage/sqlite3/send_to_device_table.go b/syncapi/storage/sqlite3/send_to_device_table.go index e9dcb1bf5..bb15373b2 100644 --- a/syncapi/storage/sqlite3/send_to_device_table.go +++ b/syncapi/storage/sqlite3/send_to_device_table.go @@ -17,7 +17,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "strings" "github.com/matrix-org/dendrite/internal" diff --git a/syncapi/storage/sqlite3/syncserver.go b/syncapi/storage/sqlite3/syncserver.go index 1ad0e9473..a144f4aa3 100644 --- a/syncapi/storage/sqlite3/syncserver.go +++ b/syncapi/storage/sqlite3/syncserver.go @@ -18,6 +18,8 @@ package sqlite3 import ( "database/sql" + jsoniter "github.com/json-iterator/go" + // Import the sqlite3 package _ "github.com/mattn/go-sqlite3" @@ -28,6 +30,8 @@ import ( "github.com/matrix-org/dendrite/syncapi/storage/sqlite3/deltas" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // SyncServerDatasource represents a sync server datasource which manages // both the database for PDUs and caches for EDUs. type SyncServerDatasource struct { diff --git a/syncapi/storage/storage_test.go b/syncapi/storage/storage_test.go index 309a3a94e..90f4fc02d 100644 --- a/syncapi/storage/storage_test.go +++ b/syncapi/storage/storage_test.go @@ -3,7 +3,6 @@ package storage_test import ( "context" "crypto/ed25519" - "encoding/json" "fmt" "os" "testing" @@ -555,7 +554,7 @@ func TestSendToDeviceBehaviour(t *testing.T) { streamPos, err := db.StoreNewSendForDeviceMessage(ctx, "alice", "one", gomatrixserverlib.SendToDeviceEvent{ Sender: "bob", Type: "m.type", - Content: json.RawMessage("{}"), + Content: []byte("{}"), }) if err != nil { t.Fatal(err) diff --git a/syncapi/sync/notifier.go b/syncapi/sync/notifier.go index 66460a8db..1796327eb 100644 --- a/syncapi/sync/notifier.go +++ b/syncapi/sync/notifier.go @@ -22,9 +22,13 @@ import ( "github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // Notifier will wake up sleeping requests when there is some new data. // It does not tell requests what that data is, only the sync position which // they can use to get at it. This is done to prevent races whereby we tell the caller diff --git a/syncapi/sync/notifier_test.go b/syncapi/sync/notifier_test.go index d24da4636..cf6bba278 100644 --- a/syncapi/sync/notifier_test.go +++ b/syncapi/sync/notifier_test.go @@ -16,7 +16,6 @@ package sync import ( "context" - "encoding/json" "fmt" "sync" "testing" diff --git a/syncapi/sync/request.go b/syncapi/sync/request.go index f2f2894be..c4a443278 100644 --- a/syncapi/sync/request.go +++ b/syncapi/sync/request.go @@ -16,7 +16,6 @@ package sync import ( "context" - "encoding/json" "net/http" "strconv" "time" diff --git a/syncapi/types/types.go b/syncapi/types/types.go index 8e5260326..08ba18b4c 100644 --- a/syncapi/types/types.go +++ b/syncapi/types/types.go @@ -15,13 +15,14 @@ package types import ( - "encoding/json" "fmt" "strconv" "strings" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" + + jsoniter "github.com/json-iterator/go" "github.com/tidwall/gjson" ) @@ -33,6 +34,8 @@ var ( // ErrInvalidSyncTokenLen is returned when the pagination token is an // invalid length ErrInvalidSyncTokenLen = fmt.Errorf("Sync token has an invalid length") + + json = jsoniter.ConfigCompatibleWithStandardLibrary ) // StreamPosition represents the offset in the sync stream a client is at. @@ -335,9 +338,9 @@ func NewStreamTokenFromString(tok string) (token StreamingToken, err error) { // PrevEventRef represents a reference to a previous event in a state event upgrade type PrevEventRef struct { - PrevContent json.RawMessage `json:"prev_content"` - ReplacesState string `json:"replaces_state"` - PrevSender string `json:"prev_sender"` + PrevContent []byte `json:"prev_content"` + ReplacesState string `json:"replaces_state"` + PrevSender string `json:"prev_sender"` } // Response represents a /sync API response. See https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync @@ -429,14 +432,14 @@ func NewJoinResponse() *JoinResponse { // InviteResponse represents a /sync response for a room which is under the 'invite' key. type InviteResponse struct { InviteState struct { - Events []json.RawMessage `json:"events"` + Events [][]byte `json:"events"` } `json:"invite_state"` } // NewInviteResponse creates an empty response with initialised arrays. func NewInviteResponse(event *gomatrixserverlib.HeaderedEvent) *InviteResponse { res := InviteResponse{} - res.InviteState.Events = []json.RawMessage{} + res.InviteState.Events = [][]byte{} // First see if there's invite_room_state in the unsigned key of the invite. // If there is then unmarshal it into the response. This will contain the diff --git a/syncapi/types/types_test.go b/syncapi/types/types_test.go index 3698fbeea..000e8319c 100644 --- a/syncapi/types/types_test.go +++ b/syncapi/types/types_test.go @@ -1,7 +1,6 @@ package types import ( - "encoding/json" "reflect" "testing" diff --git a/userapi/api/api.go b/userapi/api/api.go index 809ba0476..232124972 100644 --- a/userapi/api/api.go +++ b/userapi/api/api.go @@ -16,7 +16,6 @@ package api import ( "context" - "encoding/json" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/gomatrixserverlib" @@ -42,10 +41,10 @@ type UserInternalAPI interface { // InputAccountDataRequest is the request for InputAccountData type InputAccountDataRequest struct { - UserID string // required: the user to set account data for - RoomID string // optional: the room to associate the account data with - DataType string // required: the data type of the data - AccountData json.RawMessage // required: the message content + UserID string // required: the user to set account data for + RoomID string // optional: the room to associate the account data with + DataType string // required: the data type of the data + AccountData []byte // required: the message content } // InputAccountDataResponse is the response for InputAccountData @@ -111,8 +110,8 @@ type QueryAccountDataRequest struct { // QueryAccountDataResponse is the response for QueryAccountData type QueryAccountDataResponse struct { - GlobalAccountData map[string]json.RawMessage // type -> data - RoomAccountData map[string]map[string]json.RawMessage // room -> type -> data + GlobalAccountData map[string][]byte // type -> data + RoomAccountData map[string]map[string][]byte // room -> type -> data } // QueryDevicesRequest is the request for QueryDevices diff --git a/userapi/internal/api.go b/userapi/internal/api.go index c1b9bcabf..a1d67e20a 100644 --- a/userapi/internal/api.go +++ b/userapi/internal/api.go @@ -17,7 +17,6 @@ package internal import ( "context" "database/sql" - "encoding/json" "errors" "fmt" @@ -313,17 +312,17 @@ func (a *UserInternalAPI) QueryAccountData(ctx context.Context, req *api.QueryAc return fmt.Errorf("cannot query account data of remote users: got %s want %s", domain, a.ServerName) } if req.DataType != "" { - var data json.RawMessage + var data []byte data, err = a.AccountDB.GetAccountDataByType(ctx, local, req.RoomID, req.DataType) if err != nil { return err } - res.RoomAccountData = make(map[string]map[string]json.RawMessage) - res.GlobalAccountData = make(map[string]json.RawMessage) + res.RoomAccountData = make(map[string]map[string][]byte) + res.GlobalAccountData = make(map[string][]byte) if data != nil { if req.RoomID != "" { if _, ok := res.RoomAccountData[req.RoomID]; !ok { - res.RoomAccountData[req.RoomID] = make(map[string]json.RawMessage) + res.RoomAccountData[req.RoomID] = make(map[string][]byte) } res.RoomAccountData[req.RoomID][req.DataType] = data } else { diff --git a/userapi/inthttp/server.go b/userapi/inthttp/server.go index e495e3536..f02bb7e8f 100644 --- a/userapi/inthttp/server.go +++ b/userapi/inthttp/server.go @@ -15,15 +15,18 @@ package inthttp import ( - "encoding/json" "net/http" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/util" + + "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + // nolint: gocyclo func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) { internalAPIMux.Handle(PerformAccountCreationPath, diff --git a/userapi/storage/accounts/interface.go b/userapi/storage/accounts/interface.go index c86b2c391..4b3da1389 100644 --- a/userapi/storage/accounts/interface.go +++ b/userapi/storage/accounts/interface.go @@ -16,7 +16,6 @@ package accounts import ( "context" - "encoding/json" "errors" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" @@ -36,13 +35,13 @@ type Database interface { // account already exists, it will return nil, ErrUserExists. CreateAccount(ctx context.Context, localpart, plaintextPassword, appserviceID string) (*api.Account, error) CreateGuestAccount(ctx context.Context) (*api.Account, error) - SaveAccountData(ctx context.Context, localpart, roomID, dataType string, content json.RawMessage) error - GetAccountData(ctx context.Context, localpart string) (global map[string]json.RawMessage, rooms map[string]map[string]json.RawMessage, err error) + SaveAccountData(ctx context.Context, localpart, roomID, dataType string, content []byte) error + GetAccountData(ctx context.Context, localpart string) (global map[string][]byte, rooms map[string]map[string][]byte, err error) // GetAccountDataByType returns account data matching a given // localpart, room ID and type. // If no account data could be found, returns nil // Returns an error if there was an issue with the retrieval - GetAccountDataByType(ctx context.Context, localpart, roomID, dataType string) (data json.RawMessage, err error) + GetAccountDataByType(ctx context.Context, localpart, roomID, dataType string) (data []byte, err error) GetNewNumericLocalpart(ctx context.Context) (int64, error) SaveThreePIDAssociation(ctx context.Context, threepid, localpart, medium string) (err error) RemoveThreePIDAssociation(ctx context.Context, threepid string, medium string) (err error) diff --git a/userapi/storage/accounts/postgres/account_data_table.go b/userapi/storage/accounts/postgres/account_data_table.go index 09eb26113..5444be4bd 100644 --- a/userapi/storage/accounts/postgres/account_data_table.go +++ b/userapi/storage/accounts/postgres/account_data_table.go @@ -17,7 +17,6 @@ package postgres import ( "context" "database/sql" - "encoding/json" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -74,7 +73,7 @@ func (s *accountDataStatements) prepare(db *sql.DB) (err error) { } func (s *accountDataStatements) insertAccountData( - ctx context.Context, txn *sql.Tx, localpart, roomID, dataType string, content json.RawMessage, + ctx context.Context, txn *sql.Tx, localpart, roomID, dataType string, content []byte, ) (err error) { stmt := sqlutil.TxStmt(txn, s.insertAccountDataStmt) _, err = stmt.ExecContext(ctx, localpart, roomID, dataType, content) @@ -84,8 +83,8 @@ func (s *accountDataStatements) insertAccountData( func (s *accountDataStatements) selectAccountData( ctx context.Context, localpart string, ) ( - /* global */ map[string]json.RawMessage, - /* rooms */ map[string]map[string]json.RawMessage, + /* global */ map[string][]byte, + /* rooms */ map[string]map[string][]byte, error, ) { rows, err := s.selectAccountDataStmt.QueryContext(ctx, localpart) @@ -94,8 +93,8 @@ func (s *accountDataStatements) selectAccountData( } defer internal.CloseAndLogIfError(ctx, rows, "selectAccountData: rows.close() failed") - global := map[string]json.RawMessage{} - rooms := map[string]map[string]json.RawMessage{} + global := map[string][]byte{} + rooms := map[string]map[string][]byte{} for rows.Next() { var roomID string @@ -108,7 +107,7 @@ func (s *accountDataStatements) selectAccountData( if roomID != "" { if _, ok := rooms[roomID]; !ok { - rooms[roomID] = map[string]json.RawMessage{} + rooms[roomID] = map[string][]byte{} } rooms[roomID][dataType] = content } else { @@ -121,7 +120,7 @@ func (s *accountDataStatements) selectAccountData( func (s *accountDataStatements) selectAccountDataByType( ctx context.Context, localpart, roomID, dataType string, -) (data json.RawMessage, err error) { +) (data []byte, err error) { var bytes []byte stmt := s.selectAccountDataByTypeStmt if err = stmt.QueryRowContext(ctx, localpart, roomID, dataType).Scan(&bytes); err != nil { @@ -130,6 +129,6 @@ func (s *accountDataStatements) selectAccountDataByType( } return } - data = json.RawMessage(bytes) + data = []byte(bytes) return } diff --git a/userapi/storage/accounts/postgres/storage.go b/userapi/storage/accounts/postgres/storage.go index 870756d8b..c5d061c02 100644 --- a/userapi/storage/accounts/postgres/storage.go +++ b/userapi/storage/accounts/postgres/storage.go @@ -17,7 +17,6 @@ package postgres import ( "context" "database/sql" - "encoding/json" "errors" "strconv" @@ -28,10 +27,11 @@ import ( "github.com/matrix-org/dendrite/userapi/storage/accounts/postgres/deltas" _ "github.com/matrix-org/dendrite/userapi/storage/accounts/postgres/deltas" "github.com/matrix-org/gomatrixserverlib" - "golang.org/x/crypto/bcrypt" - // Import the postgres database driver. _ "github.com/lib/pq" + // Import the postgres database driver. + + "golang.org/x/crypto/bcrypt" ) // Database represents an account database @@ -187,7 +187,7 @@ func (d *Database) createAccount( return nil, err } - if err := d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", json.RawMessage(`{ + if err := d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", []byte(`{ "global": { "content": [], "override": [], @@ -207,7 +207,7 @@ func (d *Database) createAccount( // update the corresponding row with the new content // Returns a SQL error if there was an issue with the insertion/update func (d *Database) SaveAccountData( - ctx context.Context, localpart, roomID, dataType string, content json.RawMessage, + ctx context.Context, localpart, roomID, dataType string, content []byte, ) error { return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error { return d.accountDatas.insertAccountData(ctx, txn, localpart, roomID, dataType, content) @@ -218,8 +218,8 @@ func (d *Database) SaveAccountData( // If no account data could be found, returns an empty arrays // Returns an error if there was an issue with the retrieval func (d *Database) GetAccountData(ctx context.Context, localpart string) ( - global map[string]json.RawMessage, - rooms map[string]map[string]json.RawMessage, + global map[string][]byte, + rooms map[string]map[string][]byte, err error, ) { return d.accountDatas.selectAccountData(ctx, localpart) @@ -231,7 +231,7 @@ func (d *Database) GetAccountData(ctx context.Context, localpart string) ( // Returns an error if there was an issue with the retrieval func (d *Database) GetAccountDataByType( ctx context.Context, localpart, roomID, dataType string, -) (data json.RawMessage, err error) { +) (data []byte, err error) { return d.accountDatas.selectAccountDataByType( ctx, localpart, roomID, dataType, ) diff --git a/userapi/storage/accounts/sqlite3/account_data_table.go b/userapi/storage/accounts/sqlite3/account_data_table.go index 870a37065..1475627aa 100644 --- a/userapi/storage/accounts/sqlite3/account_data_table.go +++ b/userapi/storage/accounts/sqlite3/account_data_table.go @@ -17,7 +17,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "github.com/matrix-org/dendrite/internal/sqlutil" ) @@ -75,7 +74,7 @@ func (s *accountDataStatements) prepare(db *sql.DB) (err error) { } func (s *accountDataStatements) insertAccountData( - ctx context.Context, txn *sql.Tx, localpart, roomID, dataType string, content json.RawMessage, + ctx context.Context, txn *sql.Tx, localpart, roomID, dataType string, content []byte, ) error { _, err := sqlutil.TxStmt(txn, s.insertAccountDataStmt).ExecContext(ctx, localpart, roomID, dataType, content) return err @@ -84,8 +83,8 @@ func (s *accountDataStatements) insertAccountData( func (s *accountDataStatements) selectAccountData( ctx context.Context, localpart string, ) ( - /* global */ map[string]json.RawMessage, - /* rooms */ map[string]map[string]json.RawMessage, + /* global */ map[string][]byte, + /* rooms */ map[string]map[string][]byte, error, ) { rows, err := s.selectAccountDataStmt.QueryContext(ctx, localpart) @@ -93,8 +92,8 @@ func (s *accountDataStatements) selectAccountData( return nil, nil, err } - global := map[string]json.RawMessage{} - rooms := map[string]map[string]json.RawMessage{} + global := map[string][]byte{} + rooms := map[string]map[string][]byte{} for rows.Next() { var roomID string @@ -107,7 +106,7 @@ func (s *accountDataStatements) selectAccountData( if roomID != "" { if _, ok := rooms[roomID]; !ok { - rooms[roomID] = map[string]json.RawMessage{} + rooms[roomID] = map[string][]byte{} } rooms[roomID][dataType] = content } else { @@ -120,7 +119,7 @@ func (s *accountDataStatements) selectAccountData( func (s *accountDataStatements) selectAccountDataByType( ctx context.Context, localpart, roomID, dataType string, -) (data json.RawMessage, err error) { +) (data []byte, err error) { var bytes []byte stmt := s.selectAccountDataByTypeStmt if err = stmt.QueryRowContext(ctx, localpart, roomID, dataType).Scan(&bytes); err != nil { @@ -129,6 +128,6 @@ func (s *accountDataStatements) selectAccountDataByType( } return } - data = json.RawMessage(bytes) + data = []byte(bytes) return } diff --git a/userapi/storage/accounts/sqlite3/storage.go b/userapi/storage/accounts/sqlite3/storage.go index 92c1c669e..e278be38c 100644 --- a/userapi/storage/accounts/sqlite3/storage.go +++ b/userapi/storage/accounts/sqlite3/storage.go @@ -17,7 +17,6 @@ package sqlite3 import ( "context" "database/sql" - "encoding/json" "errors" "strconv" "sync" @@ -28,6 +27,7 @@ import ( "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/storage/accounts/sqlite3/deltas" "github.com/matrix-org/gomatrixserverlib" + "golang.org/x/crypto/bcrypt" ) @@ -219,7 +219,7 @@ func (d *Database) createAccount( return nil, err } - if err := d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", json.RawMessage(`{ + if err := d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", []byte(`{ "global": { "content": [], "override": [], @@ -239,7 +239,7 @@ func (d *Database) createAccount( // update the corresponding row with the new content // Returns a SQL error if there was an issue with the insertion/update func (d *Database) SaveAccountData( - ctx context.Context, localpart, roomID, dataType string, content json.RawMessage, + ctx context.Context, localpart, roomID, dataType string, content []byte, ) error { d.accountDatasMu.Lock() defer d.accountDatasMu.Unlock() @@ -252,8 +252,8 @@ func (d *Database) SaveAccountData( // If no account data could be found, returns an empty arrays // Returns an error if there was an issue with the retrieval func (d *Database) GetAccountData(ctx context.Context, localpart string) ( - global map[string]json.RawMessage, - rooms map[string]map[string]json.RawMessage, + global map[string][]byte, + rooms map[string]map[string][]byte, err error, ) { return d.accountDatas.selectAccountData(ctx, localpart) @@ -265,7 +265,7 @@ func (d *Database) GetAccountData(ctx context.Context, localpart string) ( // Returns an error if there was an issue with the retrieval func (d *Database) GetAccountDataByType( ctx context.Context, localpart, roomID, dataType string, -) (data json.RawMessage, err error) { +) (data []byte, err error) { return d.accountDatas.selectAccountDataByType( ctx, localpart, roomID, dataType, )