From d3315ae3dd0d3527512a85e2876cc8198068c035 Mon Sep 17 00:00:00 2001 From: withchao <48119764+withchao@users.noreply.github.com> Date: Wed, 25 Oct 2023 22:46:36 -0500 Subject: [PATCH] chore: add api timeout and im admin token cache #241 (#242) * fix: im api UserToken error non-responsive body * fix: set rand seed * fix: set rand seed * update version * fix: optimize error code * fix: optimize error code * fix: chat admin userID * fix: chat admin userID * fix: chat admin userID * feat: add SearchFriend * feat: add SearchFriend * feat: add SearchFriend * feat: add SearchFriend * feat: add SearchFriend * fix: admin char length * fix: phone already register code * feat: add api caller timeout * feat: add api caller timeout * feat: im admin token cache * feat: im admin token cache * feat: im admin token cache * chore: update pkg github.com/openimsdk/tools --- go.mod | 2 +- go.sum | 4 ++-- pkg/common/apicall/call.go | 10 +++++++++- pkg/common/apicall/caller.go | 26 ++++++++++++++++++++++---- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index c13e437ae..26a3cd66f 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( require ( github.com/OpenIMSDK/protocol v0.0.21 - github.com/OpenIMSDK/tools v0.0.14 + github.com/OpenIMSDK/tools v0.0.15 github.com/go-zookeeper/zk v1.0.3 github.com/redis/go-redis/v9 v9.1.0 ) diff --git a/go.sum b/go.sum index 4b7cc0b96..fb0e285ec 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/OpenIMSDK/open_utils v1.0.8 h1:IopxWgJwEF5ZAPsRuiZZOfcxNOQOCt/p8VDENc github.com/OpenIMSDK/open_utils v1.0.8/go.mod h1:FLoaQblWUVKQgqt2LrNzfSZLT6D3DICBn1kcOMDLUOI= github.com/OpenIMSDK/protocol v0.0.21 h1:5H6H+hJ9d/VgRqttvxD/zfK9Asd+4M8Eknk5swSbUVY= github.com/OpenIMSDK/protocol v0.0.21/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= -github.com/OpenIMSDK/tools v0.0.14 h1:WLof/+WxyPyRST+QkoTKubYCiV73uCLiL8pgnpH/yKQ= -github.com/OpenIMSDK/tools v0.0.14/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= +github.com/OpenIMSDK/tools v0.0.15 h1:FF3m0TQUG56pJC15a11jmBG6Y1EjXarEW4JV3CBF/Jc= +github.com/OpenIMSDK/tools v0.0.15/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4 h1:iC9YFYKDGEy3n/FtqJnOkZsene9olVspKmkX5A2YBEo= github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4/go.mod h1:sCavSAvdzOjul4cEqeVtvlSaSScfNsTQ+46HwlTL1hc= github.com/alibabacloud-go/darabonba-openapi v0.1.18/go.mod h1:PB4HffMhJVmAgNKNq3wYbTUlFvPgxJpTzd1F5pTuUsc= diff --git a/pkg/common/apicall/call.go b/pkg/common/apicall/call.go index 3ee2bb084..870ef0946 100644 --- a/pkg/common/apicall/call.go +++ b/pkg/common/apicall/call.go @@ -21,6 +21,7 @@ import ( "errors" "io" "net/http" + "time" "github.com/OpenIMSDK/chat/pkg/common/constant" constant2 "github.com/OpenIMSDK/protocol/constant" @@ -36,6 +37,10 @@ type baseApiResponse[T any] struct { Data *T `json:"data"` } +var client = &http.Client{ + Timeout: time.Second * 10, +} + type ApiCaller[Req, Resp any] interface { Call(ctx context.Context, req *Req) (*Resp, error) } @@ -64,6 +69,9 @@ func (a caller[Req, Resp]) Call(ctx context.Context, req *Req) (*Resp, error) { func (a caller[Req, Resp]) call(ctx context.Context, req *Req) (*Resp, error) { url := a.prefix() + a.api + defer func(start time.Time) { + log.ZDebug(ctx, "api call caller time", "api", a.api, "cost", time.Since(start).String()) + }(time.Now()) log.ZInfo(ctx, "caller req", "addr", url, "req", req) reqBody, err := json.Marshal(req) if err != nil { @@ -79,7 +87,7 @@ func (a caller[Req, Resp]) call(ctx context.Context, req *Req) (*Resp, error) { request.Header.Set(constant2.Token, token) log.ZDebug(ctx, "req token", "token", token) } - response, err := http.DefaultClient.Do(request) + response, err := client.Do(request) if err != nil { return nil, err } diff --git a/pkg/common/apicall/caller.go b/pkg/common/apicall/caller.go index 93cef4b3d..f60fbb6d7 100644 --- a/pkg/common/apicall/caller.go +++ b/pkg/common/apicall/caller.go @@ -16,7 +16,9 @@ package apicall import ( "context" - "fmt" + "github.com/OpenIMSDK/tools/log" + "sync" + "time" "github.com/OpenIMSDK/chat/pkg/common/config" "github.com/OpenIMSDK/protocol/auth" @@ -40,7 +42,11 @@ type CallerInterface interface { FriendUserIDs(ctx context.Context, userID string) ([]string, error) } -type Caller struct{} +type Caller struct { + token string + timeout time.Time + lock sync.Mutex +} func NewCallerInterface() CallerInterface { return &Caller{} @@ -58,11 +64,23 @@ func (c *Caller) ImportFriend(ctx context.Context, ownerUserID string, friendUse } func (c *Caller) ImAdminTokenWithDefaultAdmin(ctx context.Context) (string, error) { - return c.UserToken(ctx, config.GetDefaultIMAdmin(), constant.AdminPlatformID) + c.lock.Lock() + defer c.lock.Unlock() + if c.token == "" || c.timeout.Before(time.Now()) { + userID := config.GetDefaultIMAdmin() + token, err := c.UserToken(ctx, config.GetDefaultIMAdmin(), constant.AdminPlatformID) + if err != nil { + log.ZError(ctx, "get im admin token", err, "userID", userID) + return "", err + } + log.ZDebug(ctx, "get im admin token", "userID", userID) + c.token = token + c.timeout = time.Now().Add(time.Minute * 5) + } + return c.token, nil } func (c *Caller) UserToken(ctx context.Context, userID string, platformID int32) (string, error) { - fmt.Println(*config.Config.Secret) resp, err := userToken.Call(ctx, &auth.UserTokenReq{ Secret: *config.Config.Secret, PlatformID: platformID,