Giter VIP home page Giter VIP logo

crud's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

bumblebeehkf

crud's Issues

dial tcp 192.168.2.14:3306: connectex: Only one usage of each socket address (protocol/network address/port) is normally permitted.

// Query 用于底层查询,一般是SELECT语句
func (api *CRUD) Query(sql string, args ...interface{}) SQLRows {
db := api.DB()
api.LogSQL(sql, args...)
rows, err := db.Query(sql, args...)
/

dial tcp 192.168.2.14:3306: connectex: Only one usage of each socket address (protocol/network address/port) is normally permitted.
*/
if err != nil {
api.stack(err, sql, args...)
}
return &SQLRows{rows: rows, err: err}
}

CRUD重构

CRUD拥有Create 、Read、 Update、Delete,remove to
FormCreate 、FormRead、 FormUpdate、FormDelete
需要新增Create 、Read、 Update、Delete 参数为结构体

底层全部调用Table Create 、Read、 Update、Delete
底层需要针对is_delete,delete_at,create_at

Only one usage of each socket address (protocol/network address/port) is normally permitted

Lets say that you are invoking a web service from another web service. Both are on the same box. You might be making authenticated
or unauthenticated calls and perhaps you are setting the KeepAlive = false.
Intermittently, (under load) you might get “Only one usage of each socket address (protocol/network address/port) is normally permitted (typically under load).”
You might be wondering why are you getting a SOCKET Exception…

Here is the scoop

  1. When you make authenticated calls, the client is closing connections. And when you are making authenticated calls
    repeatedly to the same server, you are making and closing connections repeatedly
  2. The same might happen when you are making regular http [un authenticated] calls but setting keep-alive = false.

When a connection is closed, on the side that is closing the connection the 5 tuple
{ Protocol, Local IP, Local Port, Remote IP, Remote Port} goes into a TIME_WAIT state for 240 seconds by default.
In this case, the protocol is fixed – TCP
the local IP, remote IP and remote PORT are also typically fixed. So the variable is the local port.
What happens is that when you don’t bind a port in the range 1024-5000 is used.
So roughly you have 4000 ports. If you use all of them in 4 minutes – meaning roughly you
make 16 web service calls per second for 4 minutes you will exhaust all the ports. That is the cause of this exception.

OK now how can this be fixed?

  1. One of the ways is to increase the dynamic port range. The max by default is 5000. You can set this up to 65534.
    HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort is the key to use.

  2. The second thing you can do is once the connection does get into an TIME_WAIT state you can reduce the time it is
    in that state, Default is 4 monutes, but you can set this to 30 seconds
    HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\TCPTimedWaitDelay is the key to use.
    Set this to 30 seconds

  3. If you don’t have access to registry you can do this through code.
    In System.Net 2.0 we addeed what is called a BindIPEndPOintDelegate
    What this does is to give you a chance to choose the local end point for the
    connection that is being made.
    See http://blogs.msdn.com/malarch/archive/2005/09/13/466664.aspx
    for more info.

Using this, the following sample code attempts to choose between 5001 and 65534
and wrap around when we reach 65534.
Req.ServicePoint.BindIPEndPointDelegate
= new BindIPEndPoint(BindIPEndPointCallback);
public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,
IPEndPoint remoteEndPoint,
int retryCount)
{
int port = Interlocked.Increment(ref m_LastBindPortUsed); //increment
Interlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534);
if(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) {
return new IPEndPoint(IPAddress.Any,port);
}
else {
return new IPEndPoint(IPAddress.IPv6Any,port);
}
}

Durgaprasad Gorti
[email protected]
Test Lead
System.Net

更好的使用用户信息

方案一:
使用context传递用户数据。
方案二:
在每个CRUD中将用户信息加入,然后返回*CRUD
这个时候在执行一些操作的时候,就去找这个字段,如果这个字段为空的话就不处理,如果有用户信息的话就记录下[TIME] XXX操作了XXX

底层关于自动时间的设置

底层创建的时候会自动会为CreatedAt设置值
更新的时候会自动为UpdatedAt设置值
删除的时候会自动设置IsDeleted和DeletedAt

主函数污染

如果table进行搜索了(table.RowsMap()),那么table下面所有的条件都会一直使用之前的搜索语句。

	mdl.MSSFloor.RowsMap()
	floorid := 2
	mdl.MSSFloor.Debug(true)
	if floorid != 0 {
		fmt.Println(mdl.MSSFloor.Fields("name").WhereID(floorid).String())
	}

output 1

remove build.go

package crud

import (
"bytes"
"strings"
)

type SQLBuild struct {
count int
colums []string
container *bytes.Buffer
}

func NewSQLBuild(tableName string, colums ...string) *SQLBuild {
build := new(SQLBuild)
build.count = 0
for _, v := range colums {
build.colums = append(build.colums, v)
}
build.container = bytes.NewBufferString("INSERT INTO " + tableName + " (")
for idx := range colums {
colums[idx] = "" + colums[idx] + ""
}
build.container.WriteString(strings.Join(colums, ","))
build.container.WriteString(") VALUES")
return build
}

func (b *SQLBuild) AddValue(args ...string) {
b.count++
b.container.WriteByte('(')
length := len(args) - 1
for idx, arg := range args {
b.container.WriteByte(''')
b.container.Write(([]byte(arg)))
b.container.WriteByte(''')
if idx < length {
b.container.WriteByte(',')
}
}
b.container.WriteByte(')')
b.container.WriteByte(',')
}

func (b *SQLBuild) AddMap(m map[string]string) {
args := []string{}
for _, colnm := range b.colums {
args = append(args, m[colnm])
}
b.AddValue(args...)
}

func (b *SQLBuild) String() string {
sql := b.container.String()
return sql[:len(sql)-1]
}

func (b *SQLBuild) Count() int {
return b.count
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.