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 31 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
11 changes: 10 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,10 @@ 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
start := time.Now()
defer func() {
log.ZDebug(ctx, "call caller", "api", a.api, "cost", time.Since(start))
}()
log.ZInfo(ctx, "caller req", "addr", url, "req", req)
reqBody, err := json.Marshal(req)
if err != nil {
Expand All @@ -79,7 +88,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