Skip to content

Commit

Permalink
chore: add api timeout and im admin token cache #241 (#242)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
withchao authored Oct 26, 2023
1 parent 72bb2d5 commit d3315ae
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
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

0 comments on commit d3315ae

Please sign in to comment.