Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add api timeout and im admin token cache #241 #242

Merged
merged 33 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
76b93fe
fix: im api UserToken error non-responsive body
withchao Aug 7, 2023
34f9313
fix: set rand seed
withchao Aug 7, 2023
c2d9060
fix: set rand seed
withchao Aug 7, 2023
9e7f09c
Merge branch 'OpenIMSDK:main' into main
withchao Aug 7, 2023
a181e31
Merge branch 'OpenIMSDK:main' into main
withchao Aug 9, 2023
1c51d95
update version
withchao Aug 9, 2023
5fac974
Merge remote-tracking branch 'origin/main'
withchao Aug 9, 2023
4202a75
Merge branch 'OpenIMSDK:main' into main
withchao Aug 23, 2023
3a0bf48
fix: optimize error code
withchao Aug 23, 2023
ac6e9e5
fix: optimize error code
withchao Aug 23, 2023
6610068
Merge branch 'OpenIMSDK:main' into main
withchao Aug 23, 2023
51397b6
Merge branch 'OpenIMSDK:main' into main
withchao Sep 1, 2023
02bfda0
fix: chat admin userID
withchao Sep 1, 2023
6694940
fix: chat admin userID
withchao Sep 1, 2023
2028491
fix: chat admin userID
withchao Sep 1, 2023
de55c91
Merge branch 'OpenIMSDK:main' into main
withchao Sep 1, 2023
33bfb7a
Merge branch 'openimsdk:main' into main
withchao Sep 11, 2023
50cbcbd
feat: add SearchFriend
withchao Sep 11, 2023
a8c0829
feat: add SearchFriend
withchao Sep 11, 2023
4b04fce
feat: add SearchFriend
withchao Sep 11, 2023
84a988c
feat: add SearchFriend
withchao Sep 11, 2023
3aa5755
feat: add SearchFriend
withchao Sep 11, 2023
cbf9760
Merge branch 'openimsdk:main' into main
withchao Sep 14, 2023
1db1a2c
fix: admin char length
withchao Sep 14, 2023
9bc72de
Merge remote-tracking branch 'origin/main'
withchao Sep 14, 2023
2ccfe25
fix: phone already register code
withchao Sep 19, 2023
02acbfe
Merge branch 'openimsdk:main' into main
withchao Oct 25, 2023
fd9b622
feat: add api caller timeout
withchao Oct 25, 2023
068fdfb
feat: add api caller timeout
withchao Oct 25, 2023
470bd04
feat: im admin token cache
withchao Oct 25, 2023
5b7f104
feat: im admin token cache
withchao Oct 25, 2023
95435d6
feat: im admin token cache
withchao Oct 25, 2023
1737186
chore: update pkg github.com/openimsdk/tools
withchao Oct 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
10 changes: 9 additions & 1 deletion pkg/common/apicall/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"io"
"net/http"
"time"

"github.com/OpenIMSDK/chat/pkg/common/constant"
constant2 "github.com/OpenIMSDK/protocol/constant"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
26 changes: 22 additions & 4 deletions pkg/common/apicall/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}
Expand All @@ -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,
Expand Down
Loading